Hi,
I am very new here so I hope this is the right place to ask. 🙂
Well I am needing to build a page that gives out results from a mysql webpage. It is running at http://www.aschumann.de/orbiter/query.php
code:
<?php
include ('includes/config.php');
include ('includes/error.php');
$query = ("SELECT *, DATE_FORMAT(date_added, '%d.%m%.%Y') FROM orbiter");
// Open the database connection
if (!($connection = @ mysql_connect($db_host,
$db_login,
$db_password)))
die("Could not connect to database");
if (!(mysql_select_db($db_database, $connection)))
showerror();
// Run the query on the database thrugh the connection
if(!($result = @ mysql_query ($query, $connection)))
showerror();
echo "<html><head><title></title></head><body bgcolor=\"FFFFFF\" text=\"#000000\">";
// Open a new Table for the results.
echo "\n<table border=\"0\" width=\"70%\" align=\"center\">";
// While there are still rows in the result set,
// fetch the current row into the arrey row
while ($row = @ mysql_fetch_row($result))
{
// Print the first, last name and Tutorial name, date_added
echo "\n<tr>\n\t<td bgcolor=\"maroon\" align=\"center\">" .
"<b><font color=\"white\">" .
"<b><font color=\"#FFFF00\">Auther: </font></b>" .
$row[1] .
" " .
$row[2] .
"<b><font color=\"#FFFF00\"> Tutorial: </font></b>" .
$row[3] .
"<b><font color=\"#FFFF00\"> Date submitted: </font></b>" .
$row[8] .
"</font></b></td>\n</tr>";
// Print the Description
echo "\n<tr>\n\t<td bgcolor=\"silver\">" .
"<b>Description: </b>" .
$row[4] .
"</td>\n</tr>";
// Print the domain and email
echo "\n<tr>\n\t<td bgcolor=\"gray\" align=\"center\">" .
"<b> Link </b><a href=\"$row[6]\">" .
$row[6] .
"</a><b> Email <a href=\"mailto:$row[5]\"></b>" .
$row[5] .
"</a></td>\n</tr>";
// Blank row for presentation
echo "\n<tr>\n\t<td></td>\n</tr>\n<tr>\n\t<td></td>\n</tr>";
}
// End Table
echo "\n</table>\n";
// Close the database connection
if (!mysql_close($connection))
showerror();
?>
Now I am wanting to make that it gives 20 rows per page and then to go to the next page etc..
I had looked into it trying using the the Article section
<?php
$limit=20; // rows to return
$numresults=mysql_query("select * from TABLE where YOUR CONDITIONAL HERE order by WHATEVER");
$numrows=mysql_num_rows($numresults);
// next determine if offset has been passed to script, if not use 0
if (empty($offset)) {
$offset=1;
}
// get results
$result=mysql_query("select id,name,phone ".
"from TABLE where YOUR CONDITIONAL HERE ".
"order by WHATEVER limit $offset,$limit");
// now you can display the results returned
while ($data=mysql_fetch_array($result)) {
// include code to display results as you see fit
}
// next we need to do the links to other results
if ($offset==1) { // bypass PREV link if offset is 0
$prevoffset=$offset-20;
print "<a href=\"$PHP_SELF?offset=$prevoffset\">PREV</a> \n";
}
// calculate number of pages needing links
$pages=intval($numrows/$limit);
// $pages now contains int of pages needed unless there is a remainder from division
if ($numrows%$limit) {
// has remainder so add one page
$pages++;
}
for ($i=1;$i<=$pages;$i++) { // loop thru
$newoffset=$limit*($i-1);
print "<a href=\"$PHP_SELF?offset=$newoffset\">$i</a> \n";
}
// check to see if last page
if (!(($offset/$limit)==$pages) && $pages!=1) {
// not last page so give NEXT link
$newoffset=$offset+$limit;
print "<a href=\"$PHP_SELF?offset=$newoffset\">NEXT</a><p>\n";
}
?>
That should do the trick for you. Of course, you'll probably want to clean up the HTML output...
Also, note that the links to $PHP_SELF only include the $offset. If you need to pass parameters for the where conditional of your query, you'll need to tack those on as well.
--Rod
I have tryed to understand it and build it into it but I am not getting any nearer. Anyone can please give me some help with this.
Thanks very much
schumanna.
P.S. Sorry for all this code. 🙂