【问题标题】:pagination within a $_POST 'submit' [duplicate]$_POST“提交”中的分页[重复]
【发布时间】:2016-08-28 11:03:27
【问题描述】:

当我选择一个类别并点击提交按钮时,我的分页显示我的 SQL 语句检索到的记录数量所需的正确页面数量。但是,例如当我单击第 2 页时,页面刷新并且不再显示数据而不是显示下一页,它只是返回以显示类别下拉框和搜索按钮。

if(isset($_POST['search'])) {

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

  if (empty($filmCategory)) {
    $whereclause = '';
  }else {
    $whereclause = "where c.category_id = '$filmCategory'";
  }

  require_once('functions.php');

//user input
    $page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
    $perPage = isset($_GET['per-page']) && $_GET['per-page'] <=15 ? (int)$_GET['per_page'] : 10;

 //positioning
$start = ($page > 1) ? ($page * $perPage) - $perPage : 0;

//query 
  $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>";;

所以上面的一切都很好,但是当我选择第 2 页时,例如页面刷新并且所有数据都消失了

    $total = $db->query("SELECT FOUND_ROWS() AS total")->fetch()['total'];
//use CEIL to round up the pages so here are no pages with decimals
    $pages = ceil($total / $perPage);

    for($x = 1; $x <= $pages; $x++):;
    echo"<div id='pagination'>

                <a href='?page=$x'>$x</a>

            </div>";
    endfor;
}
?>

我希望它显示下一页记录,但我无法让它工作:(

【问题讨论】:

    标签: php forms pagination submit


    【解决方案1】:

    应该是,

    //positioning
    $start = isset($_GET['page']) && (int)$_GET['page'] > 1 ? ($_GET['page'] - 1) * $perPage : 0;
    

    【讨论】:

    • 没有工作,分页本身不是问题,它带来了我询问的正确数量的记录并显示页码但是当我点击链接时 if(isset($_POST['搜索'])) { 重置并且没有记录显示谢谢
    猜你喜欢
    • 2017-12-29
    • 2016-12-09
    • 1970-01-01
    • 1970-01-01
    • 2014-06-11
    • 1970-01-01
    • 1970-01-01
    • 2011-11-03
    • 2015-06-15
    相关资源
    最近更新 更多