Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Display 5 records per page.

Status
Not open for further replies.

padkinson

Technical User
Apr 2, 2002
48
0
0
US
I have a database with several records in it. I want to be able to display only 5 records per page and have links at the bottom of the page to display the rest. The problem I am having is that I can’t get the code to go to the next set of records. It just keeps getting the first 5. Here is the code. Thanks for the help.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"
<html>

<head>


<?php
//set database connection variables
$hostname='localhost';
$username='name';
$password='password';
$database='database';
$table='table';

//establish database connection
$connect = mysql_connect($hostname,$username,$password);
//display error if connection fails
if ($connect==FALSE) {
print 'Unable to connect to database: '.mysql_error();
exit;
}
$pagenum=$_GET[page];
if ($pagenum == 0)
$pagenum = 1;

$pagesize = $_GET[recs];
if ($pagesize == 0)
$pagesize = 5;

//$pagesize = 5;

//if ($pagenum = 0)
// $pagenum = 1;


echo $pagenum . " this is pagenum <br>";

//select database
mysql_select_db($database);

$username = $_SESSION['username'];

// Request the content from the db for display in the content of the text area
$result = mysql_query(
"SELECT * FROM $table order by WO");
if (!$result) {
echo("<P>Error performing query: " .
mysql_error() . "</P>");
exit();
}
$totalrecords = mysql_num_rows($result);

$temp = $totalrecords / $pagesize;

$numpages = ceil($temp);

?>


</head>

<body>
<br>
<table border="0" cellpadding="0" cellspacing="3">
<tr>
<td valign="top">
<h2><img src="../../../../../images/title_images/title_staff_mis.gif" width="394" height="80" border="0" alt="Faculty/Staff MIS LOGO"></h2>
</td>
<td valign="top" align="left">
<table border="0" cellpadding="0" cellspacing="3">
<tr>
<td align="left"><img src="../../../../../images/home_fast_line.gif" width="188" height="1" border="0" alt=""></td>
</tr>
<tr>
<td align="left"><?
echo ($_SESSION['username'] . " is Logged On");

?></td>
</tr>
<tr>
<td align="left"><img src="../../../../../images/home_fast_line.gif" width="188" height="1" border="0" alt=""></td>
</tr>
<tr>
<td align="left"><img src="../../../../../images/arrow_bullet.gif" width="7" height="9" border="0" alt=""> <a href="../../../logout.php" class="home">Logout</a></td>
</tr>
<tr>
<td align="left"><img src="../../../../../images/home_fast_line.gif" width="188" height="1" border="0" alt=""></td>
</tr>
<tr>
<td align="left"><img src="../../../../../images/arrow_up.gif" width="8" height="7" border="0" alt=""> <a href="../../../index.htm">back (Staff Area Index)</a></td>
</tr>
<tr>
<td align="left"><img src="../../../../../images/home_fast_line.gif" width="188" height="1" border="0" alt=""></td>
</tr>
<tr>
<td align="left"><img src="../../../../../images/arrow_up.gif" width="8" height="7" border="0" alt=""> <a href="../../index.htm">back (Departments)</a></td>
</tr>
</table>
</td>
</tr>
</table>
<br>
<table border="0" cellpadding="0" cellspacing="3">
<tr>
<td align="left" valign="top"><table border="0" cellpadding="0" cellspacing="3" valign="top">
<tr>
<td align="left" valign="top"><img src="../../../../../images/home_fast_line.gif" width="188" height="1" border="0" alt=""></td>
</tr>
<tr>
<td align="left" valign="top">
<img src="../../../../../images/arrow_bullet.gif" width="7" height="9" border="0" alt=""> <a href=" class="home" target="new">MIS Request</a></td>
</tr>
<tr>
<td align="left" valign="top"><img src="../../../../../images/home_fast_line.gif" width="188" height="1" border="0" alt=""></td>
</tr>
<tr>
<td align="left" valign="top">
<img src="../../../../../images/arrow_bullet.gif" width="7" height="9" border="0" alt=""> <a href=" class="home" target="new">MIS Request Status</a></td>
</tr>
<tr>
<td align="left" valign="top"><img src="../../../../../images/home_fast_line.gif" width="188" height="1" border="0" alt=""></td>
</tr>
<tr>
<td align="left" valign="top">
<img src="../../../../../images/arrow_bullet.gif" width="7" height="9" border="0" alt=""> <a href=" class="home" target="new">ARFA Status</a></td>
</tr>
<tr>
<td align="left" valign="top"><img src="../../../../../images/home_fast_line.gif" width="188" height="1" border="0" alt=""></td>
</tr>
<tr>
<td align="left" valign="top">
<img src="../../../../../images/arrow_bullet.gif" width="7" height="9" border="0" alt=""> <a href=" class="home" target="new">Employee Access System</a></td>
</tr>
<tr>
<td align="left" valign="top"><img src="../../../../../images/home_fast_line.gif" width="188" height="1" border="0" alt=""></td>
</tr>
<tr>
<td align="left" valign="top">
<img src="../../../../../images/arrow_bullet.gif" width="7" height="9" border="0" alt=""> <a href=" class="home" target="new">MIS Staff Logon</a></td>
</tr>
<tr>
<td align="left" valign="top"><img src="../../../../../images/home_fast_line.gif" width="188" height="1" border="0" alt=""></td>
</tr>
</table></td>

<td valign="bottom" align="left"><img src="../../../../../images/clear_spacer.gif" width="50" height="8" border="0" alt=""></td>
<td valign="top" align="left"><div align="center">
<table border="0" cellpadding="5" cellspacing="0">

<tr>
<td valign="top" align="left" colspan="2"><?php
// get the stuff
echo $a . " this is a <br>";

//if ($numpage = 1)
// $n=1

for ($n=1; $n<=$pagesize; $n++) {
$row = mysql_fetch_array($result);
$email = $row["email_add"];
$WO = $row["wo"];
$Date_rec = $row["date_rec"];
$Person_requesting_service = $row["person_requesting_service"];
$Dept = $row["dept"];
$Campus = $row["campus"];
$Building = $row["building"];
$Room = $row["room"];
$Phone = $row["phone"];
$Date_needed = $row["date_needed"];
$Description_of_Service_being_requested = $row["description_of_service_being_requested"];
$Person_receiving_request = $row["person_receiving_request"];
$Date_assigned = $row["date_assigned"];
$Person_assigned = $row["person_assigned"];
$Date_completed = $row["date_completed"];
$Description_of_action_taken = $row["description_of_action_taken"];

echo("<table align='center' border='0' cellpadding='0' cellspacing='2'>".
"<tr class='name'>".
"<td colspan='2'><font color='#FFFFFF'>&nbsp;&nbsp;Requested by: <a href='mailto:". $email . "'><font color='#FFFFFF'>". $Person_requesting_service . "</font></a> on ". $Date_rec ."&nbsp;&nbsp;</font><br>".
"</td>".
"</tr>");
echo("<tr class = odd>".
"<td align='default'>Work Order </td>".
"<td align='default'>". $WO ."</td>".
"</tr>");
echo("<tr class = even>".
"<td align='default'>Date_needed </td>".
"<td align='default'>". $Date_needed ."</td>".
"</tr>");
echo("<tr class = odd>".
"<td align='default'>Department</td>".
"<td align='default'>". $Dept ."</td>".
"</tr>");
echo("<tr class = even>".
"<td align='default'>Campus </td>".
"<td align='default'>". $Campus ."</td>".
"</tr>");
echo("<tr class = odd>".
"<td align='default'>Building</td>".
"<td align='default'>". $Building ."</td>".
"</tr>");
echo("<tr class = even>".
"<td align='default'>Room </td>".
"<td align='default'>". $Room ."</td>".
"</tr>");
echo("<tr class = odd>".
"<td align='default'>Phone</td>".
"<td align='default'>". $Phone ."</td>".
"</tr>");

echo("<tr class = odd>".
"<td align='default'>Description of Problem</td>".
"<td align='default'>". $Description_of_Service_being_requested ."</td>".
"</tr>");
echo("<tr class = even>".
"<td align='default'>Person_receiving_request</td>".
"<td align='default'>". $Person_receiving_request ."</td>".
"</tr>");
echo("<tr class = odd>".
"<td align='default'>Date_assigned </td>".
"<td align='default'>". $Date_assigned ."</td>".
"</tr>");
echo("<tr class = even>".
"<td align='default'>Person Assigned </td>".
"<td align='default'>". $Person_assigned ."</td>".
"</tr>");
echo("<tr class = odd>".
"<td align='default'>Date Completed </td>".
"<td align='default'>". $Date_completed ."</td>".
"</tr>");
echo("<tr class = even>".
"<td align='default'>Description Action</td>".
"<td align='default'>". $Description_of_action_taken ."</td>".
"</tr>");

echo("</table>");

?>
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%" id="AutoNumber1" height="22">
<tr>
<form method="post" action="mismodify2.php">
<td>
<p align="center">
<font face="Verdana"><b><font size="1">Click here to edit/close WO.
</font> </b>
</font>
<input type="submit" name="WO" value="<?php echo($WO); ?>" /></font></td>
</form>
</tr>
</table>

<br>
<?php
}

echo "<center>";

for ($a=1; $a<=$numpages; $a++){
$lb = ($a-1) * $pagesize + 1;
$ub = $a * $pagesize;
if ($ub > $totalrecords)
$ub = $totalrecords;
if ($a != $pagenum)
echo "<a href=index.htm?page=" . $a . "&recs=" . $pagesize . ">" . $lb . "-" . $ub . "</a>";
else
echo $lb . "-" . $ub;
if ($a != $numpages)
echo " | ";
}
echo "</center>";
?>


</td>
</tr>

</table>
</div>
<p><br>
<br>
<br>
<br>
<br>
<br>
<!--company footer--><?php
//This block of PHP gets the header (footer.html) and puts it here ****************
$incfilename = "/var/if (file_exists($incfilename))
include($incfilename);
else{
echo "<span class=\"alert\">There is a problem with our company page header, please notify the <a href =\"mailto:webmaster@company.com\">webmaster</a> now.</span>\n";
}
?></p>
</body>

</html>
 
Well after going through this horrid amount of coding I can see where you are going.

Maybe you should start by making a simple example and forget about layout etc for starters.

Now there are two things you can do and because there is so much coding I am not entirely sure if you are already doing one....

You can use the select * from table where bla.... limit (startrecord-endrecord) for each page, as you are already know these from your variable used to create your hyperlink. This will however mean that for the first page you will need a second query to start with to determine the total pages.

Also I can't figure out if you are using the variable you are passing through when determining your page to display?
But instead of $n maybe this could be replayed with the record number you wish to display and the pagesize may be the max record to be displayed for that page..... Unfortunatelly I am not entirely sure that if in your current script if you say $n=18 that it will actually take row 18 and you may need to change it a bit so you tell it which row to take like row 18, value X

Hope this helps. I will have a look in some example files I got on how to fix the latter.

JR
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top