Here's an odd one:
I've coded a script (included below) that fetches some records, displays them according to the limit per page, and provides navigation buttons.
I'm in the design phase, so when I call the script the first time, there is no Ad Section specified. The script just gets all of the records in the designated table. Well, I got the thing working fine, so I decided to test it with records limited to my chosen Ad Section. I log into the site and choose an Ad Section to browse.
Everything on the script works as it should, except it does not display records!
Now, before you ask, I've included debug statements for the following:
result set: it's there and it's valid.
LIMIT clause: the numbers are valid and correct.
records: they exist, and the field I've chose to test has data.
Any ideas would be greatly appreciated. In short, the script works exactly as it should with a LIMIT clause, but for some reason, it is echoing blanks. Below is the code. Thanks any and all for ideas/suggestions.
<?php
include "common.php";
html_header();
if($_REQUEST['run'] == "") get_ad_recordset(); //run get_ad_recordset on first call to script
else show_ads($_REQUEST['screen']); //run here on subsequent calls
function get_ad_recordset()
{
//Query the database for all ads
switch($_REQUEST['as'])
{
case 1: $_SESSION['ad_select'] = $ad_section = '1961 Tempest Parts List';
break;
case 2: $_SESSION['ad_select'] = $ad_section = '1962 Tempest/LeMans Parts List';
break;
case 3: $_SESSION['ad_select'] = $ad_section = '1963 Tempest/LeMans Parts List';
break;
case 4: $_SESSION['ad_select'] = $ad_section = 'Pontiac (non 61-63 Tempest/LeMans) Parts List';
break;
case 5: $_SESSION['ad_select'] = $ad_section = 'Non-Pontiac Car-Related Parts List';
break;
case 6: $_SESSION['ad_select'] = $ad_section = 'Manuals and Literature List';
break;
case 7: $_SESSION['ad_select'] = $ad_section = 'Memorabilia and Collectibles List';
break;
case 8: $_SESSION['ad_select'] = $ad_section = 'Tools, Equipment and Shop Items List';
break;
case 9: $_SESSION['ad_select'] = $ad_section = 'Members "Stuff for Sale" List';
break;
default: $_SESSION['ad_select'] = $ad_section = '';
break;
}
$link_id = db_connect();
if(empty($ad_section))
$query = "SELECT * FROM advertisement";
else
$query = "SELECT * FROM advertisement WHERE adSection = '$ad_section'";
$result = mysql_query($query);
$_SESSION['num_rows'] = mysql_num_rows($result); //determine total rows;
mysql_close($link_id);
if($result) @show_ads($screen);
else query_fail();
}
function show_ads($screen)
{
$rows_per_page = 10;
$rows = $_SESSION['num_rows'];
echo $rows . "<br>"; //debug statement
$pages = ceil($rows / $rows_per_page);
echo $pages . "<br>"; //debug statement
$last_page = $rows % $rows_per_page;
echo $last_page . "<br>"; //debug statement
if (empty($screen)) {
$screen = 1; //sets limits on LIMIT clause in SQL statement;
$start = 0; //sets limits on LIMIT clause in SQL statement;
}
else
$start = ($rows_per_page * $screen) - $rows_per_page; //Determine next starting point in recordset
//Example: $start = (10 * 2) - 10 for Page 2
if($screen != $pages) {
$link_id = db_connect();
if(empty($_SESSION['ad_select']))
$query = "SELECT * FROM advertisement LIMIT $start, $rows_per_page";
else
$query = "SELECT * FROM advertisement WHERE adSection = '$ad_section' LIMIT $start, $rows_per_page";
}
else {
$link_id = db_connect();
if(empty($_SESSION['ad_select']))
$query = "SELECT * FROM advertisement LIMIT $start, $last_page";
else
echo $_SESSION['ad_select'] . "<br>";
$query = "SELECT * FROM advertisement WHERE adSection = '$ad_section' LIMIT $start, $last_page";
}
$result = mysql_query($query);
//Problems occur in this code segment. Works fine if the SQL statement does not have a LIMIT
//clause in it. If LIMIT, echos blanks!
if($screen != $pages) {
for ($i = 0; $i < $rows_per_page; $i++) {
$arr = mysql_fetch_assoc($result);
echo $arr["adPrice"] . "<br>";
echo "Screen != pages <br>"; //debug statement
}
}
else {
for($i=0; $i < $last_page; $i++) {
$arr = mysql_fetch_assoc($result);
echo $arr["adPrice"] . "<br>";
echo "Screen = pages <br>"; //debug statement
}
}
echo "<p><hr></p>\n";
// let's create the dynamic links now
if ($screen > 1) {
$url = $_SERVER['PHP_SELF'] . "?run=no&screen=" . ($screen - 1);
echo "<a href=\"$url\">Previous</a>\n";
}
// page numbering links now
for ($i = 1; $i <= $pages; $i++) {
$url = $_SERVER['PHP_SELF'] . "?run=no&screen=" . $i;
echo " | <a href=\"$url\">$i</a> | ";
}
if ($screen < $pages) {
$url = $_SERVER['PHP_SELF'] . "?run=no&screen=" . ($screen + 1);
echo "<a href=\"$url\">Next</a>\n";
}
}
?>