Reply
Results per page not working....
Old 04-17-2007, 07:04 PM Results per page not working....
Average Talker

Posts: 23
This code is to pull out products of a certain type out of a product database based on category. It then figures out how many results there were and splits them up into 3 per page and calculates the number of pages. Then, it displays the products and the page links.

When I go to http://www.nancygoldmanart.com/iteml...pe=watercolors

All 4 products with type set to watercolor are displayed on the page, when the code says to display only 3. Can anyone see a problem in this code? Thanks for the help! I left out the connection info for security....but it is there for the actual code.
PHP Code:
<?php
$type 
$_GET['type'];
    
    
// Create some variables
    
$pageNumber = (isset($_GET["page"]) && is_numeric($_GET["page"])) ? $_GET["page"] : 1;
    
    
// Establish number of results per page
    
$perPage 3;
    
    
// Establish a padding value
    
$padding 5;
    
    
// Get Start index of results
    
$startIndex = ($pageNumber $perPage) - $perPage;
    
    
// Get total number of database entries
    
$query "SELECT * FROM paintinginfo WHERE type='$type'";
    
$result mysql_query($query);
    
$numRows mysql_num_rows($result);
    
// Show Page Listings
    
print "<div align=\"center\">";
        
// Get our total number of pages
        
$numOfPages ceil($numRows $perPage);
        
            
// Get page results
    
$rs mysql_query("SELECT * FROM paintinginfo WHERE type='$type' LIMIT $startIndex, $perPage");
    print 
"<div class=\"futura9999ff\">";
    
// Do we have results?
    
if(mysql_num_rows($rs) > 0) {
        
// Show the results
        
while($row mysql_fetch_array($result)) {
            print 
"<table width=\"95%\" border='1'>";
            print 
"<tr>";
            print 
"<td align=\"center\" width=\"120px\">";
            print 
"<a href=\"itemdetails.php\"><img border='0' src=\"http://nancygoldmanart.com/";
            print 
$row['thumbnail'];
            print 
"\"";
            print 
"/></a>";
            print 
"</td>";
            print 
"<td align=\"center\" width=\55%>";
            print 
"<a href=\"itemdetails.php\">".$row['title']."</a>";
            print 
"</td>";
            print 
"<td align=\"center\" width=\"20%\">";
            print 
"$";
            print 
$row['price'];
            print 
"</td>";            
            print 
"</tr>";
            print 
"</table";
        }
    } else {
        print 
"Sorry, no results found.";
    }
        
if(
$numRows 0)
{                                                            
        
// Print link to first page
        
print "<a id=\"container\" href=\"itemlisting.php?page=1&type=$type\">FIRST</a> ";
        
        
// If our current page, minus our padding, is greater than 1
        
if(($pageNumber $padding) > 1) {
            print 
"... ";
            
// Set our lower limit
            
$lowerLimit $pageNumber $padding;
            
// Print all padded numbers between lowerLimit and current page
            
for($i $lowerLimit$i $pageNumber$i++) {
                print 
"<a id=\"container\" href=\"itemlisting.php?page=".$i."&type=$type\">".$i."</a> ";
            }
        } else {
            
// Print all numbers between current page, and first page
            
for($i 2$i $pageNumber$i++) {
                print 
"<a id=\"container\" href=\"itemlisting.php?page=".$i."&type=$type\">".$i."</a> ";
            }
        }
        
        
// If we're not on the first page, or the last page, print current page
        
if(($pageNumber != 0) && ($pageNumber != 1) && ($pageNumber != $numOfPages)) {
            print 
"<b> - " $pageNumber " - </b>";
        }
        
        
// If our current page, plus our padding, is less than the total number of pages
        
if(($pageNumber $padding) < $numOfPages) {
            
// Set upper limit
            
$upperLimit $pageNumber $padding;
            
// Print all number from padded pages above current page
            
for($i = ($pageNumber 1); $i <= $upperLimit$i++) {
                print 
"<a id=\"container\" href=\"itemlisting.php?page=".$i."&type=$type\">".$i."</a> ";
            }
            print 
"... ";
        } else {
            
// Print all page numbers between number of pages and current page
            
for($i = ($pageNumber 1); $i $numOfPages$i++) {
                print 
"<a id=\"container\" href=\"itemlisting.php?page=".$i."&type=$type\">".$i."</a> ";
            }
        }
        
        
// Print link to last page
        
print "<a id=\"container\" href=\"itemlisting.php?page=".$numOfPages."&type=$type\">LAST</a>";
}
    print 
"</div>";

    
// Close our database connection
    
mysql_close($connection);

?>
mattgoldman is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
     
Old 04-17-2007, 07:20 PM Re: Results per page not working....
mgraphic's Avatar
Truth Seeker

Posts: 2,305
Name: Keith Marshall
Location: West Hartford, CT
I've posted this before, but I have a pagination script example:

http://www.webmaster-talk.com/blog/2...sql-pagination
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 04-17-2007, 07:27 PM Re: Results per page not working....
Average Talker

Posts: 25
Okay, you're while loop where you're outputting all the information is using the variable $result which holds a resource to the query: "SELECT * FROM paintinginfo WHERE type='$type'", you should have used the variable $rs, which has the limit section to the query so it should be:
Code:
while($row = mysql_fetch_array($rs))
Also, use mysql_escape_quotes to prevent SQL injections. It's not that hard because you're using $_GET[] to provide the 'type'.
Code:
$type = mysql_escape_quotes($_GET['type']);
ryogo is offline
Reply With Quote
View Public Profile
 
Old 04-17-2007, 07:45 PM Re: Results per page not working....
Average Talker

Posts: 23
mgraphic: I already used a pagination script and modified it for this site and it was working until I added the $_GET['type'] part.

ryogo: I changed both of those but 4 results are still being displayed. Any other ideas?
mattgoldman is offline
Reply With Quote
View Public Profile
 
Old 04-17-2007, 08:13 PM Re: Results per page not working....
Average Talker

Posts: 23
now the page is all scrambled around, any explanations?

http://www.nancygoldmanart.com/itemlisting.php?type=watercolors


and I am getting an error that says:

Fatal error: Call to undefined function mysql_escape_quotes() in /home/mhgegol3/public_html/nancygoldmanart/Code/itemlisting.inc on line 9


Here is my current code:
PHP Code:
<?php
$type 
mysql_escape_quotes($_GET['type']);
//Include login information
include '********';

//Establish connection and select database
$connection mysql_connect($host,$user,$password);
$db mysql_select_db($database,$connection);
    
     
// Create some variables
    
$pageNumber = (isset($_GET["page"]) && is_numeric($_GET["page"])) ? $_GET["page"] : 1;
    
    
// Establish number of results per page
    
$perPage 3;
    
    
// Establish a padding value
    
$padding 5;
    
    
// Get Start index of results
    
$startIndex = ($pageNumber $perPage) - $perPage;
    
    
// Get total number of database entries
    
$query "SELECT * FROM paintinginfo WHERE type='$type'";
    
$result mysql_query($query);
    
$numRows mysql_num_rows($result);
     
// Get our total number of pages
        
$numOfPages ceil($numRows $perPage);        
            
// Get page results
    
$rs mysql_query("SELECT * FROM paintinginfo WHERE type='$type' LIMIT $startIndex, $perPage");
    
// Do we have results?
    
if(mysql_num_rows($rs) > 0) {
        
// Show the results
        
while($row mysql_fetch_array($rs)) {
            print 
"<table width=\"95%\" border='1'>";
            print 
"<tr>";
            print 
"<td align=\"center\" width=\"120px\">";
            print 
"<a href=\"itemdetails.php\"><img border='0' src=\"http://nancygoldmanart.com/";
            print 
$row['thumbnail'];
            print 
"\"";
            print 
"/></a>";
            print 
"</td>";
            print 
"<td align=\"center\" width=\55%>";
            print 
"<a href=\"itemdetails.php\">".$row['title']."</a>";
            print 
"</td>";
            print 
"<td align=\"center\" width=\"20%\">";
            print 
"$";
            print 
$row['price'];
            print 
"</td>";            
            print 
"</tr>";
            print 
"</table";
        }
    } else {
        print 
"Sorry, no results found.";
    }
            
// Show Page Listings
if($numRows 0)
{                                                            
        
// Print link to first page
        
print "<a id=\"container\" href=\"itemlisting.php?page=1&type=$type\">FIRST</a> ";
        
        
// If our current page, minus our padding, is greater than 1
        
if(($pageNumber $padding) > 1) {
            print 
"... ";
            
// Set our lower limit
            
$lowerLimit $pageNumber $padding;
            
// Print all padded numbers between lowerLimit and current page
            
for($i $lowerLimit$i $pageNumber$i++) {
                print 
"<a id=\"container\" href=\"itemlisting.php?page=".$i."&type=$type\">".$i."</a> ";
            }
        } else {
            
// Print all numbers between current page, and first page
            
for($i 2$i $pageNumber$i++) {
                print 
"<a id=\"container\" href=\"itemlisting.php?page=".$i."&type=$type\">".$i."</a> ";
            }
        }
        
        
// If we're not on the first page, or the last page, print current page
        
if(($pageNumber != 0) && ($pageNumber != 1) && ($pageNumber != $numOfPages)) {
            print 
"<b> - " $pageNumber " - </b>";
        }
        
        
// If our current page, plus our padding, is less than the total number of pages
        
if(($pageNumber $padding) < $numOfPages) {
            
// Set upper limit
            
$upperLimit $pageNumber $padding;
            
// Print all number from padded pages above current page
            
for($i = ($pageNumber 1); $i <= $upperLimit$i++) {
                print 
"<a id=\"container\" href=\"itemlisting.php?page=".$i."&type=$type\">".$i."</a> ";
            }
            print 
"... ";
        } else {
            
// Print all page numbers between number of pages and current page
            
for($i = ($pageNumber 1); $i $numOfPages$i++) {
                print 
"<a id=\"container\" href=\"itemlisting.php?page=".$i."&type=$type\">".$i."</a> ";
            }
        }
        
        
// Print link to last page
        
print "<a id=\"container\" href=\"itemlisting.php?page=".$numOfPages."&type=$type\">LAST</a>";
}

    
// Close our database connection

    
mysql_close($connection);

?>
mattgoldman is offline
Reply With Quote
View Public Profile
 
Old 04-18-2007, 04:20 AM Re: Results per page not working....
feraira's Avatar
BeTheBand!

Posts: 313
That error means that the function doesn't exist.

Change
$type = mysql_escape_quotes($_GET['type']);

to

$type = $_GET['type'];

then...

echo "$type";

just below it this way you will find out if the data is being picked up.
__________________
www.alldrumtabs.com

Last edited by feraira : 04-18-2007 at 04:23 AM.
feraira is offline
Reply With Quote
View Public Profile
 
Old 04-18-2007, 04:27 AM Re: Results per page not working....
jito's Avatar
L

Posts: 550
Name: surajit ray
Location: inside the heart of my friends
use $type=addslashes($_GET['type']); instead of
Code:
$type = mysql_escape_quotes($_GET['type']);
And you will get every records each time because of

while($row = mysql_fetch_array($result)) {

you suppose to pass the resource $rs like

while($row = mysql_fetch_array($rs)) {



__________________
Take care about what you think. Thoughts can travel far - Swami Vivekananda

Last edited by jito : 04-18-2007 at 04:53 AM. Reason: addition
jito is offline
Reply With Quote
View Public Profile
 
Old 04-18-2007, 10:01 AM Re: Results per page not working....
mgraphic's Avatar
Truth Seeker

Posts: 2,305
Name: Keith Marshall
Location: West Hartford, CT
There IS a function mysql_real_escape_string()
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Results per page not working....
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.17541 seconds with 13 queries