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

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 »

that is the query getting executed on first page.. Try to go to another page number[2 or 3] and see what the query shows.
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 »

SELECT * FROM products WHERE title LIKE '%%' AND category = '' LIMIT 6, 6
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 »

See, post data not being passed.
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 »

Yep, I can see.
But why does it work for this code, which is an exact same code, but with just ONE variable used:

This is my problem. I shouldn't have to change much when it works here (and on another web site I did using this method. I just want to use more than one variable for the search criteria.

Code: Select all

<?php 
$search = $_POST['search'];
include "dbconn.php";
echo "<div class='head'>Search for $search</div>";
 
$rowsPerPage = 20;
 
// 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 '%$search%' OR description LIKE '%$search%' OR id = '$search' LIMIT $offset, $rowsPerPage") or die (mysql_error());
 
if (mysql_num_rows($result)==0) { echo "<tr><td><div class='sectionhead'>Sorry, there are no results for $search.</div></td></tr>"; }
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 ...</div>£$row->price<br/>";
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>
                    
            ";
            }}          
    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 '%$search%' OR description LIKE '%$search%' OR id = '$search'";
    
$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=search&menu=search&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=search&menu=search&pagenum=$page\" class='bodylink'>[Prev]</a> ";
 
   $first = " <a href=\"index.php?page=search&menu=search&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=search&menu=search&pagenum=$page\" class='bodylink'>[Next]</a>";
 
   $last = " <a href=\"index.php?page=search&menu=search&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);
?>
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 »

It is working because the query gives results even if you give nothing because you have put a like % % term. Giving a % % means the results do appear but are valid only in the first page. Further pages will query something like this
[sql] SELECT * FROM products WHERE title LIKE '%%' AND category = 'something'//This IS equal TO SELECT * FROM products WHERE category = 'something' [/sql]
Try to apply the same query for your mysql and see what shows up.
I have modified your code and implemented the same through sessions.. Hope that will help you solve your problem.

Code: Select all

 
<?php
session_start();
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'>";
$query="SELECT * FROM products WHERE title LIKE '%$searchadv%' AND category = '$category' LIMIT $offset, $rowsPerPage";
$result = mysql_query ("SELECT * FROM products WHERE title LIKE '%$searchadv%' AND category = '$category' LIMIT $offset, $rowsPerPage") or die (mysql_error());
echo $query; 
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 ...</div>
            <a href='index.php?page=product&menu=categ&category=$row->category&product=$row->id' title='Look at the $row->title'><img src='images/productphotos/$row->photoprimary' border='0' /></a></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'";
   
$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);
?>
 
 
Note that only the initial parts of the file are modified and the rest is kept the same.
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 »

Ok - so you put the variables into a session so they can be used on the other pages - gotchya.
Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\xampp\phpmyadmin\test\index.php:1) in C:\xampp\phpmyadmin\test\includes\searchadvresults.inc on line 2

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\phpmyadmin\test\index.php:1) in C:\xampp\phpmyadmin\test\includes\searchadvresults.inc on line 2
I get these errors. And I don't have a clue what they mean as I have never used sessions before.
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 »

Two things:
1. If u are including another file before this code, place the session_start() at the start of the file and then add the include statements.
2. put this statement

Code: Select all

 
if(!isset($_SESSION))
{
session_start();
}
 
instead of

Code: Select all

 
session_start();
 
3. Dont leave any spaces at the start of this file or at the end of the included file. session_start() function needs to be called before any output is sent to the browser.
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 »

Mmmm I get the same error.
This is my URL.
index.php?page=searchadvresults&menu=home

And this is the start of searchadvresults.inc.

Code: Select all

<?php
if(!isset($_SESSION))
{
session_start();
}
 
if(isset($_POST['searchadv']))
{
    $searchadv = $_POST['searchadv'];
    $_SESSION['searchadv']=$searchadv;
}
else
{
    $searchadv = $_SESSION['searchadv'];
}............
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 »

Output started in index.php.
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 »

I'm sorry, what do you mean?
Should this code be placed in index.php?

Please don't confused me - I'm confused enough!
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 »

so you are writing this code in the file searchadvresults.inc and including it in index.php??
if not.. whats the point in creating next and previous pages pointing to index.php??

If you are including searchadvresults.inc file in index.php, put the session_start() statement at the start of index.php and not at the start of searchadvresults.inc file.

Basically the session_start() function should be called before any output is sent to the browser.
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 »

Sorry, that now produces the same error, but on every page of the web site.
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 »

can you please post the error and the first few lines of index.php
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 »

It's the EXACT SAME error as the one I reported before.
I used that code right at the top of the <?php section of the page.
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 »

simonmlewis wrote: I used that code right at the top of the <?php section of the page.
is there any html code before that??
the function needs to be called at the start of the file.
goes like this

Code: Select all

 
//start of index.php
<?php session_start(); ?>
... //rest of the code of index.php
 
Post Reply