分页搜索导致php无法在同一页面上运行?

时间:2016-05-10 11:21:45

标签: php mysql search pagination

我正在开发一个职位门户网站,我的索引页面中有搜索框,为此我尝试分页,但分页不起作用,如果我们点击下一步它也不显示任何内容     我是php的新手,我的sql跟着教程,现在正在开发

我无法弄清楚错误,结果显示,下一个按钮,工作正常但是当点击下一步它没有显示任何内容     这是我的代码

<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>
<?php
    if (isset($_POST['submit']))
    {
        $start = 0;
        $limit = 6;
        $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();
    ?>
<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
    $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 . "%' ) ");
    $stmt->execute();
    $rows = $stmt->rowCount();
    $total = ceil($rows / $limit);
    if ($pid > 1)
    {

        // Go to previous page to show previous 6 items. If its in page 1 then it is inactive

        echo "<div class='button'><a href='?pid=" . ($pid - 1) . "' >PREVIOUS</a></div>";
    }

    if ($id != $total)
    {

        // //Go to previous page to show next 6 items.

        echo "<div class='button'><a href='?pid=" . ($pid + 1) . "'>NEXT</a></div>";
    }

    ?>
<ul class='page'>
    <?php
        // show all the page link with page number. When click on these numbers go to particular page.

        for ($i = 1; $i <= $total; $i++)
        {
            if ($i == $id)
            {
                echo "<li class='current'>" . $i . "</li>";
            }
            else
            {
                echo "<li><a href='?pid=" . $i . "'>" . $i . "</a></li>";
            }
        }

        ?>
</ul>
<?php
    } ?>
<?php
    if (isset($_GET['pid']))
    {
        $id = $_GET['pid'];
        $start = ($pid - 1) * $limit;
    }
    else
    {
        $pid = 1;
    } ?>
if what i am following is not the best way suggest me any other better ways

0 个答案:

没有答案