Prev and Next Buttons not functioning mysql data SOLVED

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

Post Reply
joeyoung25
Forum Newbie
Posts: 7
Joined: Sat Mar 03, 2012 4:05 am

Prev and Next Buttons not functioning mysql data SOLVED

Post by joeyoung25 »

First of all thanks to everyone for taking the time if you are reading this. I have some code that displays 3 rows from mysql database and then below it are the prev and next buttons and they work and it shows the proper number of pages that are needed for all the rows in the table but when you click on the nav buttons they dont actually do anything. the page reloads and the URL seems correct but the results shown on the screen never actually change. You can look at my page at http://theproofstation.com/fishing if you need to see what Im trying to accomplish. I have to give the credit to himerus at daniweb.com for the code as I have edited only about 5% and it would be awesome if only it worked.
Thanks

Code: Select all

<?							
$server = "localhost";
$user = "root";
$pass = "";
$databasename = "proofstation";
$db = mysql_connect($server, $user, $pass);
	mysql_select_db($databasename,$db);

		$sql = "SELECT * FROM Main ORDER BY id DESC ";
		$query = mysql_query($sql,$db);
		$total_results = mysql_num_rows($query);
		$limit = "3"; //limit of archived results per page.
		$total_pages = ceil($total_results / $limit); //total number of pages
if (empty($page))
	{
		$page = "1"; //default page if none is selected
	}
$offset = ($page - 1) * $limit; //starting number for displaying results out of DB

	$query = "SELECT * FROM Main ORDER BY id DESC LIMIT $offset, $limit";
	$result = mysql_query($query);
//This is the start of the normal results...
?>
<table><tr>
<?
	
while ($objResult = mysql_fetch_array($result))
		{
		?>
		<td width="260">
							<form enctype="multipart/form-data" name="fishingdetails" action="fishingdetails" method="post">
							<input type="hidden" name="id" value=<?=$objResult["id"];?>>
							<center><input type="image" src="http://theproofstation.com/upload/<?=$objResult["fishingphoto"];?>" border="0" width="150" alt="Submit" /></a><BR>
							<b><?=$objResult["fishingname"];?> <BR></b> <?=$objResult["postercity"];?>, <?=$objResult["posterstate"];?><BR><?=$objResult["fishingdate"];?><BR>
							<?=$objResult["fishingdescription"];?></form><BR></center></td>
		
	
		<?
		}
		?>
	</tr></table>	
		<?

		mysql_close();


// This is the Previous/Next Navigation
echo "<font face=Verdana size=3><center>";
echo "<BR>Pages:($total_pages)&nbsp;&nbsp;"; // total pages
if ($page != 1)
{
echo "<a href=$PHP_SELF?page=1><< First</a>&nbsp;&nbsp;&nbsp;"; // First Page Link
$prevpage = $page - 1;
echo "&nbsp;<a href=$PHP_SELF?page=$prevpage><<</a>&nbsp;"; // Previous Page Link
}
    	if ($page == $total_pages) 
			{
      			$to = $total_pages;
    		} 
		elseif ($page == $total_pages-1) 
			{
      			$to = $page+1;
    		} 
		elseif ($page == $total_pages-2) 
			{
     		 	$to = $page+2;
    		} 
		else 
			{
      			$to = $page+3;
    		}
    	if ($page == 1 || $page == 2 || $page == 3) 
			{
      			$from = 1;
    		} 
		else 
			{
      			$from = $page-3;
    		}
			
for ($i = $from; $i <= $to; $i++)

	{
	if ($i == $total_results) $to=$total_results;
	if ($i != $page)
		{
		echo "<a href=$PHP_SELF?showold=yes&page=$i>$i</a>";
		}
	else
		{
		echo "<b><font face=Verdana size=2>[$i]</font></b>";
		}
	if ($i != $total_pages)
		echo "&nbsp;";
	}
if ($page != $total_pages)
{
$nextpage = $page + 1;
echo "&nbsp;<a href=$PHP_SELF?page=$nextpage>>></a>&nbsp;&nbsp;"; // Next Page Link
echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=$PHP_SELF?page=$total_pages>Last >></center><BR></a>"; // Last Page Link
}
echo "</font>";

// This is the end of the Previous/Next Navigation
?>
Last edited by joeyoung25 on Sun Mar 04, 2012 3:49 pm, edited 1 time in total.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Prev and Next Buttons not functioning mysql data shows

Post by califdon »

First, I recommend that every place "<?" appears, you replace it with "<?php". The former is known as a "short form tag" and should be avoided. That isn't causing your problem, but it could if the script were run on a different server.

If what you showed us is the entire script, it's not complete. I don't see any code that looks for the form data from the previous page; that is, where does it get the value for $page? Without that, of course, it can't respond with the desired data from the database. It's not hard to insert the required code (check any tutorial on "pagination" (e.g., http://www.phpfreaks.com/tutorial/basic-pagination), but if you got this script from someone else, wouldn't it make more sense to ask that person to provide you with the code that works?
joeyoung25
Forum Newbie
Posts: 7
Joined: Sat Mar 03, 2012 4:05 am

Re: Prev and Next Buttons not functioning mysql data shows

Post by joeyoung25 »

OK I think I understand. but what should $page be ? I have done things similar to this before in a form, in fact thats how I made it possible to click on the fish images. ( theproofstation.com/fishing ). I have a hidden input type with the unique ID for that post that is auto incremented by the database when you post something. but I dont know how to do that for $page. any ideas what $page should equal?

If I put a

Code: Select all

$page = "1"; 
or

Code: Select all

$page = "2";
then the appropriate page is displayed.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Prev and Next Buttons not functioning mysql data shows

Post by califdon »

joeyoung25 wrote:OK I think I understand. but what should $page be ? I have done things similar to this before in a form, in fact thats how I made it possible to click on the fish images. ( theproofstation.com/fishing ). I have a hidden input type with the unique ID for that post that is auto incremented by the database when you post something. but I dont know how to do that for $page. any ideas what $page should equal?

If I put a

Code: Select all

$page = "1"; 
or

Code: Select all

$page = "2";
then the appropriate page is displayed.
I think you have answered your own question: $page should contain the value of the appropriate page to be displayed. Where does that come from? It comes from the link that the user clicks on, either "Previous" or "Next", right? So your script has to obtain the value from the "query string" that follows the ? at the end of the URL (href) in your script. How do you read that value? Read this: http://solidlystated.com/scripting/php- ... -variable/

I must add that the script you are using is full of deprecated syntax (that means that new scripts should NOT use them because, while some browsers may still recognize them, there is no guarantee that they will recognize them in the next update!). If you intend to actually deploy this script on a public Internet site, I would strongly advise you to start with a syntactically correct script. If you were an experienced PHP and HTML programmer, you could go through and correct these outdated expressions (like the <? that I mentioned earlier), but if you were experienced, you wouldn't have copied an old script to begin with.
joeyoung25
Forum Newbie
Posts: 7
Joined: Sat Mar 03, 2012 4:05 am

Re: Prev and Next Buttons not functioning mysql data shows

Post by joeyoung25 »

So I need to have something in the link that tells what page your currently on?
joeyoung25
Forum Newbie
Posts: 7
Joined: Sat Mar 03, 2012 4:05 am

Re: Prev and Next Buttons not functioning mysql data shows

Post by joeyoung25 »

When I hover over number 2 then I get this. wouldnt that mean that this is already where it should be?

http://www.theproofstation.com/fishing? ... yes&page=2
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Prev and Next Buttons not functioning mysql data shows

Post by califdon »

It's IN the URL already. Your script has to retrieve it from the URL so it can deliver the desired page. That's what the "query string" is all about. Did you read the reference I provided?
joeyoung25
Forum Newbie
Posts: 7
Joined: Sat Mar 03, 2012 4:05 am

Re: Prev and Next Buttons not functioning mysql data shows

Post by joeyoung25 »

OK thats what I was trying to do. I did read through it but I must have missed something. I will go through it again.

So I should end up with something like...

Code: Select all

$page="current page from URL blah blah blah"l
joeyoung25
Forum Newbie
Posts: 7
Joined: Sat Mar 03, 2012 4:05 am

Re: Prev and Next Buttons not functioning mysql data shows

Post by joeyoung25 »

OK so I think I had the right idea anyways. Heres what I ended up with. instead of the $page=...

Code: Select all

// get the current page or set a default
if (isset($_GET['page']) && is_numeric($_GET['page'])) {
   // cast var as int
   $page = (int) $_GET['page'];
} else {
   // default page num
   $page = 1;

Code: Select all

<?php
// get the current page or set a default
if (isset($_GET['page']) && is_numeric($_GET['page'])) {
   // cast var as int
   $page = (int) $_GET['page'];
} else {
   // default page num
   $page = 1;
} // end if$server = "localhost";
$user = "root";
$pass = "Laptop45";
$databasename = "proofstation";
$db = mysql_connect($server, $user, $pass);
	mysql_select_db($databasename,$db);

		$sql = "SELECT * FROM Main ORDER BY id DESC ";
		$query = mysql_query($sql,$db);
		$total_results = mysql_num_rows($query);
		$limit = "3"; //limit of archived results per page.
		$total_pages = ceil($total_results / $limit); //total number of pages
if (empty($page))
	{
		$page = "1"; //default page if none is selected
	}
$offset = ($page - 1) * $limit; //starting number for displaying results out of DB

	$query = "SELECT * FROM Main ORDER BY id DESC LIMIT $offset, $limit";
	$result = mysql_query($query);
//This is the start of the normal results...
?>
<table><tr>
<?php
	
while ($objResult = mysql_fetch_array($result))
		{
		?>
		<td width="260">
							<form enctype="multipart/form-data" name="fishingdetails" action="fishingdetails" method="post">
							<input type="hidden" name="id" value=<?=$objResult["id"];?>>
							<center><input type="image" src="http://theproofstation.com/upload/<?=$objResult["fishingphoto"];?>" border="0" width="150" alt="Submit" /></a><BR>
							<b><?=$objResult["fishingname"];?> <BR></b> <?=$objResult["postercity"];?>, <?=$objResult["posterstate"];?><BR><?=$objResult["fishingdate"];?><BR>
							<?=$objResult["fishingdescription"];?></form><BR></center></td>
		
	
		<?php
		}
		?>
	</tr></table>	
		<?php

		mysql_close();


// This is the Previous/Next Navigation
echo "<font face=Verdana size=3><center>";
echo "<BR>Pages:($total_pages)&nbsp;&nbsp;"; // total pages
if ($page != 1)
{
echo "<a href=$PHP_SELF?page=1><< First</a>&nbsp;&nbsp;&nbsp;"; // First Page Link
$prevpage = $page - 1;
echo "&nbsp;<a href=$PHP_SELF?page=$prevpage><<</a>&nbsp;"; // Previous Page Link
}
    	if ($page == $total_pages) 
			{
      			$to = $total_pages;
    		} 
		elseif ($page == $total_pages-1) 
			{
      			$to = $page+1;
    		} 
		elseif ($page == $total_pages-2) 
			{
     		 	$to = $page+2;
    		} 
		else 
			{
      			$to = $page+3;
    		}
    	if ($page == 1 || $page == 2 || $page == 3) 
			{
      			$from = 1;
    		} 
		else 
			{
      			$from = $page-3;
    		}
			
for ($i = $from; $i <= $to; $i++)

	{
	if ($i == $total_results) $to=$total_results;
	if ($i != $page)
		{
		echo "<a href=$PHP_SELF?showold=yes&page=$i>$i</a>";
		}
	else
		{
		echo "<b><font face=Verdana size=2>[$i]</font></b>";
		}
	if ($i != $total_pages)
		echo "&nbsp;";
	}
if ($page != $total_pages)
{
$nextpage = $page + 1;
echo "&nbsp;<a href=$PHP_SELF?page=$nextpage>>></a>&nbsp;&nbsp;"; // Next Page Link
echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=$PHP_SELF?page=$total_pages>Last >></center><BR></a>"; // Last Page Link
}
echo "</font>";

// This is the end of the Previous/Next Navigation
?>
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Prev and Next Buttons not functioning mysql data SOLVED

Post by califdon »

You're on the right track. Have you tried to run it? That's what you need to do in order to learn. If it runs properly, then you don't have to ask and you can reflect on what you did to make it work. If it fails, you can first try to see if you can understand why--did it send an error message? (You don't have error messaging turned on, which is a good idea during the debugging stage; see http://php.net/manual/en/function.error-reporting.php and http://coding.smashingmagazine.com/2011 ... designers/), did nothing at all print on the screen? (a common problem caused by PHP being unable to complete the parsing of your script), did it do something unexpectedly?. If there are no error messages, or you cannot understand what they mean, THAT's the time to post to the forum, describing exactly what you did, what error messages you received, if any, and exactly what didn't work as it should. This is the only way you will learn to do this yourself.
joeyoung25
Forum Newbie
Posts: 7
Joined: Sat Mar 03, 2012 4:05 am

Re: Prev and Next Buttons not functioning mysql data SOLVED

Post by joeyoung25 »

I didnt get any error messages. It just works perfect, now Im just perfecting the results display to pretty it up a little.
Post Reply