hey all, just workin on a script here.. need a little help.. might as well take a look at the script first
here it is..
@mysql_connect("localhost", "fdsfds", "fdsafdsa")
or die("Unable to connect");
$result =mysql_query("SELECT * FROM trucktable",$db);
while ($myrow = mysql_fetch_array($result))
{
printf("<tr>\n");
printf("<td width=\"97\" valign=\"top\" align=\"left\" background=\"images/sideing1.gif\"> </td>");
printf("<td width=\"120\" valign=\"top\" align=\"left\">%s</td>",$myrow[model]);
printf("<td width=\"1\" valign=\"top\" align=\"left\" background=\"images/sideing2.gif\"> </td>");
printf("<tr>\n");
}
now...lets say I have 50 entries in the databse.. and I only want 10 to be listed here.. how do I
a) make it stop after 10. I know I have to keep a variable and add 1 to it eachtime it loops, but how do I check if it reached 10?
b) Less important, but Id like to know just for the hell of it How would i go about finding the last row printed out, so on another page i can continue where it left of?
freddy showed me this code a while ago, and it's probably what you're looking for, thanks Freddy!
This code allows you to display a certain number of rows per page, with a Previous and Next link on the apporiate pages.
[php]
<?php
if(!isset($offset)) $offset = 0;
$recordsperpage = 10;
$result = mysql_query("SELECT COUNT(*) as totalnum from trucktable");
$row = mysql_fetch_array($result);
$totalrecords = $row["totalnum"];
$result = mysql_query("SELECT * from trucktable LIMIT $offset, $recordsperpage");
while ($row = mysql_fetch_array($result))
{
printf("<tr>\n");
printf("<td width=\"97\" valign=\"top\" align=\"left\" background=\"images/sideing1.gif\"> </td>");
printf("<td width=\"120\" valign=\"top\" align=\"left\">%s</td>",$myrow[model]);
printf("<td width=\"1\" valign=\"top\" align=\"left\" background=\"images/sideing2.gif\"> </td>");
printf("<tr>\n");
}
if ($totalrecords > $offset)
{
$newoffset = $offset + $recordsperpage;
printf('<a href="%s?offset=%s">more >></a>', $PHP_SELF, $newoffset);
}
if ($offset >= $recordsperpage)
{
$newoffset = $offset - $recordsperpage;
printf('<a href="%s?offset=%s">back <<</a>', $PHP_SELF, $newoffset);
}
thanx man.. now could u explain roughly how it works?
i get some of it.. but whats with the limit stuff in the select statement
$row = mysql_fetch_array($result);
$totalrecords = $row["totalnum"];
?? whats with that part there? If i understand correctly u are assinging totalrecords a value found in the database under the heading of totalnum?
It would be great if you could clear these things up.. Im sure it will work, but I want to know
how and why it works?
thanx
LIMIT is a special MySQL query function.
say you have records a, b, c, d, e, f
and you go:
SELECT blah blah LIMIT 1, 3
it start selecting from record 1, which is b (counting starts at zero), and select 3 records, which would be b, c, d.
$totalrecords just lets PHP knows how many records there are in the table, so it can show the next and previous link apporiately, you don't want to have a next button when you've reached the end of the table already, or have a previous button when there isn't any previous records.
and this..?
$result = mysql_query("SELECT COUNT(*) as totalnum from trucktable");
is totalnum also a basic mysql query?
thanx
No. It's just an alias to COUNT(*), so the number of COUNT is store in the $totalnum variable.
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Bookmarks