PHP中的分页不在同一页面上工作?

时间:2016-05-11 05:21:53

标签: php mysql pagination

我正在开发一个职位门户网站,我的索引页面中有搜索框,为此我尝试分页,但是分页不起作用,如果我们点击数字它没有显示任何内容

我无法弄清楚错误,结果显示,数字按钮,工作正常,但点击数字时显示没有这是我的代码

<?php
    if( $user->is_logged_in() ){ header('Location: /emprdash'); } 
    if( ! empty($_SESSION['uid']))
    {
    header('Location: /emdash'); 
    }
    ?>
    <div class="container">
        <div class="row">
            <div class="col-md-12">
                <form action="" method="post"  >
                    <div id="adv-search" class="input-group">
                        <input class="form-control" name="term" id="term"  type="text" placeholder="Search for jobs" required />
                        <div class="input-group-btn">
                            <div class="btn-group"><button class="btn btn-primary" name="submit" type="submit" value="search"  >Search</button></div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <br/>
    <br/>
    <?php 
        if(isset($_POST['submit']))
        { 
        $limit=1;
        $p=$_GET['p']=="" ? 1:$_GET['p'];
        $start=($p-1)*$limit;
        $status='active';
        $term =  $_POST['term'];     
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $stmt = $db->prepare("SELECT * FROM job WHERE status='active' AND  ( jdesc LIKE '%".$term."%' OR  jtitle LIKE '%".$term."%' OR duration  LIKE '%".$term."%' OR budget  LIKE '%".$term."%' OR  keyskills  LIKE '%".$term."%' OR jdate  LIKE '%".$term."%' OR edate  LIKE '%".$term."%' OR cdexmin  LIKE '%".$term."%' OR cdexmax  LIKE '%".$term."%' ) ORDER BY jid DESC LIMIT $start,$limit  "); 
        $stmt->execute();
        $rows = $stmt->rowCount(); 
        ?>
    <?php if($rows>=1){ ?>
    <table class="table  table-responsive  table-inverse table-hover table-striped" >
        <thead>
            <tr class="info">
                <th> JobTitle</th>
                <th>Duration</th>
                <th>Budget</th>
                <th>Key Skills</th>
                <th>Posted Date</th>
                <th>Expiry Date</th>
                <th>Experience Minimum</th>
                <th>Experience Maximum</th>
                <th></th>
            </tr>
        </thead>
        <tbody>
            <?php while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
                $jid=$row['jid'];
                $repl = '<span class="highlight">' . $term . '</span>';
                ?>
            <tr class="success">
                <td>
                    <p><a href="/showjob?jid=<?php echo $row['jid']; ?>"><?php echo ucwords(str_ireplace($term, $repl, $row['jtitle'])); ?></a></p>
                </td>
                <td>
                    <p><?php echo str_ireplace($term, $repl, $row['duration']); ?></p>
                </td>
                <td>
                    <p><?php echo str_ireplace($term, $repl, $row['budget']); ?></p>
                </td>
                <td>
                    <p><?php echo str_ireplace($term, $repl, $row['keyskills']); ?></p>
                </td>
                <td>
                    <p><?php $jdate=strtotime( $row['jdate']);  echo str_ireplace($term, $repl, date('d/M/Y',$jdate)); ?></p>
                </td>
                <td>
                    <p><?php $edate=strtotime( $row['edate']);  echo str_ireplace($term, $repl, date('d/M/Y',$edate)); ?></p>
                </td>
                <td>
                    <p><?php echo str_ireplace($term, $repl,$row['cdexmin']);  ?></p>
                </td>
                <td>
                    <p><?php echo str_ireplace($term, $repl,$row['cdexmax']); ?></p>
                </td>
                <td>
                    <form method="POST" action="">
                        <input type="hidden" name="jid" value="<?php echo $jid; ?>" >
                        <center><button type="submit"  name="apply" class="btn btn-outlined btn-primary" >Login to Apply</button></center>
                    </form>
                </td>
            </tr>
            <?php
                }
                 ?>
        </tbody>
    </table>
    <?php } else {
        echo '<center><div class="well">No results to display</div></center>';} ?>
    <?php
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $stmt = $db->prepare("SELECT * FROM job WHERE status='active' AND  ( jdesc LIKE '%".$term."%' OR  jtitle LIKE '%".$term."%' OR duration  LIKE '%".$term."%' OR budget  LIKE '%".$term."%' OR  keyskills  LIKE '%".$term."%' OR jdate  LIKE '%".$term."%' OR edate  LIKE '%".$term."%' OR cdexmin  LIKE '%".$term."%' OR cdexmax  LIKE '%".$term."%' )  "); 
        $stmt->execute();
        $count= $stmt->rowCount(); 
        $countP=(ceil($count/$limit)) + 1;
        $tW=($countP*50) + $countP;
        echo"<center style='overflow-x:auto;margin-top:10px;padding-bottom:10px;'>";
        echo"<div style='width:".$tW."px'>";
        for($i=1;$i<$countP;$i++){
        $isC=$i==$_GET['p'] ? "b-green":"";
        echo "<a href='?p=$i'><button class='pgbutton $isC'>$i</button></a>";
        }
        echo"</div>";
        echo"</center>";
        ?>
    <?php } ?>
    <?php
        if(isset($_POST['apply'])) 
        {
        $jobid=$_POST['jid'];
        $_SESSION['url'] = "/applyjob?jid=$jobid"; 
        header("Location:/login");
        }
        ?>
<style>
    .pgbutton{
    width:45px;
    margin:0px 5px;
    }
</style>

1 个答案:

答案 0 :(得分:0)

它无法正常工作的原因是您点击链接<a href='?p=$i'>进入下一页。点击该链接后,您将丢失$ _POST值,因为您不再提交表单。

您需要更改您的实现,可能会更改页面上的网址并使用htaccess重写将搜索字词存储在网址中。或者将搜索词存储在会话中。因此,您始终可以使用搜索字词。

您可能想要隐藏输入

<input type="hidden" name="page" value="1" />

更改您的页面链接

<a href="" data-pagenumber="2" class="page_link">dsfsdf</a>

然后是一些JS - (请注意这使用jquery javascript库)

<script>
$(function() {
$('a.page_link').click(function(event) {
event.preventDefault();

// update page input
$('input[name="page"]').val($(this).data('pagenumber'));

$('input[name="page"]').closest('form').submit();

return false;
}
</script>