Help with next/previous array

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
tsm4781
Forum Commoner
Posts: 38
Joined: Wed Jul 09, 2003 7:17 pm

Help with next/previous array

Post by tsm4781 »

I am somewhat new to PHP when it comes to certain arrays. I have created a php/mySQL website, and am looking to display 50 listings of information per page, with a next and previous link as needed. I'm trying to find some array information to give me a good tutorial on how to create the links to the next 50 listings. Could someone point me in the right direction. I searched PHP.net, but I may be looking for the wrong thing as I couldn't find much.

Can anyone point me in the right direction?
User avatar
Sevengraff
Forum Contributor
Posts: 232
Joined: Thu Apr 25, 2002 9:34 pm
Location: California USA
Contact:

Post by Sevengraff »

well, you'll probally want to have some variable to keep track of what page your on, like $pg in the URL so you can get it as $_GET['pg']. You could multiply the page number by the number of results you want. like so:

Code: Select all

$page = 0;
if(isset($_GET['pg'])) {
  $page = $_GET['pg'];
}
$perpage = 50;
$results = $page * $perpage;
$query = mysql_query("Select * from table where thing > other_thing LIMIT $perpage , $page");

// output your data and stuff here ....

// link to other pages
if($page > 0) {
  $prev_page = '<a href="results.php?pg=' . ($page-1) . '">Previous Page</a>';
}
$next_page = '<a href="results.php?pg=' . ($page+1) . '">Previous Page</a>';
That should work
tsm4781
Forum Commoner
Posts: 38
Joined: Wed Jul 09, 2003 7:17 pm

Post by tsm4781 »

When I entered that code, I received a "Parse error: parse error, unexpected T_VARIABLE".. any thoughts?

Code: Select all

<?php 

include_once("db.inc.php");

mysql_connect($db_host,$db_user,$db_pass) or DIE("Could not connect to DB Server");
mysql_select_db("$db_name") or DIE("Could not find DB on Server");

$page = 0; 
if(isset($_GET['pg'])) 
{ 
  $page = $_GET['pg']; 
} 
$perpage = 50; 
$results = $page * $perpage; 
$query = mysql_query("Select * from members where LIMIT $perpage , $page"); 

// output your data and stuff here .... 

if($page > 0) { 
$prev_page = '<a href="results.php?pg=' . ($page-1) . '">Previous 50</a>'; 
} 
$next_page = '<a href="results.php?pg=' . ($page+1) . '">Next 50</a>';
?>
User avatar
Sevengraff
Forum Contributor
Posts: 232
Joined: Thu Apr 25, 2002 9:34 pm
Location: California USA
Contact:

Post by Sevengraff »

On what line number?
tsm4781
Forum Commoner
Posts: 38
Joined: Wed Jul 09, 2003 7:17 pm

Post by tsm4781 »

The error was on line 61. This page loads within a custom cp.php page which then calls a listusers.php.inc file as an include.

Code: Select all

<link rel="stylesheet" href="../style.css" type="text/css">

<title>Display Users</title>

<table width="100%" border="0" cellspacing="5" cellpadding="0">

  <tr>

    <td>

      <table width="100%" border="0" cellspacing="2" cellpadding="3" align="center" class="bordercolor">
        <tr> 
          <td colspan="6" class="underline"><font face="Verdana, Arial, Helvetica, sans-serif" size="3"><b>Current 
            Members :</b></font><font face="Verdana, Arial, Helvetica, sans-serif" size="2">&nbsp;</font></td>
        </tr>
        <tr bgcolor="#CCCCCC"> 
          <td width="10%"><b>ID</b></td>
          <td width="25%"><b>First Name</b></td>
          <td width="25%"><b>Last Name</b></td>
          <td width="20%"><b>Username</b></td>
          <td width="10%"><strong>EDIT</strong></td>
          <td width="10%"><strong>DELETE</strong></td>
        </tr>
        <?PHP	
			
			include_once("db.inc.php");

			mysql_connect($db_host,$db_user,$db_pass) or DIE("Could not connect to DB Server");
			mysql_select_db("$db_name") or DIE("Could not find DB on Server");
			
			$query = "SELECT * FROM members ORDER BY id";
			$result = mysql_query($query) or die("<strong class=black-large>Whoops! Something wrong happened to the my database! It would be nice if you emailed <a href=mailto:$webmaster>me</a> and told me!</strong>");
				if ($result) 
				&#123;
					while ($r3 = mysql_fetch_array($result)) 
					&#123;
					extract($r3);
					$id = $r3&#1111;0];
				echo "<tr><td width="10%" bgcolor="#e7e7e7"><b>$id</b></td><td width="25%" bgcolor="#e7e7e7"><b>$firstname</b></td><td width="25%" bgcolor="#e7e7e7"><b>$lastname</b></td><td width="20%" bgcolor="#e7e7e7"><b>$username</b></td><td width="10%" bgcolor="#e7e7e7"><a href=''>|Edit|</a></td><td width="10%" bgcolor="#e7e7e7"><a href='';>|Delete|</a></td></tr>";
					&#125;

				mysql_free_result($result);
				&#125;

		  
		  ?>
      </table>
        <table>
		<tr> 
          <td>
<?php 

include_once("db.inc.php");

mysql_connect($db_host,$db_user,$db_pass) or DIE("Could not connect to DB Server");
mysql_select_db("$db_name") or DIE("Could not find DB on Server");

$page = 0; 
if(isset($_GET&#1111;'pg'])) 
&#123; 
  $page = $_GET&#1111;'pg']; 
&#125; 
$perpage = 50; 
$results = $page * $perpage; 
$query = mysql_query("Select * from members where LIMIT $perpage , $page"); 

// output your data and stuff here .... 

if($page > 0) &#123; 
$prev_page = '<a href="results.php?pg=' . ($page-1) . '">Previous 50</a>'; 
&#125; 
$next_page = '<a href="results.php?pg=' . ($page+1) . '">Next 50</a>';
?>

			</td>
        </tr>
      </table>

    </td>

  </tr>

</table>
User avatar
Sevengraff
Forum Contributor
Posts: 232
Joined: Thu Apr 25, 2002 9:34 pm
Location: California USA
Contact:

Post by Sevengraff »

I didn't get any error like that, but I think you put the code I gave you in the wrong spot. Try something like this:

Code: Select all

<link rel="stylesheet" href="../style.css" type="text/css">

<title>Display Users</title>

<table width="100%" border="0" cellspacing="5" cellpadding="0">

  <tr>

    <td>

      <table width="100%" border="0" cellspacing="2" cellpadding="3" align="center" class="bordercolor">
        <tr>
          <td colspan="6" class="underline"><font face="Verdana, Arial, Helvetica, sans-serif" size="3"><b>Current
            Members :</b></font><font face="Verdana, Arial, Helvetica, sans-serif" size="2"> </font></td>
        </tr>
        <tr bgcolor="#CCCCCC">
          <td width="10%"><b>ID</b></td>
          <td width="25%"><b>First Name</b></td>
          <td width="25%"><b>Last Name</b></td>
          <td width="20%"><b>Username</b></td>
          <td width="10%"><strong>EDIT</strong></td>
          <td width="10%"><strong>DELETE</strong></td>
        </tr>
        <?PHP   
        
        include_once("db.inc.php"); 
         
		mysql_connect($db_host,$db_user,$db_pass) or DIE("Could not connect to DB Server");
		mysql_select_db("$db_name") or DIE("Could not find DB on Server");

		$page = 0;
		if(isset($_GET['pg']))
		{
		  $page = $_GET['pg'];
		}
		$perpage = 50;
		$results = $page * $perpage;
		$query = mysql_query("Select * from members LIMIT $results , $perpage");

         $result = mysql_query($query) or die("<strong class=black-large>Whoops! Something wrong happened to the my database! It would be nice if you emailed <a href=mailto:$webmaster>me</a> and told me!</strong>");
            if ($result)
            {
               while ($r3 = mysql_fetch_array($result))
               {
               extract($r3);
               $id = $r3[0];
            echo "<tr><td width="10%" bgcolor="#e7e7e7"><b>$id</b></td><td width="25%" bgcolor="#e7e7e7"><b>$firstname</b></td><td width="25%" bgcolor="#e7e7e7"><b>$lastname</b></td><td width="20%" bgcolor="#e7e7e7"><b>$username</b></td><td width="10%" bgcolor="#e7e7e7"><a href=''>|Edit|</a></td><td width="10%" bgcolor="#e7e7e7"><a href='';>|Delete|</a></td></tr>";
               }

            mysql_free_result($result);
            } 

			if($page > 0) {
			$prev_page = '<a href="results.php?pg=' . ($page-1) . '">Previous 50</a>';
			}
			$next_page = '<a href="results.php?pg=' . ($page+1) . '">Next 50</a>';

       
        ?>
      </table>
        <table>
      <tr>
          <td>

          <?PHP
          if(isset($prev_page)) {
	          echo $prev_page;
          }
          echo $next_page;
          ?>

         </td>
        </tr>
      </table>

    </td>

  </tr>

</table>
tsm4781
Forum Commoner
Posts: 38
Joined: Wed Jul 09, 2003 7:17 pm

Post by tsm4781 »

Very odd.. not I get the same error on line 24. So strange.
Post Reply