Page Numbering Query - page one ok, page 2,3..not ok.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Page Numbering Query - page one ok, page 2,3..not ok.

Post by simonmlewis »

YES - that works with no error. But if I enter a price range now of 14.00 to 15.00 (via dropdown menus with no £), then I get no results at all, but '1' page.

Here's the code:

Code: Select all

<?php
if(isset($_POST['searchadv']))
{
    $searchadv = $_POST['searchadv'];
    $_SESSION['searchadv']=$searchadv;
}
else
{
    $searchadv = $_SESSION['searchadv'];
}
if(isset($_POST['category']))
{
    $category = $_POST['category'];
    $_SESSION['category']=$category;
}
else
{
    $category = $_SESSION['category'];
}
if(isset($_POST['pricemin']))
{
    $pricemin = $_POST['pricemin'];
    $_SESSION['pricemin']=$pricemin;
}
else
{
    $pricemin = $_SESSION['pricemin'];
}
if(isset($_POST['pricemax']))
{
    $pricemax = $_POST['pricemax'];
    $_SESSION['pricemax']=$pricemax;
}
else
{
    $pricemax = $_SESSION['pricemax'];
}
include "dbconn.php";
echo "<div class='head'>Advanced Searching</div>";
 
$rowsPerPage = 5;
 
// by default we show first page
$pageNum = 1;
 
// if $_GET['pagenum'] defined, use it as page number
if(isset($_GET['pagenum']))
{
    $pageNum = $_GET['pagenum'];
}
 
// counting the offset
$offset = ($pageNum - 1) * $rowsPerPage;
   
echo "<table width='100%' cellpadding='0' cellspacing='0' class='table'>";
$result = mysql_query ("SELECT * FROM products WHERE title LIKE '%$searchadv%' AND category = '$category' AND (price BETWEEN '$pricemin' AND '$pricemax') LIMIT $offset, $rowsPerPage") or die (mysql_error());
if (mysql_num_rows($result)==0) { echo "<div class='sectionhead'>Sorry, there are no results for $search.</div>"; }
else {
 
while ($row = mysql_fetch_object($result))
      while ($row = mysql_fetch_object($result)) {
    echo "
<div class='cat_prodlistbox'>
<div class='cat_producttitle'>";
 
                    $position=43; //Defines how many characters will be displayed from content field.
$postcontent = substr($row->title,0,$position);
echo "$postcontent ...<br/>$row->price</div>";
 
if ($row->photoprimary == NULL) { echo "<a href='index.php?page=product&menu=categ&category=$row->category&product=$row->id&head=$row->title' title='Look at the $row->title'><img src='images/blank.gif' border='0' /></a>";}
            elseif ($row->photoprimary != NULL) { echo"
            <a href='index.php?page=product&menu=categ&category=$row->category&product=$row->id&head=$row->title' title='Look at the $row->title'><img src='images/productphotos/$row->photoprimary' border='0' /></a>";}
            
            echo "</div></div>";
            }}        
    mysql_free_result($result);
    echo "</table>";
           
    echo "<div style='clear:both' /><hr noshade size='1' color='#cccccc' />";
    $query   = "SELECT COUNT(id) AS numrows FROM products WHERE title LIKE '%$searchadv%' AND category = '$category' AND (price BETWEEN '$pricemin' AND '$pricemax')";
   
$result  = mysql_query($query) or die(mysql_error());
$row     = mysql_fetch_array($result, MYSQL_ASSOC);
$numrows = $row['numrows'];
    // how many pages we have when using paging?
$maxPage = ceil($numrows/$rowsPerPage);
 
// print the link to access each page
$self = $_SERVER['PHP_SELF'];
$nav  = '';
 
for($page = 1; $page <= $maxPage; $page++)
{
   if ($page == $pageNum)
   {
      $nav .= " $page "; // no need to create a link to current page
   }
   else
   {
      $nav .= " <a href=\"index.php?page=searchadvresults&menu=home&pagenum=$page\" class='bodylink'>$page</a>";
   }
}
 
// creating previous and next link
// plus the link to go straight to
// the first and last page
 
if ($pageNum > 1)
{
   $page  = $pageNum - 1;
   $prev  = " <a href=\"index.php?page=searchadvresults&menu=home&pagenum=$page\" class='bodylink'>[Prev]</a> ";
 
   $first = " <a href=\"index.php?page=searchadvresults&menu=home&pagenum=1\" class='bodylink'>[First Page]</a>";
}
else
{
   $prev  = '&nbsp;'; // we're on page one, don't print previous link
   $first = '&nbsp;'; // nor the first page link
}
 
if ($pageNum < $maxPage)
{
   $page = $pageNum + 1;
   $next = " <a href=\"index.php?page=searchadvresults&menu=home&pagenum=$page\" class='bodylink'>[Next]</a>";
 
   $last = " <a href=\"index.php?page=searchadvresults&menu=home&pagenum=$maxPage\" class='bodylink'>[Last Page]</a>";
}
else
{
   $next = '&nbsp;'; // we're on the last page, don't print next link
   $last = '&nbsp;'; // nor the last page link
}
 
// print the navigation link
echo "<div class='navpages'>" . $first . $prev . $nav . $next . $last . "</div>";
    mysql_close($sqlconn);
?>
Last edited by John Cartwright on Mon Aug 24, 2009 11:34 am, edited 2 times in total.
Reason: Please use appropriate [code=php][/code] tags
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Page Numbering Query - page one ok, page 2,3..not ok.

Post by jackpf »

I don't see session_start() anywhere.

This would be so much easier using GET variables...honestly. You're just trying to avoid rewriting it by "hacking" it. That's not the "proper" way.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Page Numbering Query - page one ok, page 2,3..not ok.

Post by simonmlewis »

Errr read back a bit - I was told to put it in index.php.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Page Numbering Query - page one ok, page 2,3..not ok.

Post by jackpf »

Oh yeah, my bad.

What does

Code: Select all

var_dump($_SESSION);
show?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Page Numbering Query - page one ok, page 2,3..not ok.

Post by simonmlewis »

array(4) { ["searchadv"]=> string(4) "boat" ["category"]=> string(8) "RC Boats" ["pricemin"]=> string(5) "14.00" ["pricemax"]=> string(5) "20.00" }
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Page Numbering Query - page one ok, page 2,3..not ok.

Post by jackpf »

Oh right...then your sessions variables are being set.

I'm not entirely sure what the problem is tbh :?

I think you just need to go through each variable, make sure it's what you expect it is, and so on...
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Page Numbering Query - page one ok, page 2,3..not ok.

Post by simonmlewis »

The variables are correct. Just pricemin and pricemax.

It's very peculiar indeed.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: Page Numbering Query - page one ok, page 2,3..not ok.

Post by susrisha »

i think its the error in the mysql statament and not in the code. Try executing the same query over mysql and see what you get. And i dont know if its possible to do a BETWEEN for two varchars. The query shows that pricemin and pricemax are taken as strings instead of numbers or floats.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Page Numbering Query - page one ok, page 2,3..not ok.

Post by simonmlewis »

Ahhh u may have something there.

Maybe they should be DOUBLES....?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Page Numbering Query - page one ok, page 2,3..not ok.

Post by simonmlewis »

Hey, it works when they are DOUBLEs.

Brilliant. So many factors came into place with this problem.

Thanks to all who donated their time.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: Page Numbering Query - page one ok, page 2,3..not ok.

Post by susrisha »

:drunk: :drunk: finally..
Post Reply