Help with this code Please

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
Robert_T
Forum Newbie
Posts: 2
Joined: Sat Feb 19, 2005 9:46 pm

Help with this code Please

Post by Robert_T »

I am working on a script from PHP and MySQL for dynamic web site by Larry Ullman and am compleatly stuck. The code should query the database, find out how many records there are and if over 10 then only show 10 and make a next button. if 10 or under show the 10 and make no next button. If I set display = 20 then all 20 products show like the should, if I set display = 10 then only 10 products show like the should. The problem is that no NEXT link is made. Here is my code so far.

Code: Select all

<?php # Script 3.4 - index.php
include_once ('./header.inc');

?>

<table width="90%" border="1" cellspacing="2" cellpadding="4" align="center">
  <tr bgcolor="#0d2696">     <td>    <table width="100%" border="1" cellspacing="0" cellpadding="4">
        <tr>           <td bgcolor="#0d2696"><img src="/pic/special/rid.php?pic=random"></td>


          <td width="100%"align='center'> <font color="#FFCC00"><font size="5"> <b>Knifes and Swords</b></font></td>

				<td><?php include_once ('./chat.inc')?></td>
        </tr>
      </table></td>
 
<table width="90%" border="0" cellspacing="4" cellpadding="2" align="center">
  <tr>     <td width="70%" valign="top"> 
      <hr />       
 <?php
      require_once('../mysql_connect.php');
//number of records to= show per page
$display = 2;
//Determine how many pages there are.
if (isset($_GET&#1111;'np']))&#123;//Already been determined.
$num_pages = $_GET&#1111;'= np'];

&#125; else &#123; //Need to determine.
   $query="SELECT   photo, wording,price2,paypal2
    FROM products
Left Join ".$_GET&#1111;'imd2']."
On inumber = inumber2
    Where cat2=1
order by inumber
ASC";
    $query_results = mysql_query ($query);
$num_records = @mysql_num_rows ($query_result);
if ($num_records > $display) &#123;//More than 1 page.
$num_pages =ceil ($num_records/$display);

&#125; else &#123;
$num_pages = 1;


&#125;
&#125;
//Determine where in the database to start returning results.
if (isset($_GET&#1111;'s']))&#123;//Already been determined.
$start = $_GET&#1111;'s'];
&#125; else &#123;
$start = 0;
&#125;
//Make the Query.
$query="SELECT  photo, wording, price2, paypal2
    FROM products
Left Join ".$_GET&#1111;'imd2']."
On inumber = inumber2
    Where cat2=1
order by inumber
ASC LIMIT $start, $display";
$result = @mysql_query ($query); //Run the query.
$num = mysql_num_rows ($result);//How many Products are there?
if ($num > 0)&#123;//If it ran OK, display the records.
      echo "<h1>Products</h1>";
//Make the links to other pages, if necessary.
      if ($num_pages > 1)&#123;
            echo '<p>';

//Determine what page the script is on.
      $current_page = ($start/$display) + 1;


//If it's not the first page, make a previous button.
      if ($current_page != 1)&#123;
            echo "<a href="view_products.php?s=" . ($start - $display) . '&np=' . $num_pages ."">Previous</a> ";
&#125;
//Make all the numbered pages
      for ($i = 1; $i <= $num_pages; $i++)&#123;
            if ($i != $current_page) &#123;
                  echo "<a href="view_products.php?s=" . (($display * ($i - 1))) . '&np=' . $num_pages . "">' . $i . '</a>";
&#125; else&#123;
      echo $i . ' ';

&#125;
&#125;
//if it's not the last= page, make a next button.
if ($current_page != $num_pages)&#123;
      echo "<a href="view_products.php?s=" . ($start + $display) . '&np=' . $num_pages . "">Next</a> ";
&#125;
echo '</p><br />';
&#125;//End of links 
//Table header.
echo '<table width="90%" align="center" cellspacing="2" cellpadding="2">
<tr><td align="center"><b>Photo</b></td><td align="center"><b>Decription</b></td><td align="center"><b>Price</b></td><td align="center"><b>Payment</b></td></tr>' ;
//Fetch and print all = the records.
$bg ='#66ccff';//Set the background color.
while ($row = mysql_fetch_array($result, MYSQL_NUM))&#123;
      $bg = ($bg=='#66CCFF' ? '#00CCFF' : '#66CCFF');//Switch the background color.
      echo '<tr bgcolor = "', $bg, '"><td align="center">', stripslashes($row&#1111;0]), '</td><td align="center">', $row&#1111;1], '</td><td align="center">', $row&#1111;2],'</td><td align="center">', $row&#1111;3],  '</td></tr>';
&#125;
      echo '</table>'; //Close the table.
      mysql_free_result ($result); //Free up the resources.
&#125; else &#123;//If there are= no products .
      echo '<h3>There are currently no products in this catagory.</h3>';
&#125;

      
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

search for "pagination".
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

your problem has a name :) it's called pagination... and we have discussed it many times before...
Robert_T
Forum Newbie
Posts: 2
Joined: Sat Feb 19, 2005 9:46 pm

Post by Robert_T »

Sorry I had no idea first time I have been here. Will look around and see what I can find under that name.
Post Reply