【问题标题】:pagination doesnt link to where clause when new page clicked单击新页面时分页未链接到 where 子句
【发布时间】:2016-05-03 14:37:05
【问题描述】:

我有一个 where 子句,它显示所有类别,其中 id=来自上一页的输入值。分页工作给了我正确的页数,而 where 子句给了我正确的记录。

当我点击一个页面时,页面上显示的记录不是下一组必需的记录,而是所有记录

指向每个页面的链接不标识 where 子句

<?php
include ('dbconnect.php');
$db = dbConnect();  // function defined in dbconnect.php

echo $filmCategory = isset($_REQUEST['filmCategory']) ? $_REQUEST['filmCategory'] : null;

$whereclause = "where c.category_id = '$filmCategory'";


require_once('functions.php');

//user input this will limit the amount of records per page the user can see
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$perPage = isset($_GET['per-page']) && $_GET['per-page'] <=15 ? (int)$_GET['per_page'] : 10;

//positioning: replace certain variables within the query
//this will be where the rows start from the calculation will work out if page is greater than 1
//if the page is less than one it should be 0
$start = ($page > 1) ? ($page * $perPage) - $perPage : 0;
//query to fetch required records
$filmSQL =  $db->prepare("SELECT SQL_CALC_FOUND_ROWS  f.title, f.description, f.release_year, c.name,
f.rating, f.last_update
from nfc_film f
inner join nfc_film_category fc
on f.film_id = fc.film_id
inner join nfc_category c
on fc.category_id = c.category_id
$whereclause
LIMIT {$start}, {$perPage}");

// execute the query and get the title, description, release year, name, rating and last update of film
$filmSQL->execute();

//echo the table with the titles and correct data from SQL
echo "<table border=\"1\">\n";
    echo "<tr><th>Title</th><th>Description</th><th>Release Year</th><th>Category</th><th>Rating</th><th>Last Update</th></tr>";

    while ($filmInfo = $filmSQL->fetchObject()) {

    $upperLower= upperFirst(lowercase($filmInfo->title));
    $uLDescription= firstUpper(lowercase($filmInfo->description));
    $noChar = substr($uLDescription,0,100).'...';
    echo "<form action='filmInfo.php' method='get'>";
        echo "<tr>
            <td><input type='text' name='filmInfo' value='{$upperLower}'</td>
            <td><p>$noChar.</p></td>
            <td>{$filmInfo->release_year}</td>
            <td>{$filmInfo->name}</td>
            <td>{$filmInfo->rating}</td>
            <td>{$filmInfo->last_update}</td>
            <td><input type='submit' value='Film Details' </td>
        </tr>";
        echo" </form>";

    }
echo "</table>";
//pages
$total = $db->query("SELECT FOUND_ROWS() as total")->fetch()['total'];
$pages = ceil($total / $perPage);


?>

<div id="pagintation">
    <?php for($x = 1; $x <= $pages; $x++):  ?>
        <a href="?page=<?php echo $x; ?>"><?php echo $x; ?></a>
    <?php endfor;?>

【问题讨论】:

  • 旁注:如果你在这个&lt;div id="pagintation"&gt; 中使用 CSS,请确保你在这里使用的是同一个词。 pagintation 拼写错误。因此,如果您的 CSS 读取为 #pagination,您的 CSS 将无法正常工作。顺便说一句,这就是它的拼写方式。 ;-)

标签: php pagination where-clause


【解决方案1】:

将类别添加到链接中:

$cat = isset($_REQUEST['category']) ? "&category=" . $_REQUEST['category'] : '';
?>

<div id="pagintation">
    <?php for($x = 1; $x <= $pages; $x++):  ?>
        <a href="?page=<?php echo $x . $cat; ?>"><?php echo $x; ?></a>
    <?php endfor;?>

【讨论】:

  • @Fred-ii- 这是一个不同的问题,当他单击页面链接时,它根本没有显示表格。这是由顶部的if(isset($_POST['search'])) 引起的,他从这个版本中删除了。
  • 不过,两者中 95% 的代码是相同的。祝你好运;-)
  • @Fred-ii- 5% 的不同是第一个问题的解决方案。然后它发现了一个新问题。
  • 那么,OP/应答者应该在那里解决它。我没有看到新问题的意义,而另一个问题没有被标记为已解决,除非他们不知道 Stack 是如何滚动的 ;-)
  • 我已将其标记为副本,因为它具有正确的原始代码和第二个问题的解决方案。
猜你喜欢
  • 2014-01-14
  • 2014-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多