简体   繁体   中英

Pagination in PHP not working on same page?

I am developing a job portal where i have search box in my index page and for that i tried for pagination but pagination is not working and also if we click number it displaying nothing

i can't figure out the mistakes, results displaying , numbers button, is working perfectly but when click on number it is displaying nothing Here is my code

<?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>

The reason it's not working is because you are clicking a link <a href='?p=$i'> to get to the next page. Once you click that link you'll lose your $_POST values because you are no longer submitting a form.

You need to change your implementation maybe change the url on the page and use htaccess rewrite to store the search term in the url. Or store the search term in the session. so you'll always have the search term available.

You might want to put a hidden input

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

change your page links

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

Then some JS - (please note this uses jquery javascript library)

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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