Help with this code Please
Posted: Sat Feb 19, 2005 9:52 pm
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ї'np'])){//Already been determined.
$num_pages = $_GETї'= np'];
} else { //Need to determine.
$query="SELECT photo, wording,price2,paypal2
FROM products
Left Join ".$_GETї'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) {//More than 1 page.
$num_pages =ceil ($num_records/$display);
} else {
$num_pages = 1;
}
}
//Determine where in the database to start returning results.
if (isset($_GETї's'])){//Already been determined.
$start = $_GETї's'];
} else {
$start = 0;
}
//Make the Query.
$query="SELECT photo, wording, price2, paypal2
FROM products
Left Join ".$_GETї'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){//If it ran OK, display the records.
echo "<h1>Products</h1>";
//Make the links to other pages, if necessary.
if ($num_pages > 1){
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){
echo "<a href="view_products.php?s=" . ($start - $display) . '&np=' . $num_pages ."">Previous</a> ";
}
//Make all the numbered pages
for ($i = 1; $i <= $num_pages; $i++){
if ($i != $current_page) {
echo "<a href="view_products.php?s=" . (($display * ($i - 1))) . '&np=' . $num_pages . "">' . $i . '</a>";
} else{
echo $i . ' ';
}
}
//if it's not the last= page, make a next button.
if ($current_page != $num_pages){
echo "<a href="view_products.php?s=" . ($start + $display) . '&np=' . $num_pages . "">Next</a> ";
}
echo '</p><br />';
}//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)){
$bg = ($bg=='#66CCFF' ? '#00CCFF' : '#66CCFF');//Switch the background color.
echo '<tr bgcolor = "', $bg, '"><td align="center">', stripslashes($rowї0]), '</td><td align="center">', $rowї1], '</td><td align="center">', $rowї2],'</td><td align="center">', $rowї3], '</td></tr>';
}
echo '</table>'; //Close the table.
mysql_free_result ($result); //Free up the resources.
} else {//If there are= no products .
echo '<h3>There are currently no products in this catagory.</h3>';
}
?>