简体   繁体   English

php 分页限制 50

[英]pagination on php limit 50

Can anyone help me with the pagination?谁能帮我分页? I am trying this code on pagination and the pages are showing but when clicked on the pages like Next or the number with links it gives a syntax error.我正在分页上尝试此代码并且页面正在显示,但是当单击 Next 等页面或带有链接的数字时,它会出现语法错误。

"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?page=2' at line 1" “您的 SQL 语法有错误;请查看与您的 MySQL 服务器版本相对应的手册,以获取在第 1 行的 '?page=2' 附近使用的正确语法”

My tables data is around 250 and I wanted to limit it to 50 data per page.我的表格数据大约为 250,我想将其限制为每页 50 个数据。

Here is my code: (pagination section - is the problem)这是我的代码:(分页部分 - 是问题所在)

<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>'; }
        }
?>

You have error on html URL syntax您在 html URL 语法上有错误

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

The correct would be正确的是

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

You have to use ?你必须使用? only for the first URL parameter, & for the following, if any.仅用于第一个 URL 参数, &用于以下参数(如果有)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM