Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MySQL Query Page Numbering 1

Status
Not open for further replies.

WilliamMute

Programmer
Jan 4, 2006
117
Hi all,

Thanks for viewing this post.

I have a dilema. I have used a piece of code which perfectly worked a a previous coursework I did, now its not working on this one am working on even though, I literally copied a past the code.

The am is to number the returned query result, i.e only display 5 results per page and the display next page numbers.

Code:
<?php
$var = $_GET[id];
mysql_select_db($database_Connection, $Connection);
$table = "products";
$maxperpage = 5;


$sql = "Select count(*) as cnt from $table WHERE Category='$var'";

//query
$results = mysql_query($sql);

//how many rows 
$row = mysql_fetch_assoc($results);
$numrows = $row['cnt'];

//pages we going to use
$numpages = ceil($numrows/$maxperpage);

//start
$offset = isset($_GET['offset'])?$_GET['offset']:0;

//query
$sql = "Select * from $table where Category ='$var' order by product_name limit $maxperpage offset $offset";

$results = mysql_query($sql);

	
?>


THEN MY While Statement. and then...

Code:
<? if ($numpages > 1): ?>

<div class="pages">
<?
for ($i=0; $i<$numpages; $i++):
$class = (($offset/$maxperpage) === $i) ? "current":"noncurrent";
?>
<a class="<?=$class?>" href="<?=$_SERVER['PHP_SELF']?>?id=<?php echo $var;?>?offset=<?=$i*$maxperpage?>">&nbsp;<?=$i+1?></a>  
<?
endfor;
?>
</div>
<? endif; ?>


It does display the first 5 products and tells me the accurate amount of products that matches the search criterias, but when I click on the next 5 products, no results are returned.
 
this line is wrong
Code:
<a class="<?=$class?>" href="<?=$_SERVER['PHP_SELF']?>?id=<?php echo $var;?>?offset=<?=$i*$maxperpage?>">&nbsp;<?=$i+1?></a>

this sets up the pages so it is not right to have the $var in there too. you can't also have two queries in the same link.

try this instead
Code:
<a class="<?=$class?>" href="<?=$_SERVER['PHP_SELF']?>?offset=<?=$i*$maxperpage?>">&nbsp;<?=$i+1?></a>
 
Hi Justin,

Thanks for the reply.

I've just tried that but still the same problem. This is really weired.

 
can you post an example of the html source code the page produces?
 






<HTML>
<HEAD>
<TITLE>indexNewsubpages</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<link href="css/css.css" rel="stylesheet" type="text/css">


</HEAD>
<BODY BGCOLOR=#FFFFFF LEFTMARGIN=0 TOPMARGIN=0 MARGINWIDTH=0 MARGINHEIGHT=0>

<!-- ImageReady Slices (indexNewsubpages.psd) -->
<TABLE WIDTH=775 BORDER=0 align="center" CELLPADDING=0 CELLSPACING=0>
<TR>
<TD ROWSPAN=9>
<IMG SRC="images/products_01.jpg" WIDTH=18 HEIGHT=775 ALT=""></TD>
<TD COLSPAN=10>
<IMG SRC="images/products_02.jpg" WIDTH=600 HEIGHT=73 ALT=""></TD>
<TD>
<IMG SRC="images/products_03.jpg" WIDTH=137 HEIGHT=73 ALT=""></TD>
<TD ROWSPAN=9>

<IMG SRC="images/products_04.jpg" WIDTH=20 HEIGHT=775 ALT=""></TD>
</TR>
<TR>
<TD COLSPAN=11>
<IMG SRC="images/products_05.jpg" WIDTH=737 HEIGHT=176 ALT=""></TD>
</TR>
<TR>
<TD>
<IMG SRC="images/products_06.jpg" WIDTH=139 HEIGHT=64 ALT=""></TD>

<TD COLSPAN=4>
<IMG SRC="images/products_07.jpg" WIDTH=158 HEIGHT=64 ALT=""></TD>
<TD COLSPAN=2>
<IMG SRC="images/products_08.jpg" WIDTH=138 HEIGHT=64 ALT=""></TD>
<TD COLSPAN=3>
<IMG SRC="images/products_09.jpg" WIDTH=165 HEIGHT=64 ALT=""></TD>
<TD>
<IMG SRC="images/products_10.jpg" WIDTH=137 HEIGHT=64 ALT=""></TD>
</TR>

<TR>
<TD COLSPAN=11>
<IMG SRC="images/products_11.jpg" WIDTH=737 HEIGHT=39 ALT=""></TD>
</TR>
<TR>
<TD COLSPAN=2 valign="top">
<IMG SRC="images/products_12.jpg" WIDTH=214 HEIGHT=33 ALT="">
<IMG SRC="images/products_15.jpg" WIDTH=214 HEIGHT=27 ALT="">
<IMG SRC="images/products_16.jpg" WIDTH=214 HEIGHT=24 ALT="">
<IMG SRC="images/products_17.jpg" WIDTH=214 HEIGHT=24 ALT="">

<IMG SRC="images/products_18.jpg" WIDTH=214 HEIGHT=23 ALT="">
<IMG SRC="images/products_19.jpg" WIDTH=214 HEIGHT=24 ALT="">


&nbsp; </TD>
<TD ROWSPAN=3>
<IMG SRC="images/products_13.jpg" WIDTH=19 HEIGHT=345 ALT=""></TD>
<TD COLSPAN=8 ROWSPAN=3 valign="top">

<style>
.odd_row {background:url(products/mysqlbg1.jpg);}
.even_row {background:url(products/mysqlbg2.jpg);}
/*.odd_row {background:url(../images/mysql_bg2.jpg);} */
/*.odd_row {background-color:#00CCFF;} */
/*.even_row {background-color:#CCCCCC;} */
</style>
<table width="100%"> There are currently 0 Products that match your Search



<br>

<tr class="SubHeadingwithWhiteBg">
<td>







</td>
<td> <a href="javascript:history.go(-1);" title="go back to previous page">Previous Page</a> &nbsp; <strong> | </strong></td>
<td> <a href="#top">^top of page</a> </td>
</tr>
</table>








</TD>

</TR>
<TR>
<TD COLSPAN=2>
<IMG SRC="images/products_20.jpg" WIDTH=214 HEIGHT=109 ALT=""></TD>
</TR>
<TR>
<TD COLSPAN=2>
<IMG SRC="images/products_21.jpg" WIDTH=214 HEIGHT=81 ALT=""></TD>
</TR>

<TR>
<TD COLSPAN=4 ROWSPAN=2>
<IMG SRC="images/products_22.jpg" WIDTH=282 HEIGHT=78 ALT=""></TD>
<TD COLSPAN=2>
<IMG SRC="images/products_23.jpg" WIDTH=88 HEIGHT=37 ALT=""></TD>
<TD COLSPAN=2>
<IMG SRC="images/products_24.jpg" WIDTH=126 HEIGHT=37 ALT=""></TD>
<TD>
<IMG SRC="images/products_25.jpg" WIDTH=72 HEIGHT=37 ALT=""></TD>

<TD COLSPAN=2>
<IMG SRC="images/products_26.jpg" WIDTH=169 HEIGHT=37 ALT=""></TD>
</TR>
<TR>
<TD COLSPAN=7>
<IMG SRC="images/products_27.jpg" WIDTH=455 HEIGHT=41 ALT=""></TD>
</TR>
<TR>
<TD>

<IMG SRC="images/spacer.gif" WIDTH=18 HEIGHT=1 ALT=""></TD>
<TD>
<IMG SRC="images/spacer.gif" WIDTH=139 HEIGHT=1 ALT=""></TD>
<TD>
<IMG SRC="images/spacer.gif" WIDTH=75 HEIGHT=1 ALT=""></TD>
<TD>
<IMG SRC="images/spacer.gif" WIDTH=19 HEIGHT=1 ALT=""></TD>
<TD>
<IMG SRC="images/spacer.gif" WIDTH=49 HEIGHT=1 ALT=""></TD>

<TD>
<IMG SRC="images/spacer.gif" WIDTH=15 HEIGHT=1 ALT=""></TD>
<TD>
<IMG SRC="images/spacer.gif" WIDTH=73 HEIGHT=1 ALT=""></TD>
<TD>
<IMG SRC="images/spacer.gif" WIDTH=65 HEIGHT=1 ALT=""></TD>
<TD>
<IMG SRC="images/spacer.gif" WIDTH=61 HEIGHT=1 ALT=""></TD>
<TD>

<IMG SRC="images/spacer.gif" WIDTH=72 HEIGHT=1 ALT=""></TD>
<TD>
<IMG SRC="images/spacer.gif" WIDTH=32 HEIGHT=1 ALT=""></TD>
<TD>
<IMG SRC="images/spacer.gif" WIDTH=137 HEIGHT=1 ALT=""></TD>
<TD>
<IMG SRC="images/spacer.gif" WIDTH=20 HEIGHT=1 ALT=""></TD>
</TR>
</TABLE>

<!-- End ImageReady Slices -->
</BODY>
</HTML>
 
?? there are no page numbers or anything in the above code.

can i just be sure we're on the same page here? the code you posted above does not output anything other than the page numbers with links. it does not actually output the 5 results for that page.

it also does not output any page numbering if the number of pages is less than 2. so if only 5 results are returned by the count(*) query, you will not get any page numbers.
 
Sorry, I thouaght you wanted the output of the 'second' page. Here is the code. Thanks

Code:
<HTML>
<HEAD>
<TITLE>indexNewsubpages</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<link href="css/css.css" rel="stylesheet" type="text/css">


</HEAD>
<BODY BGCOLOR=#FFFFFF LEFTMARGIN=0 TOPMARGIN=0 MARGINWIDTH=0 MARGINHEIGHT=0>

<!-- ImageReady Slices (indexNewsubpages.psd) -->
<TABLE WIDTH=775 BORDER=0 align="center" CELLPADDING=0 CELLSPACING=0>
	<TR>
		<TD ROWSPAN=9>
			<IMG SRC="images/products_01.jpg" WIDTH=18 HEIGHT=775 ALT=""></TD>
		<TD COLSPAN=10>
			<IMG SRC="images/products_02.jpg" WIDTH=600 HEIGHT=73 ALT=""></TD>
		<TD>
			<IMG SRC="images/products_03.jpg" WIDTH=137 HEIGHT=73 ALT=""></TD>
		<TD ROWSPAN=9>

			<IMG SRC="images/products_04.jpg" WIDTH=20 HEIGHT=775 ALT=""></TD>
	</TR>
	<TR>
		<TD COLSPAN=11>
			<IMG SRC="images/products_05.jpg" WIDTH=737 HEIGHT=176 ALT=""></TD>
	</TR>
	<TR>
		<TD>
			<IMG SRC="images/products_06.jpg" WIDTH=139 HEIGHT=64 ALT=""></TD>

		<TD COLSPAN=4>
			<IMG SRC="images/products_07.jpg" WIDTH=158 HEIGHT=64 ALT=""></TD>
		<TD COLSPAN=2>
			<IMG SRC="images/products_08.jpg" WIDTH=138 HEIGHT=64 ALT=""></TD>
		<TD COLSPAN=3>
			<IMG SRC="images/products_09.jpg" WIDTH=165 HEIGHT=64 ALT=""></TD>
		<TD>
			<IMG SRC="images/products_10.jpg" WIDTH=137 HEIGHT=64 ALT=""></TD>
	</TR>

	<TR>
		<TD COLSPAN=11>
			<IMG SRC="images/products_11.jpg" WIDTH=737 HEIGHT=39 ALT=""></TD>
	</TR>
	<TR>
		<TD COLSPAN=2 valign="top">
		<IMG SRC="images/products_12.jpg" WIDTH=214 HEIGHT=33 ALT="">
<IMG SRC="images/products_15.jpg" WIDTH=214 HEIGHT=27 ALT="">
<IMG SRC="images/products_16.jpg" WIDTH=214 HEIGHT=24 ALT="">
<IMG SRC="images/products_17.jpg" WIDTH=214 HEIGHT=24 ALT="">

<IMG SRC="images/products_18.jpg" WIDTH=214 HEIGHT=23 ALT="">
<IMG SRC="images/products_19.jpg" WIDTH=214 HEIGHT=24 ALT="">
		
		
		&nbsp;																		</TD>
		<TD ROWSPAN=3>
			<IMG SRC="images/products_13.jpg" WIDTH=19 HEIGHT=345 ALT=""></TD>
		<TD COLSPAN=8 ROWSPAN=3 valign="top">
		  
			  <style>
.odd_row {background:url(products/mysqlbg1.jpg);}
.even_row {background:url(products/mysqlbg2.jpg);}
/*.odd_row {background:url(../images/mysql_bg2.jpg);} */
/*.odd_row {background-color:#00CCFF;} */
/*.even_row {background-color:#CCCCCC;} */
</style>	
		  <table width="100%"> There are currently 9 Products that match your Search
			  
			  	    

<tr>

              <td class="SubHeadings" colspan="3"><a href="[URL unfurl="true"]http://www.ylabel/pages/products.php?id=11">B-LUIGI</a></td>[/URL]
            </tr>


<tr class="odd_row"><p>
     <td width="173" align="center" class="LoopRowsQuer"><p>
	 <a href="[URL unfurl="true"]http://www.ylabel.co.uk/pages/single_item.php?id=11"><img[/URL] src="products/B-LUIGI.jpg" alt="Gift Ideas" width="62" height="80" border="0" align="absbottom" class="RightTablesImges"></a></p></td>
	 
	   <td width="173" align="center" class="LoopRowsQuer"><P>B-LUIGI</a></p></td>
	   
	     <td width="173" align="center" class="LoopRowsQuer"><p>£39.99</a></p>

		 </td>
		
	</tr>
	<tr>
	 <td height="10">&nbsp; </td>
	 </tr>

	
  

<tr>
              <td class="SubHeadings" colspan="3"><a href="[URL unfurl="true"]http://www.ylabel/pages/products.php?id=13">KATI[/URL] BROWN</a></td>

            </tr>


<tr class="even_row"><p>
     <td width="173" align="center" class="LoopRowsQuer"><p>
	 <a href="[URL unfurl="true"]http://www.ylabel.co.uk/pages/single_item.php?id=13"><img[/URL] src="products/KATI BROWN.jpg" alt="Gift Ideas" width="62" height="80" border="0" align="absbottom" class="RightTablesImges"></a></p></td>
	 
	   <td width="173" align="center" class="LoopRowsQuer"><P>KATI BROWN</a></p></td>
	   
	     <td width="173" align="center" class="LoopRowsQuer"><p>£34.99</a></p>
		 </td>

		
	</tr>
	<tr>
	 <td height="10">&nbsp; </td>
	 </tr>

	
  

<tr>
              <td class="SubHeadings" colspan="3"><a href="[URL unfurl="true"]http://www.ylabel/pages/products.php?id=14">KATI-BLACK</a></td>[/URL]
            </tr>


<tr class="odd_row"><p>
     <td width="173" align="center" class="LoopRowsQuer"><p>
	 <a href="[URL unfurl="true"]http://www.ylabel.co.uk/pages/single_item.php?id=14"><img[/URL] src="products/KATI-BLACK.jpg" alt="Gift Ideas" width="62" height="80" border="0" align="absbottom" class="RightTablesImges"></a></p></td>
	 
	   <td width="173" align="center" class="LoopRowsQuer"><P>KATI-BLACK</a></p></td>
	   
	     <td width="173" align="center" class="LoopRowsQuer"><p>£34.99</a></p>
		 </td>
		
	</tr>

	<tr>
	 <td height="10">&nbsp; </td>
	 </tr>

	
  

<tr>
              <td class="SubHeadings" colspan="3"><a href="[URL unfurl="true"]http://www.ylabel/pages/products.php?id=12">LUIGI</a></td>[/URL]
            </tr>


<tr class="even_row"><p>
     <td width="173" align="center" class="LoopRowsQuer"><p>
	 <a href="[URL unfurl="true"]http://www.ylabel.co.uk/pages/single_item.php?id=12"><img[/URL] src="products/LUIGI.jpg" alt="Gift Ideas" width="62" height="80" border="0" align="absbottom" class="RightTablesImges"></a></p></td>
	 
	   <td width="173" align="center" class="LoopRowsQuer"><P>LUIGI</a></p></td>
	   
	     <td width="173" align="center" class="LoopRowsQuer"><p>£39.99</a></p>
		 </td>
		
	</tr>
	<tr>

	 <td height="10">&nbsp; </td>
	 </tr>

	
  

<tr>
              <td class="SubHeadings" colspan="3"><a href="[URL unfurl="true"]http://www.ylabel/pages/products.php?id=15">MARY-ANN</a></td>[/URL]
            </tr>


<tr class="odd_row"><p>
     <td width="173" align="center" class="LoopRowsQuer"><p>

	 <a href="[URL unfurl="true"]http://www.ylabel.co.uk/pages/single_item.php?id=15"><img[/URL] src="products/MARY-ANN.jpg" alt="Gift Ideas" width="62" height="80" border="0" align="absbottom" class="RightTablesImges"></a></p></td>
	 
	   <td width="173" align="center" class="LoopRowsQuer"><P>MARY-ANN</a></p></td>
	   
	     <td width="173" align="center" class="LoopRowsQuer"><p>£39.99</a></p>
		 </td>
		
	</tr>
	<tr>
	 <td height="10">&nbsp; </td>

	 </tr>

	
   

<br>
<tr class="SubHeadingwithWhiteBg">
<td> 



<div class="pages">

<a class="current" href="/pages/products.php?offset=0">&nbsp;1</a>


<a class="noncurrent" href="/pages/products.php?offset=5">&nbsp;2</a>

</div>





</td>
<td> <a href="javascript:history.go(-1);" title="go back to previous page">Previous Page</a> &nbsp; <strong> | </strong></td>
<td>  <a href="#top">^top of page</a>     </td>

</tr>
 </table>			
		
		
		
		
		
		
		
		
	  </TD>
	</TR>
	<TR>
		<TD COLSPAN=2>
			<IMG SRC="images/products_20.jpg" WIDTH=214 HEIGHT=109 ALT=""></TD>
	</TR>
	<TR>
		<TD COLSPAN=2>

			<IMG SRC="images/products_21.jpg" WIDTH=214 HEIGHT=81 ALT=""></TD>
	</TR>
	<TR>
		<TD COLSPAN=4 ROWSPAN=2>
			<IMG SRC="images/products_22.jpg" WIDTH=282 HEIGHT=78 ALT=""></TD>
		<TD COLSPAN=2>
			<IMG SRC="images/products_23.jpg" WIDTH=88 HEIGHT=37 ALT=""></TD>
		<TD COLSPAN=2>
			<IMG SRC="images/products_24.jpg" WIDTH=126 HEIGHT=37 ALT=""></TD>

		<TD>
			<IMG SRC="images/products_25.jpg" WIDTH=72 HEIGHT=37 ALT=""></TD>
		<TD COLSPAN=2>
			<IMG SRC="images/products_26.jpg" WIDTH=169 HEIGHT=37 ALT=""></TD>
	</TR>
	<TR>
		<TD COLSPAN=7>
			<IMG SRC="images/products_27.jpg" WIDTH=455 HEIGHT=41 ALT=""></TD>
	</TR>

	<TR>
		<TD>
			<IMG SRC="images/spacer.gif" WIDTH=18 HEIGHT=1 ALT=""></TD>
		<TD>
			<IMG SRC="images/spacer.gif" WIDTH=139 HEIGHT=1 ALT=""></TD>
		<TD>
			<IMG SRC="images/spacer.gif" WIDTH=75 HEIGHT=1 ALT=""></TD>
		<TD>
			<IMG SRC="images/spacer.gif" WIDTH=19 HEIGHT=1 ALT=""></TD>

		<TD>
			<IMG SRC="images/spacer.gif" WIDTH=49 HEIGHT=1 ALT=""></TD>
		<TD>
			<IMG SRC="images/spacer.gif" WIDTH=15 HEIGHT=1 ALT=""></TD>
		<TD>
			<IMG SRC="images/spacer.gif" WIDTH=73 HEIGHT=1 ALT=""></TD>
		<TD>
			<IMG SRC="images/spacer.gif" WIDTH=65 HEIGHT=1 ALT=""></TD>
		<TD>

			<IMG SRC="images/spacer.gif" WIDTH=61 HEIGHT=1 ALT=""></TD>
		<TD>
			<IMG SRC="images/spacer.gif" WIDTH=72 HEIGHT=1 ALT=""></TD>
		<TD>
			<IMG SRC="images/spacer.gif" WIDTH=32 HEIGHT=1 ALT=""></TD>
		<TD>
			<IMG SRC="images/spacer.gif" WIDTH=137 HEIGHT=1 ALT=""></TD>
		<TD>
			<IMG SRC="images/spacer.gif" WIDTH=20 HEIGHT=1 ALT=""></TD>

	</TR>
</TABLE>
<!-- End ImageReady Slices -->
</BODY>
</HTML>
 
my bad.

change this line
Code:
<a class="<?=$class?>" href="<?=$_SERVER['PHP_SELF']?>?offset=<?=$i*$maxperpage?>">&nbsp;<?=$i+1?></a>
to
Code:
<a class="<?=$class?>" href="<?=$_SERVER['PHP_SELF']?>?[red]id=<?=$var?>&[/red]offset=<?=$i*$maxperpage?>">&nbsp;<?=$i+1?></a>
 
i'd add that you should also escape your variables before using them in the sql query

Code:
$var = mysql_escape_string($_GET['id']);
$offset = mysql_escape_string($_GET['offset']);

otherwise you are at risk of sql injection attacks
 
Thank you once more Justin works perfect. About the excaping thing, never heard about it, am gonna read up on it now any article about it you know?

Thanks a million again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top