基于条件的分页,当我点击下一个时不工作

时间:2013-10-20 08:38:13

标签: php mysql

我有一个搜索页面,用户可以根据他们可以从下拉列表中选择的类别搜索项目。当我选择一个类别时,它显示数据和下一页,但是当我点击下一页时,页面不会显示任何结果。请帮忙

<?php
include("scripts/connection.php");
session_start();

if (empty($_SESSION["username"])) {
    header("Location:login.php");
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Search Posts</title>
        <link rel="stylesheet" href="style.css" type="text/css" media="all" />
    </head>
    <body>
        <div class="main-menu">
            <ul id="menu">
                <li><a href="mainhome.php">Home</a></li>
                <li><a href="itempost.php">New Post</a></li>
                <li><a href="yourpost.php">Your Posts</a></li>
                <li id="active"><a href="Search.php">Search Posts</a></li>
                <li class="log"><a href="scripts/logout.php">Log Out</a></li>
                <li class="log"><a href="account.php">Account</a></li>
            </ul>
        </div>
        <div id="header"><img src="Images/header.gif"/></div>
        <div id="sidebar"><h2><u>Posts</u></h2><p/> 
            <form method="POST" name="SearchForm" action="search.php">
                <table id="mainhomeform">
                    <tr>
                        <td>Select a category to search through:<p/> 
                        <select name="SearchCategory" class="searchlist" tabindex="1">
                            <option value="<?php if(isset($searchcategory)){echo $searchcategory;}else{echo "Uncategorized";}?>"><?php if(isset($searchcategory)){echo $searchcategory;}else{echo "Uncategorized";}?></option>
                            <option value="Clothing">Clothing</option>
                            <option value="Electronics">Electronics</option>
                            <option value="Furniture">Furniture</option>
                            <option value="Stationary">Stationary</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>
                        <input name="search" type="submit" class="wbutton" tabindex="3" value="Search" />
                    </td>
                </tr>
                </table>
            </form>
        </div> 
        <div id="content"><h2><img src="Images/posts.gif"/></h2>
            <?php
            if(isset($_POST['search'])) {
                $searchcategory = $_POST['SearchCategory'];
                $searchbutton = $_POST['search'];

                $search = "SELECT * FROM items WHERE Category='$searchcategory' ORDER BY PostDate DESC";
                $select = mysql_query($search);

                $nr = mysql_num_rows($select);

                if ($nr>0) {
                    if (isset($_GET['pn'])) {
                        $pn = preg_replace('#[^0-9]#i', '', $_GET['pn']);
                    } else {
                        $pn = 1;
                    }

                    $itemsPerPage = 5;
                    $lastPage = ceil($nr / $itemsPerPage);

                    if ($pn < 1) {
                        $pn = 1; 
                    } else if ($pn > $lastPage) {
                        $pn = $lastPage;
                    }

                    $centerPages = "";

                    $sub1 = $pn - 1;
                    $sub2 = $pn - 2;
                    $add1 = $pn + 1;
                    $add2 = $pn + 2;

                    if ($pn == 1) {
                        $centerPages .= '&nbsp; <span  class="pagenumbers">' . $pn . '</span> &nbsp;';
                        $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> &nbsp;';

                    } else if ($pn == $lastPage) {
                        $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> &nbsp;';
                        $centerPages .= '&nbsp; <span  class="pagenumbers">' . $pn . '</span> &nbsp;';

                    } else if ($pn > 2 && $pn < ($lastPage - 1)) {
                        $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub2 . '">' . $sub2 . '</a> &nbsp;';
                        $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> &nbsp;';

                        $centerPages .= '&nbsp; <span  class="pagenumbers">' . $pn . '</span> &nbsp;';
                        $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> &nbsp;';
                        $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add2 . '">' . $add2 . '</a> &nbsp;';

                    } else if ($pn > 1 && $pn < $lastPage) {
                        $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> &nbsp;';
                        $centerPages .= '&nbsp; <span  class="pagenumbers">' . $pn . '</span> &nbsp;';
                        $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> &nbsp;';
                    }

                    $limit = 'LIMIT ' .($pn - 1) * $itemsPerPage .',' .$itemsPerPage;
                    $select2 = mysql_query("SELECT * FROM items WHERE Category='$searchcategory' ORDER BY PostDate DESC $limit");

                    $select2_array = array();

                    $paginationDisplay = "";

                    if ($lastPage != "1") {
                        if ($pn != 1) {
                            $previous = $pn - 1;
                            $paginationDisplay .=  '&nbsp;  <a class="button" href="' . $_SERVER['PHP_SELF'] . '?pn=' . $previous . '"> Back</a> ';
                        }

                        $paginationDisplay .= '<span  class="pagenumbers">' . $centerPages . '</span>';

                        if ($pn != $lastPage) {
                            $nextPage = $pn + 1;
                            $paginationDisplay .=  '&nbsp;  <a class="button" href="' . $_SERVER['PHP_SELF'] . '?pn=' . $nextPage . '"> Next</a> ';
                        }
                    }

                    for ($count=0; $row = mysql_fetch_array($select2);$count++) {
                        $select2_array[$count] = $row;

                        echo "<table id='home'>";

                        foreach($select2_array as $itemshome);

                        echo "<tr>";
                        echo "<td class='title'>". $itemshome ['Title']."</td>";
                        echo "<td class='rf'>". $itemshome ['PostDate']."<br/>".$itemshome ['Category']."</td>";
                        echo "<hr>";
                        echo "</tr>";
                        echo "<tr>";
                        echo "<td colspan='3'>"."<img src='$itemshome[Image1]' class='pic' alt='userimage' />";
                        echo "<img src='$itemshome[Image2]' class='pic' alt='userimage' />";
                        echo "<img src='$itemshome[Image3]' class='pic' alt='userimage' />"."</td>";
                        echo "</tr>";
                        echo "<tr>";
                        echo "<td colspan='3'>"."<textarea name='Description' type='text' class='description' tabindex='2' readonly >".$itemshome ['Description']."</textarea>"."</td>";
                        echo "</tr>";
                        echo "</tr>";
                    }

                    echo "</table>";
                    echo "$paginationDisplay";

                } else {
                    $empty = '<div class="error">That category is empty.</div>';

                    echo $empty;
                }
            }
            ?>

            <div class="footer">
                <hr/>
                Copyright &copy;<br/>Designed Using XHTML 1.1
                <p id="rf">Designed by Mutondo Sitemba</p>
            </div>
        </div>
    </body>
</html>

2 个答案:

答案 0 :(得分:0)

您的链接有pn,但没有searchCategory。所以我想你的查询是: SELECT * FROM items WHERE Category =''ORDER BY PostDate DESC

当您遇到空结果问题时,作为第一步,您应该打印查询并查看它是否正确合成。通常这有帮助。

答案 1 :(得分:0)

问题可能在于您检查 isset($ _ POST ['search'])?如果单击“下一步”,表单

中将没有发布变量