Jump to content

**Solved ** need some help on missing record and page numbers


stubyh

Recommended Posts

Hi

   I have two problems in the following code and need help

   1) I have a DB with 18 items in it and would only like to display the first 10 on each page, but it's not working I only get 1 page with all items listed.

   2) the first record is not displayed only items displayed are displayed 2 - 18


    Thanks

[code]
<? include("templates/header.php"); ?>

<?php

  // get the data from the database
require_once('Connections/banners.php');


// Number of records to show per page:
$display = 10;

// Determine how many pages there are.
if (isset($_GET['np'])) { // Already been determined.

$num_pages = $_GET['np'];

} else { // Need to determine.


    $get_ids_sql = "SELECT id, link AS url, alt_title, image FROM banners WHERE status = 'on'";
    $get_ids_res = mysql_query ($get_ids_sql);
    $row = mysql_fetch_array($get_ids_res, MYSQL_NUM);
    $num_records = $row[0];

    // check the output - remove after fix
echo $num_records;
echo $display;
    // end check

// Calculate the number of pages.
if ($num_records > $display) { // More than 1 page.
$num_pages = ceil($num_records/$display);
} else {
$num_pages = 1;
}

} // End of np IF.



// create the page for real

echo '<body bgcolor="#FFFFFF"  text="#000000" link="#003399" vlink="#003399" alink="#CC0000">';

echo '</td>
<table width=80% height=100% align="center" border=0 cellpadding=0 cellspacing=0 >
<tr>
<td width=75% height=100%>&nbsp;</td>
<td valign=top bgcolor=#FFFFFF>
<table cellspacing="0" cellpadding="0" border="0" width="791" height=100%>
  <tr><td colspan="2" rowspan="2" valign="top" align="center">
<table cellspacing="0" cellpadding="10" border="0">';




// Fetch and print all the records.

while ($row = mysql_fetch_array($get_ids_res, MYSQL_ASSOC)) {
// remove the {$row['id']} in echo statement after fix
echo '
<tr><td width="770" align="center">

      <table cellspacing="0" cellpadding="0" border="0" width="680">
      <tr><td>
        <table cellspacing="0" cellpadding="6" border="0" width="680">
        <tr>
        <td bgcolor="#FFFFFF" align="center">';

echo "<a href=\"{$row['url']}\"><img src=\"/banner/bannerimages/{$row['image']}\" alt=\"{$row['alt_title']}\" border=\"0\">{$row['id']}</p>";

echo '


</td>
        </tr>
        </table>
      </td>
      <td bgcolor="#C2C2C2"><img src="spacer.gif" width="2" height="1" border="0"></td></tr>
      <tr><td bgcolor="#C2C2C2"><img src="spacer.gif" width="1" height="2" border="0"></td>
      <td bgcolor="#C2C2C2"><img src="spacer.gif" width="1" height="1" border="0"></td></tr>
      </table>

</td>';
}
?>
<td width=50% height=100%>&nbsp;</td></tr></table>
<p>&nbsp;</p>
</table>
<?


// Make the links to other pages, if necessary.
if ($num_pages > 1) {

echo '<br /><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="index3.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="index3.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="index3.php?s=' . ($start + $display) . '&np=' . $num_pages . '">Next</a>';
}

echo '</p>';

} // End of links section.
?>



<? include("templates/footer.php"); ?>
[/code]
Hi

  OK solved problem (2) and sorry missed some code out from the first, It now only displays the first 10 (thats good) but the next page links are not showing (that's bad)

any ideas  ???

new code

[code]
<? include("templates/header.php"); ?>

<?php

  // get the data from the database
require_once('Connections/banners.php');


// Number of records to show per page:
$display = 10;

// Determine how many pages there are.
if (isset($_GET['np'])) { // Already been determined.

$num_pages = $_GET['np'];

} else { // Need to determine.

    $get_ids_sql = "SELECT COUNT(*) FROM banners";
    $get_ids_res = mysql_query ($get_ids_sql);
    $row = mysql_fetch_array($get_ids_res, MYSQL_NUM);
    $num_records = $row[0];

    // check the output - remove after fix
echo $num_records;
echo $display;
    // end check

// Calculate the number of pages.
if ($num_records > $display) { // More than 1 page.
$num_pages = ceil($num_records/$display);
} else {
$num_pages = 1;
}

} // End of np IF.


// Determine where in the database to start returning results.
if (isset($_GET['s'])) {
$start = $_GET['s'];
} else {
$start = 0;
}

    $get_ids_sql = "SELECT id, link AS url, alt_title, image FROM banners WHERE status = 'on'LIMIT $start, $display";
    $get_ids_res = mysql_query ($get_ids_sql);

// create the page for real

echo '<body bgcolor="#FFFFFF"  text="#000000" link="#003399" vlink="#003399" alink="#CC0000">';

echo '</td>
<table width=80% height=100% align="center" border=0 cellpadding=0 cellspacing=0 >
<tr>
<td width=75% height=100%>&nbsp;</td>
<td valign=top bgcolor=#FFFFFF>
<table cellspacing="0" cellpadding="0" border="0" width="791" height=100%>
  <tr><td colspan="2" rowspan="2" valign="top" align="center">
<table cellspacing="0" cellpadding="10" border="0">';




// Fetch and print all the records.

while ($row = mysql_fetch_array($get_ids_res, MYSQL_ASSOC)) {
// remove the {$row['id']} in echo statement after fix
echo '
<tr><td width="770" align="center">

      <table cellspacing="0" cellpadding="0" border="0" width="680">
      <tr><td>
        <table cellspacing="0" cellpadding="6" border="0" width="680">
        <tr>
        <td bgcolor="#FFFFFF" align="center">';

echo "<a href=\"{$row['url']}\"><img src=\"/banner/bannerimages/{$row['image']}\" alt=\"{$row['alt_title']}\" border=\"0\">{$row['id']}</p>";

echo '


</td>
        </tr>
        </table>
      </td>
      <td bgcolor="#C2C2C2"><img src="spacer.gif" width="2" height="1" border="0"></td></tr>
      <tr><td bgcolor="#C2C2C2"><img src="spacer.gif" width="1" height="2" border="0"></td>
      <td bgcolor="#C2C2C2"><img src="spacer.gif" width="1" height="1" border="0"></td></tr>
      </table>

</td>';
}

mysql_free_result ($get_ids_res); // Free up the resources.

mysql_close(); // Close the database connection.

?>
<td width=50% height=100%>&nbsp;</td></tr></table>
<p>&nbsp;</p>
</table>
<?


// Make the links to other pages, if necessary.
if ($num_pages > 1) {

echo '<br /><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="index3.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="index3.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="index3.php?s=' . ($start + $display) . '&np=' . $num_pages . '">Next</a>';
}

echo '</p>';

} // End of links section.
?>



<? include("templates/footer.php"); ?>[/code]
True but what if there are only 10 items to be displayed then no need for a next page button. only need button if there are 11 or more. the SQL code will eventually be driven from a search, but I'm a long way off from that.

  stubyh
I have solved the problem, below is listed the code.



[code]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>


<? include("templates/header.php"); ?>

<table width="100%" border="0" cellpadding="1">
  <tr>
    <td width=20%>&nbsp;</td>

    <td width=60%>

<?php

  // get the data from the database
require_once('Connections/banners.php');


// Number of records to show per page:
$display = 10;

// Determine how many pages there are.
if (isset($_GET['np'])) { // Already been determined.

$num_pages = $_GET['np'];

} else { // Need to determine.

    $get_ids_sql = "SELECT COUNT(*) FROM banners";
    $get_ids_res = mysql_query ($get_ids_sql);
    $row = mysql_fetch_array($get_ids_res, MYSQL_NUM);
    $num_records = $row[0];


// Calculate the number of pages.
if ($num_records > $display) { // More than 1 page.
$num_pages = ceil($num_records/$display);
} else {
$num_pages = 1;
}

} // End of np IF.


// Determine where in the database to start returning results.
if (isset($_GET['s'])) {
$start = $_GET['s'];
} else {
$start = 0;
}

    $get_ids_sql = "SELECT id, link AS url, alt_title, image FROM banners WHERE status = 'on'LIMIT $start, $display";
    $get_ids_res = mysql_query ($get_ids_sql);
// Fetch and print all the records.

while ($row = mysql_fetch_array($get_ids_res, MYSQL_ASSOC)) {


echo "<a href=\"{$row['url']}\"><img src=\"/banner/bannerimages/{$row['image']}\" alt=\"{$row['alt_title']}\" border=\"0\"></p>";


}

mysql_free_result ($get_ids_res); // Free up the resources.

mysql_close(); // Close the database connection.

?>

&nbsp;</td>
    <td width=20%>&nbsp;</td>
  </tr>
</table>
<table width="200" border="0" cellpadding="1">
  <tr>
    <td><?


// Make the links to other pages, if necessary.
if ($num_pages > 1) {

echo '<br /><p>';
// Determine what page the script is on.
$current_page = ($start/$display) + 1;
echo 'Page >> ';
// If it's not the first page, make a Previous button.
if ($current_page != 1) {
   
echo '<a href="index4.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="index4.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="index4.php?s=' . ($start + $display) . '&np=' . $num_pages . '">Next</a>';
}



} // End of links section.

?>

</td>
  </tr>
</table>
<? include("templates/footer.php"); ?>


<p>&nbsp;</p>
</body>
</html>[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.