Well, I need some more help. I stayed up until 4:30 this morning working on my site along with other scripts and this one, and this one is really really kicking my butt. Ok, whta Im doing is creating the Prev / Next buttons as explained here at phpbuilder.com and I cannot for the life of me get it to work 100%. My archive script needs the next prev buttons so you can view 5 scripts on each page, but what is happening is that the next prev links are conflicting somehow and it only displays the script from a given catagory ONLY if there are 2 records for it, and still it will only display one of the 2. I have heavily commented as much as possible to make things easier, here it is: (also, can someone tell me if this is considered nice & neat code)
<?php
//a somewhat working version is found at http://acidsun.com/scripts.php
//Regular connection to MySQL
$user="user";
$pass="password";
$db="acidsun_acidsun";
$link=mysql_connect("localhost",$user,$pass);
if(!$link)
die("Couldn't connect to MySQL.");
mysql_select_db($db,$link)
or die("Couldn't open $db: ".mysql_error());
//Following was taken from phpbuilder.com and it somewhat works
//http://www.phpbuilder.org/columns/rod20000221.php3
if(isset($id) && !empty($id))//begin of first if(statement) it shows results for
//the id selected (i.e. scripts?id=1 would be the
//first id, which at acidsun, it is the db "General"
{
$limit=20; // rows to return
//I'm pretty sure this code is right, it may want to be checked
$numresults=mysql_query("select * from php_scripts WHERE cat_id=$id");
$numrows=mysql_num_rows($numresults);
// next determine if offset has been passed to script, if not use 0
//I think the problem lies in here. It only allows me to show my results
//ONLY if I have 2 or more in that table and it will not show the first one
if (empty($offset))
{
$offset=1;
}
//returns all the scripts in selected archive
$result=mysql_query("SELECT * FROM php_scripts WHERE cat_id=$id ORDER BY id DESC LIMIT $offset,$limit");
//While loop to print out scripts
while($a_row=mysql_fetch_array($result))
{
print"<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" height=\"22\">\n";
print"<tr>\n";
print"<td><b>$a_row[title]</b></td>\n";
print"</tr>\n";
print"<tr>\n";
print"<td>\n";
print"<p>$a_row[description]</p>\n";
print"</td>\n";
print"</tr>\n";
print"<tr>\n";
print"<td><font color=\"#999999\" size=\"2\">Posted by <a href=\"mailto:$a_row[auth_email]\">$a_row[auth_name]</a>
on $a_row[date] | <a href=\"$a_row[auth_web]\"><b><u>Download Now</u></b></a></font></td>\n";
print"</tr>\n";
print"<tr>\n";
print"<td> </td>\n";
print"</tr>\n";
print"</table>\n";
}
//links to other results(next/prev buttons)
if ($offset=1) // bypass PREV link if offset is 0
{
$prevoffset=$offset-20;
//id is in there because it is calling back to the selected section of
//the archive. If this was in a while loop I think I wold have an easier
//time calling the $id as seen in the below one I use id=$a_row[id]
print "<a href=\"$page?id=$id&offset=$prevoffset\">Previous</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);
//shows numbers inbetween Next and Prev
print "<a href=\"$page?id=$id&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=\"$page?id=$id&offset=$newoffset\">Next</a><p>\n";
}
}//end of first if(statement)
else
{
//This stuff all below is to get the Catagory names from the archive
$result=mysql_query("SELECT FROM php_cats ORDER BY title");
while($a_row=mysql_fetch_array($result))
{
$result2=mysql_query("SELECT FROM php_scripts WHERE cat_id=$a_row[id]");
$num_rows=mysql_num_rows($result2);
print"<a href=\"http://acidsun.com/$page?id=$a_row[id]\"><u>$a_row[title]</u></a> ($num_rows)<br>";
}
}
mysql_close($link);
?>