So basically everything shows when it's on search.php. However, once it gets to search.php?pn=2, The table and pagination navigation disappear. Here's the code
<?php require('includes/header.php'); ?>
<?php
/* Array values
* ------------
* $_POST['search']; // the search field
* $_POST['submitsearch']; // the search button
*
*/
if(isset($_POST['submitsearch'])) {
$search = mysql_real_escape_string(htmlentities(trim($_POST['search'])));
$search = preg_split('/[\s]+/', $search);
$total_words = count($search);
$postsearch = $_POST['search'];
if(empty($_POST['search'])) {
echo "Enter something to search";
}else{
if(strlen($_POST['search']) < 4) {
echo "Search must be 3 or more characters long";
}else{
$where = "";
foreach ($search as $key=>$searches) {
$where .= "description like '%{$searches}%'";
if($key != $total_words - 1) {
$where.= " and ";
}
}
$results = mysql_query("select name, left(description, 100) as description, logo, userid, number_sold, price from ns_products where $where") or die(mysql_error());
echo "<table border=1>";
if(mysql_num_rows($results)==0) {
echo "<tr><td>No results found for <strong>$postsearch</strong>.</td></tr>";
}else{
//originial query
$sql = mysql_query("select id, name, left(description, 100) as description, logo, userid, number_sold, price from ns_products where $where");
//end of original query
$nr = mysql_num_rows($sql);
if (isset($_GET['pn'])) {
$pn = preg_replace('#[^0-9]#i', '', $_GET['pn']);
} else {
$pn = 1;
}
$itemsPerPage = 3;
$lastPage = ceil($nr / $itemsPerPage);
if ($pn < 1) {
$pn = 1;
} else if ($pn > $lastPage) {
$pn = $lastPage; // force it to be $lastpage's value
}
// This creates the numbers to click in between the next and back buttons
$centerPages = ""; // Initialize this variable
$sub1 = $pn - 1;
$sub2 = $pn - 2;
$add1 = $pn + 1;
$add2 = $pn + 2;
if ($pn == 1) {
$centerPages .= ' <span class="pagNumActive">' . $pn . '</span> ';
$centerPages .= ' <a href="search.php?pn=' . $add1 . '">' . $add1 . '</a> ';
} else if ($pn == $lastPage) {
$centerPages .= ' <a href="search.php?pn=' . $sub1 . '">' . $sub1 . '</a> ';
$centerPages .= ' <span class="pagNumActive">' . $pn . '</span> ';
} else if ($pn > 2 && $pn < ($lastPage - 1)) {
$centerPages .= ' <a href="search.php?pn=' . $sub2 . '">' . $sub2 . '</a> ';
$centerPages .= ' <a href="search.php?pn=' . $sub1 . '">' . $sub1 . '</a> ';
$centerPages .= ' <span class="pagNumActive">' . $pn . '</span> ';
$centerPages .= ' <a href="search.php?pn=' . $add1 . '">' . $add1 . '</a> ';
$centerPages .= ' <a href="search.php?pn=' . $add2 . '">' . $add2 . '</a> ';
} else if ($pn > 1 && $pn < $lastPage) {
$centerPages .= ' <a href="search.php?pn=' . $sub1 . '">' . $sub1 . '</a> ';
$centerPages .= ' <span class="pagNumActive">' . $pn . '</span> ';
$centerPages .= ' <a href="search.php?pn=' . $add1 . '">' . $add1 . '</a> ';
}
$limit = 'LIMIT ' .($pn - 1) * $itemsPerPage .',' .$itemsPerPage;
$sql2 = mysql_query("select id, name, left(description, 100) as description, logo, userid, number_sold, price from ns_products where $where $limit");
$paginationDisplay = "";
if ($lastPage != "1"){
$paginationDisplay .= 'Page <strong>' . $pn . '</strong> of ' . $lastPage. '<img src="images/clearImage.gif" width="48" height="1"/>';
if ($pn != 1) {
$previous = $pn - 1;
$paginationDisplay .= ' <a href="search.php?pn=' . $previous . '"> Back</a> ';
}
$paginationDisplay .= '<span class="paginationNumbers">' . $centerPages . '</span>';
if ($pn != $lastPage) {
$nextPage = $pn + 1;
$paginationDisplay .= ' <a href="search.php?pn=' . $nextPage . '"> Next</a> ';
}
}
$num_results = mysql_num_rows($results);
if($num_results == 1) {
echo "Search has returned 1 result.";
}else{
echo "Search has returned $num_results results.";
}
echo "<tr><td></td><td>Name</td><td>Description</td><td>Sold</td><td>Price</td><td>Owner</td></tr>";
//while loop here
while($row8 = mysql_fetch_array($sql2)) {
$prodname = $row8['name'];
$prodid = $row8['id'];
$proddesc = $row8['description'];
$prodprice = $row8['price'];
$prodlogo = $row8['logo'];
$prodsold = $row8['number_sold'];
$userid = $row8['userid'];
$prodownerquery = mysql_fetch_assoc(mysql_query("select * from users where id='{$userid}'"));
$prodowner = $prodownerquery['username'];
echo "<tr><td><img src=images/productpics/$prodlogo></td><td><a href=viewproduct.php?pid=$prodid>$prodname</a></td><td width=200>$proddesc...</td><td>$prodsold</td><td>$$prodprice</td><td><a href=viewuser.php?pid=$userid>$prodowner</a></td></tr>";
}
}
}
}
}
echo "</table>";
echo $centerPages;
?>
<?php require('includes/footer.php') ?>
Thanks!
For God's sake, use a framework. I haven't had a look at your code much but just at how you make query to your database, I'm sure you will end up creating exploitable code via SQL Injections (your queries aren't decoupled so you need to sanitize everytime).
Also, frameworks have paging builtin.
Here's a list for PHP (I dev in Ruby and nodejs, so there might be more)