2016-10-12 17 views
-3

誰でもページネーションを手伝ってくれますか?私はこのコードをページネーションにしようとしていますが、ページは表示されていますが、Nextやリンク数などのページをクリックすると構文エラーが発生します。PHPページ上のページ区切り50

「あなたはあなたのSQL構文でエラーが発生している。ライン1に近い使用する権利構文についてはMySQLサーバのバージョンに対応するマニュアルを確認してください 『?ページ= 2』」

私のテーブルのデータが周りです250と私は1ページあたり50データに制限したかった。ここで

は私のコードです: -

<link rel="stylesheet" type="text/css" href="css/navbar.css"> 

<?php include 'navbar.php';?> 


<br> 
<?php 
if(!isset($_GET['table'])){ 
    echo 'You must assign a table to view.'; 
    exit; 
} 

session_start(); 

//Connect here 
$conn = mysqli_connect("localhost", "root", "", "dkusers"); 

$table = mysqli_real_escape_string($conn, $_GET['table']); 
$fields = array(); 
$q = mysqli_query($conn, "SHOW COLUMNS FROM " . $table) or die(mysqli_error($conn)); 
while($r = mysqli_fetch_assoc($q)) $fields[count($fields)] = $r['Field']; 

echo '<b><font size="4">Table: </b>', $table, '</font><br>'; 

// -----------------INSERT----------------- 
if(isset($_POST['action']) && $_POST['action'] == "Insert"){ 
    if(!isset($_POST['insert'])){ 
     echo '<h3>Insert Row</h3>'; 
     echo '<form method="post"><input type="hidden" name="action" value="' . $_POST['action'] . '" />'; 
     echo '<table border="1" cellpadding="7"><tr><th>Field</th><th>Value</th><th>MD5</th></tr>'; 
     foreach($fields as $key => $value){ 
      echo '<tr><td>' . $value . ':</td><td><input type="text" name="field_' . $value . '" /></td><td><input type="checkbox" name="md5_' . $value . '" /></td></tr>'; 
     } 
     echo '<tr><td><input type="submit" name="insert" value="Submit" /></td><td colspan="2"><a href="?table=' . $_GET['table'] . '">Back</a></td></tr></table></form>'; 
     exit; 
    }else{ 
     $first = true; 
     $query = "INSERT INTO " . $table; 
     foreach($_POST as $key => $value){ 
      if(strrpos($key, "field_", -strlen($key)) !== false){ 
       $key = substr($key, 6); 
       $query .= sprintf("%s%s", ($first) ? " (" : ",", $key); 
       $first = false; 
      } 
     } 
     $query .= ") VALUES"; 
     $first = true; 
     foreach($_POST as $key => $value){ 
      if(strrpos($key, "field_", -strlen($key)) !== false){ 
       $key = substr($key, 6); 
       $query .= sprintf("%s'%s'", ($first) ? " (" : ",", (isset($_POST['md5_' . $key])) ? md5($value) : $value); 
       $first = false; 
      } 
     } 
     $q = mysqli_query($conn, $query . ")"); 
     if($q) echo 'Successfully inserted row into table!<br/><br/>'; else echo mysqli_error($conn) . '<br/><br/>'; 
    } 
} 

// -----------------DELETE----------------- 
if(isset($_POST['action']) && $_POST['action'] == "Delete"){ 
    if(!isset($_POST['rows'])){ 
     echo 'You didn\'t send any rows to delete.<br/><br/>'; 
    }else{ 
     $count = 0; 
     for($i = 0;$i < count($_POST['rows']);$i++){ 
      if($_POST['rows'][$i] >= count($_SESSION['store'])) continue; 
      $query = "DELETE FROM " . $table . ""; 
      $row = $_SESSION['store'][$_POST['rows'][$i]]; 
      $first = true; 
      foreach($row as $key => $value){ 
       $query .= sprintf(" %s %s = '%s'", ($first) ? "WHERE" : "AND", $key, $value); 
       $first = false; 
      } 
      $q = mysqli_query($conn, $query . " LIMIT 1"); 
      if(!$q) echo mysqli_error($conn) . '<br/>'; 
      $count += mysqli_affected_rows($conn); 
     } 
     echo 'Successfully deleted ' . $count . ' row(s)!<br/><br/>'; 
    } 
} 

// -----------------MODIFY----------------- 
if(isset($_POST['action']) && $_POST['action'] == "Modify"){ 
    if(!isset($_POST['rows'])){ 
     echo 'You didn\'t send any rows to modify.<br/><br/>'; 
    }else if(isset($_POST['modify'])){ 
     $count = 0; 
     for($i = 0;$i < count($_POST['rows']);$i++){ 
      if($_POST['rows'][$i] >= count($_SESSION['store'])) continue; 
      $first = true; 
      $query = "UPDATE " . $table . " SET"; 
      foreach($_POST as $key => $value){ 
       if(strrpos($key, "field_", -strlen($key)) !== false){ 
        $key = explode("_", $key, 3); 
        if($key[1] == $i){ 
         $query .= sprintf(((!$first) ? "," : "") . " %s = '%s'", $key[2], (isset($_POST['md5_' . $key[1] . '_' . $key[2]])) ? md5($value) : $value); 
         $first = false; 
        } 
       } 
      } 
      $row = $_SESSION['store'][$_POST['rows'][$i]]; 
      $first = true; 
      foreach($row as $key => $value){ 
       $query .= sprintf(" %s %s = '%s'", ($first) ? "WHERE" : "AND", $key, $value); 
       $first = false; 
      } 
      $q = mysqli_query($conn, $query . " LIMIT 1"); 
      if(!$q) echo mysqli_error($conn) . '<br/>'; 
      $count += mysqli_affected_rows($conn); 
     } 
     echo 'Successfully updated ' . $count . ' row(s)!<br/><br/>'; 
    }else{ 
     echo '<h3>Modify Row</h3>'; 
     echo '<form method="post"><input type="hidden" name="action" value="' . $_POST['action'] . '" />'; 
     for($i = 0;$i < count($_POST['rows']);$i++) if($_POST['rows'][$i] < count($_SESSION['store'])) echo '<input type="hidden" name="rows[]" value="' . $_POST['rows'][$i] . '" />'; 
     echo '<table border="1" cellpadding="7"><tr><th>Field</th><th>Value</th><th>MD5</th></tr>'; 
     for($i = 0;$i < count($_POST['rows']);$i++){ 
      if($_POST['rows'][$i] >= count($_SESSION['store'])) continue; 
      if($i != 0) echo '<tr><td colspan="3"><hr/></td></tr>'; 
      $row = $_SESSION['store'][$_POST['rows'][$i]]; 
      foreach($row as $key => $value){ 
       echo '<tr><td>' . $key . ':</td><td><input type="text" name="field_' . $i . '_' . $key . '" value="' . $value . '" /></td><td><input type="checkbox" name="md5_' . $i . '_' . $key . '" /></td></tr>'; 
      } 
     } 
     echo '<tr><td><input type="submit" name="modify" value="Submit" /></td><td colspan="2"><a href="?table=' . $_GET['table'] . '">Back</a></td></tr></table></form>'; 
     exit; 
    } 
} 


// -----------------SEARCH----------------- 
echo '<br><form method="post">Search: <input type="text" name="filter" value="' . ((isset($_POST['filter'])) ? $_POST['filter'] : '') . '"/><br/>Filter by: <br/>'; 
for($i = 0;$i < count($fields);$i++) echo '<input type="checkbox" name="' . $fields[$i] . '"' . ((isset($_POST['filter']) && isset($_POST[$fields[$i]])) ? ' checked' : '') . '/>' . $fields[$i] . ' '; 
echo '</form><form method="post"><table border="1" cellpadding="7"><tr>'; 
for($i = 0;$i < count($fields);$i++) echo '<th>' . $fields[$i] . '</th>'; 
echo '<th>-</th></tr>'; 
$sql = "SELECT * FROM " . $table; 
if(isset($_POST['filter'])){ 
    $filter = mysqli_real_escape_string($conn, $_POST['filter']); 
    foreach($fields as $key => $value) if(!isset($_POST[$fields[$key]])) unset($fields[$key]); 
    if(count($fields) > 0){ 
     $first = true; 
     foreach($fields as $key => $value){ 
      $sql .= " " . (($first) ? "WHERE" : "OR") . " " . $value . " LIKE '%" . $filter . "%'"; 
      $first = false; 
     } 
    } 
} 
// -----------------Bottoms (Above table)----------------- 
echo '<input type="submit" name="action" value="Modify" /> <input onclick="return confirm(\'Are you sure you want to delete these rows?\')" type="submit" name="action" value="Delete" /> <input type="submit" name="action" value="Insert" /><br><br></form>'; 

$_SESSION['store'] = array(); 
$q = mysqli_query($conn, $sql) or die(mysqli_error($conn)); 
while($r = mysqli_fetch_assoc($q)){ 
    echo '<tr>'; 
    foreach($r as $key => $value) echo '<td>' . $value. '</td>'; 
    echo '<td><input type="checkbox" name="rows[]" value="' . count($_SESSION['store']) . '" /></td></tr>'; 
    $_SESSION['store'][count($_SESSION['store'])] = $r; 
} 

// -----------------Bottoms (Below table)----------------- 
echo '</table>'; 
//echo '<br><input type="submit" name="action" value="Modify" /> <input onclick="return confirm(\'Are you sure you want to delete these rows?\')" type="submit" name="action" value="Delete" /> <input type="submit" name="action" value="Insert" /></form>'; 
?> 


<br> 
// -----------------Pagination----------------- 
<br> 

<?php 
$start=0; 
$limit=50; 

    if(isset($_GET['page'])) 
    { 
     $page=$_GET['page']; 
     $start=($page-1)*$limit; 
    } 
    else{ 
     $page=1; 
    } 
    //Fetch from database first 10 items which is its limit. For that when page open you can see first 10 items. 
    $query=mysqli_query($conn,"select * from $table LIMIT $start, $limit"); 
?> 


<?php 
//print 10 items 
while($result=mysqli_fetch_array($query)) 
    { 
     //echo "<li>".$result['username']."</li>"; 
    } 
?> 


<?php 
//fetch all the data from database. 
$rows=mysqli_num_rows(mysqli_query($conn,"select * from $table")); 
//calculate total page number for the given table in the database 
$total=ceil($rows/$limit); 
if($page>1) 
{ 
    //Go to previous page to show previous 10 items. If its in page 1 then it is inactive 
    echo '<a href="?table=' . $_GET['table'] . '?page='.($page-1).'" class="button">PREVIOUS</a>'; 
} 
if($page!=$total) 
{ 
    ////Go to previous page to show next 10 items. 
    echo '<a href="?table=' . $_GET['table'] . '?page='.($page+1).'" class="button">NEXT</a>'; 
} 
?> 


<?php 
//show all the page link with page number. When click on these numbers go to particular page. 
     for($i=1;$i<=$total;$i++) 
     { 
      if($i==$page) { echo "<li class='current'>".$i."</li>"; } 

      else { echo '<li><a href="?table=' . $_GET['table'] . '?page='.$i.'">'.$i.'</a></li>'; } 
     } 
?> 
+1

[最小限で完全で検証可能な例を作成する] –

答えて

0

あなたは

echo '<a href="?table=' . $_GET['table'] . '?page='.($page+1).'" class="button">NEXT</a>'; 

正しい

echo '<a href="?table=' . $_GET['table'] . '&page='.($page+1).'" class="button">NEXT</a>'; 

だろうHTMLのURL構文に誤りがあります(ページネーションセクションは問題がある)あなた最初のURLパラメータには?、フォローインには&を使用する必要がありますg(もしあれば)。

+0

ありがとうございますが、私はまだ1ページに250のレコードを見ることができます。 1ページで50のデータをフィルタリングしていません。 – Nicocaine

+0

http://imgur.com/a/qHDWw - どのように見えるかの画像 – Nicocaine