Table of Contents
'.$v['num'].'
'.$v['name'].'
回复讨论(解决方案)
Home Backend Development PHP Tutorial 一个分页的问题:不能分页查询

一个分页的问题:不能分页查询

Jun 23, 2016 pm 01:02 PM

代码写的比较乱  如果不想看代码只看我表述也行 然后提交解决的办法就行
代码是这样的 就是类似百度的一个搜索页 道理也是百度搜索这样的 有一个搜索框
 下面是一个div显示查询结果 比如我搜索PHP 然后就去表里面的num字段下查找PHP的内容 查询的所有结果以分页的形式展示出来 
就是第一页 第二页……   现在的问题是这个查询结果是根据用户每次提交时$_POST['num']来查询数据库的  
如果每页显示5条数据 现在查询出来3页数据 每次查询可以显示数据和分页信息  但是每次点击分页时就会报错 比如点击第二页时就是提示num未定义
因为点击第二页时没有提交的$_POST['num']数据 只有一个post过来的page=2 所以会报错 
 现在的问题就是只能查询第一页的数据  点第二页就会报错 知道问题出在哪里就是不知道怎么改  用户在点击第二页时希望之前查询出来的数据不要消失 这个应该怎么办呢?这样说不知道大家能不能看懂啊

<div id="show">       <?php       header('content-type:text/html;charset=utf-8;');       include 'function.php';       if (isset($_POST['num'])||isset($_GET['page'])) {       $pdo=new PDO("mysql:host=localhost;dbname=t1","root","");       session_start();       $_SESSION['num']=$_POST['num'];       $stmt3=$pdo->prepare("select * from table1 where num=?");       $stmt3->execute(array($_POST['num']));       $res3=$stmt3->fetchall();       $rows=count($res3);       $pagesize=5;       if($rows==0){        $pagenum=1;       }else{        $pagenum=ceil($rows/$pagesize);       }       if(isset($_GET['page'])){          $page=$_GET['page'];          if(empty($page)||$page<0||!is_numeric($page)) {            $page=1;          }else{            $page=intval($page);          }        }else{        $page=1;       }       if($page>$pagenum){        $page=$pagenum;       }       $startnum = ($page - 1)*$pagesize;       $display_page=5;       $query = "SELECT * FROM qq where num=? LIMIT $startnum,$pagesize";       $stmt=$pdo->prepare("$query");       //$stmt=$pdo->prepare("select * from table1 where num=?");       $stmt->execute(array($_POST['num']));       $res=$stmt->fetchall(PDO::FETCH_ASSOC);       $v = current($res);       if($res){         foreach($res as $v){          //echo '<h3 id="span-v-num-span"><span>'.$v['num'].'</span></h3>' ;          echo '<h4 id="v-name">'.$v['name'].'</h4>';         }       }else{        echo "无数据";       }               }       ?>     </div>     <div id="page">        <?php         if(isset($_POST['num'])||isset($_GET['page'])){           fenye();         }       ?>     </div>
Copy after login


回复讨论(解决方案)

你的 num 是 post 提交的,从第二页起就没有 post 提交了,自然也就没有了 num

因为你点第一页,第二页时,用的是GET方法传递,而不是POST方法。

所以红色部分出问题
$query = "SELECT * FROM qq where num=? LIMIT $startnum,$pagesize";
$stmt=$pdo->prepare("$query");
//$stmt=$pdo->prepare("select * from table1 where num=?");
$stmt->execute(array($_POST['num']));

然后fenye方法要把num和page传入去
分页的url要加上这两个参数:?page=xxx&num=xxx

这样改试试

<div id="show">       <?php       header('content-type:text/html;charset=utf-8;');       include 'function.php';       $num = isset($_REQUEST['num'])? $_REQUEST['num'] : 0;       $page = isset($_REQUEST['page'])? $_REQUEST['page'] : 1;        $pdo=new PDO("mysql:host=localhost;dbname=t1","root","");        session_start();        $_SESSION['num']=$num;        $stmt3=$pdo->prepare("select * from table1 where num=?");        $stmt3->execute(array($num));        $res3=$stmt3->fetchall();        $rows=count($res3);        $pagesize=5;        if($rows==0){            $pagenum=1;        }else{            $pagenum=ceil($rows/$pagesize);        }        if($page){            if(empty($page)||$page<0||!is_numeric($page)) {                $page=1;            }else{                $page=intval($page);            }         }else{            $page=1;        }        if($page>$pagenum){            $page=$pagenum;        }        $startnum = ($page - 1)*$pagesize;        $display_page=5;        $query = "SELECT * FROM qq where num=? LIMIT $startnum,$pagesize";        $stmt=$pdo->prepare("$query");        //$stmt=$pdo->prepare("select * from table1 where num=?");        $stmt->execute($num);        $res=$stmt->fetchall(PDO::FETCH_ASSOC);        $v = current($res);        if($res){            foreach($res as $v){                //echo '<h3 id="span-v-num-span"><span>'.$v['num'].'</span></h3>' ;                echo '<h4 id="v-name">'.$v['name'].'</h4>';            }        }else{         echo "无数据";        }       ?>     </div>     <div id="page">        <?php         if($num || $page){           fenye($num, $page);         }       ?>     </div>
Copy after login
Copy after login

你说像百度那样子,你就看看百度的下一页链接。搜索词都是在地址参数上的,用GET就行

还是不行 只是现在不报错了 点击第几页后还是什么都不显示内容


因为你点第一页,第二页时,用的是GET方]法传递,而不是POST方法。

所以红色部分出问题
$query = "SELECT * FROM qq where num=? LIMIT $startnum,$pagesize";
$stmt=$pdo->prepare("$query");
//$stmt=$pdo->prepare("select * from table1 where num=?");
$stmt->execute(array($_POST['num']));

然后fenye方法要把num和page传入去
分页的url要加上这两个参数:?page=xxx&num=xxx

这样改试试

<div id="show">       <?php       header('content-type:text/html;charset=utf-8;');       include 'function.php';       $num = isset($_REQUEST['num'])? $_REQUEST['num'] : 0;       $page = isset($_REQUEST['page'])? $_REQUEST['page'] : 1;        $pdo=new PDO("mysql:host=localhost;dbname=t1","root","");        session_start();        $_SESSION['num']=$num;        $stmt3=$pdo->prepare("select * from table1 where num=?");        $stmt3->execute(array($num));        $res3=$stmt3->fetchall();        $rows=count($res3);        $pagesize=5;        if($rows==0){            $pagenum=1;        }else{            $pagenum=ceil($rows/$pagesize);        }        if($page){            if(empty($page)||$page<0||!is_numeric($page)) {                $page=1;            }else{                $page=intval($page);            }         }else{            $page=1;        }        if($page>$pagenum){            $page=$pagenum;        }        $startnum = ($page - 1)*$pagesize;        $display_page=5;        $query = "SELECT * FROM qq where num=? LIMIT $startnum,$pagesize";        $stmt=$pdo->prepare("$query");        //$stmt=$pdo->prepare("select * from table1 where num=?");        $stmt->execute($num);        $res=$stmt->fetchall(PDO::FETCH_ASSOC);        $v = current($res);        if($res){            foreach($res as $v){                //echo '<h3 id="span-v-num-span"><span>'.$v['num'].'</span></h3>' ;                echo '<h4 id="v-name">'.$v['name'].'</h4>';            }        }else{         echo "无数据";        }       ?>     </div>     <div id="page">        <?php         if($num || $page){           fenye($num, $page);         }       ?>     </div>
Copy after login
Copy after login

就是啊 这个问题怎么解决呢?

你的 num 是 post 提交的,从第二页起就没有 post 提交了,自然也就没有了 num

这个那个分页函数的代码
function fenye(){
global $page,$pagenum,$shoupage,$pageoffset;
echo "

";
echo "
    ";
    echo '
  • '.$page.'/'.$pagenum.'页|
  • ';
    if($page==1){
    echo '
  • 首页|
  • ';
    echo '
  • 上一页|
  • ';
    }else{
    echo '
  • 首页|
  • ';
    echo '
  • 上一页|
  • ';
    }
    $shoupage=5;
    $pageoffset=($shoupage-1)/2;
    $start=1;
    $end=$pagenum;
    if($pagenum>$shoupage){
    if($page>$pageoffset){
    $start=$page-$pageoffset;
    $end=$pagenum>$page+$pageoffset?$page+$pageoffset:$pagenum;
    }else{
    $start=1;
    $end=$pagenum>$shoupage?$shoupage:$pagenum;
    }
    if($page+$pageoffset>$pagenum){
    $start=$start-($page+$pageoffset-$end);
    }
    }
    echo '
    ';
    echo '
      ';
      for($i=$start;$i<=$end;$i++){
      echo '
    • '.($i).'
    • ';
      }
      echo '
    ';
    echo '
    ';
    if($page==$pagenum){
    echo '
  • |下一页|
  • ';
    echo '
  • 尾页|
  • ';
    }else{
    echo '
  • |下一页|
  • ';
    echo '
  • 尾页|
  • ';
    }
    echo '
';
echo '
';

};
?>

在链接中加入 num=$num

把原来这句话 echo '

  • '.($i).'
  • ';
    改成了这样echo '
  • ';这样改对吗 但是还是提示我拼接错误 为什么?Parse error: syntax error, unexpected '=', expecting ',' or ';' in D:\wamp\www\function.php on line 33

    在链接中加入 num=$num

    改这样

    <?phpfunction fenye($num){global $page,$pagenum,$shoupage,$pageoffset;echo "<div >";echo "<ul id='ul2'>";echo '<li>'.$page.'/'.$pagenum.'页|</li>';   if($page==1){             echo '<li>首页|</li>';             echo '<li>上一页|</li>';   }else{     echo '<li><a href="'.$_SERVER["SCRIPT_NAME"].'">首页</a>|</li>';             echo '<li><a href="'.$_SERVER["SCRIPT_NAME"].'?'.($page-1).'&num='.$num.'">上一页</a>|</li>';   }   $shoupage=5;       $pageoffset=($shoupage-1)/2;       $start=1;       $end=$pagenum;   if($pagenum>$shoupage){        if($page>$pageoffset){        $start=$page-$pageoffset;        $end=$pagenum>$page+$pageoffset?$page+$pageoffset:$pagenum;        }else{        $start=1;        $end=$pagenum>$shoupage?$shoupage:$pagenum;        }        if($page+$pageoffset>$pagenum){        $start=$start-($page+$pageoffset-$end);        }       }   echo '<div>';       echo '<ul id="ul1">';       for($i=$start;$i<=$end;$i++){        echo '<li><a href="'.$_SERVER["SCRIPT_NAME"].'?page='.$i.'&num='.$num.'">'.($i).'</a></li>';       }       echo '</ul>';           echo '</div>';   if($page==$pagenum){    echo '<li>|下一页|</li>';    echo '<li>尾页|</li>';   }else{    echo '<li>|<a href="'.$_SERVER["SCRIPT_NAME"].'?page='.($page+1).'&num='.$num.'">下一页</a>|</li>';    echo '<li><a href="'.$_SERVER["SCRIPT_NAME"].'?page='.$pagenum.'&num='.$num.'">尾页</a>|</li>';   }echo '</ul>';    echo '</div>';    };?>
    Copy after login
    Copy after login


    然后调用改这样

    if($num || $page){
    fenye($num);
    }
    ?>

    哈哈可以了 我先好好看看你的代码

    改这样

    <?phpfunction fenye($num){global $page,$pagenum,$shoupage,$pageoffset;echo "<div >";echo "<ul id='ul2'>";echo '<li>'.$page.'/'.$pagenum.'页|</li>';   if($page==1){             echo '<li>首页|</li>';             echo '<li>上一页|</li>';   }else{     echo '<li><a href="'.$_SERVER["SCRIPT_NAME"].'">首页</a>|</li>';             echo '<li><a href="'.$_SERVER["SCRIPT_NAME"].'?'.($page-1).'&num='.$num.'">上一页</a>|</li>';   }   $shoupage=5;       $pageoffset=($shoupage-1)/2;       $start=1;       $end=$pagenum;   if($pagenum>$shoupage){        if($page>$pageoffset){        $start=$page-$pageoffset;        $end=$pagenum>$page+$pageoffset?$page+$pageoffset:$pagenum;        }else{        $start=1;        $end=$pagenum>$shoupage?$shoupage:$pagenum;        }        if($page+$pageoffset>$pagenum){        $start=$start-($page+$pageoffset-$end);        }       }   echo '<div>';       echo '<ul id="ul1">';       for($i=$start;$i<=$end;$i++){        echo '<li><a href="'.$_SERVER["SCRIPT_NAME"].'?page='.$i.'&num='.$num.'">'.($i).'</a></li>';       }       echo '</ul>';           echo '</div>';   if($page==$pagenum){    echo '<li>|下一页|</li>';    echo '<li>尾页|</li>';   }else{    echo '<li>|<a href="'.$_SERVER["SCRIPT_NAME"].'?page='.($page+1).'&num='.$num.'">下一页</a>|</li>';    echo '<li><a href="'.$_SERVER["SCRIPT_NAME"].'?page='.$pagenum.'&num='.$num.'">尾页</a>|</li>';   }echo '</ul>';    echo '</div>';    };?>
    Copy after login
    Copy after login


    然后调用改这样
         


                     if($num || $page){
               fenye($num);
             }
           ?>
         
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    AI Hentai Generator

    AI Hentai Generator

    Generate AI Hentai for free.

    Hot Article

    R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. Best Graphic Settings
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. How to Fix Audio if You Can't Hear Anyone
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    WWE 2K25: How To Unlock Everything In MyRise
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

    Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

    cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

    The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

    Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

    Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

    12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

    Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

    Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

    Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

    Customizing/Extending Frameworks: How to add custom functionality. Customizing/Extending Frameworks: How to add custom functionality. Mar 28, 2025 pm 05:12 PM

    The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

    Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

    Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

    See all articles