Arrays start at 0 not 1
for($i = 1; $i <= $total_pages; $i++)
for($i = 0; $i <= $total_pages; $i++)
Morning,
I am using a script that was posted on WMW a while ago but the problem I have is that the first result is always missing.
<?
$db = mysql_connect("localhost",$username,$password) or die("Couldn't connect to the database.");
mysql_select_db($database) or die("Couldn't select the database");
/* Set current, prev and next page */
$page = (!isset($_GET['page']))? 1 : $_GET['page'];
$prev = ($page - 1);
$next = ($page + 1);
/* Max results per page */
$max_results = 15;
/* Calculate the offset */
$from = (($page * $max_results) - $max_results);
/* Query the db for to
tal results. You need to edit the sql to fit your needs */
$result = mysql_query("SELECT * from products where brand='$brand' and category='$category'");
$total_results = mysql_num_rows($result);
$total_pages = ceil($total_results / $max_results);
$pagination = '';
/* Create a PREV link if there is one */
if($page > 1)
{
$pagination .= "<a href=\"review$brand-$category-$prev.htm\"class='maintext'>Previous</a>";
}
/* Loop through the total pages */
for($i = 1; $i <= $total_pages; $i++)
{
if(($page) == $i)
{
$pagination .= $i;
}
else
{
$pagination .= "<a href=\"review$brand-$category-$i.htm\" class='maintext'>$i</a>";
}
}
/* Print NEXT link if there is one */
if($page < $total_pages)
{
$pagination .= "<a href=\"review$brand-$category-$next.htm\" class='maintext'>Next</a>";
}
$result=mysql_query("SELECT * from products where brand='$brand' and category='$category' order by product ASC LIMIT $from, $max_results ");
while ($i = mysql_fetch_array($result))
{
echo "<table width='100%' class='maintext'>";
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
echo "results here";
}
echo "</table>";
}
?>
I think that my first result is getting put into this line: while ($i = mysql_fetch_array($result)) but am not sure how to overcome it.
Thanks in advance for any help
Brett
Brett
Arrays start at 0 not 1
for($i = 1; $i <= $total_pages; $i++)
for($i = 0; $i <= $total_pages; $i++)
That for statement is ok as you want the first page to be page 1 and its not an array. The problem is here:-The first mysql_fetch_array grabs the first product but doesn't dispay it.PHP Code:$result=mysql_query("SELECT * from products where brand='$brand' and category='$category' order by product ASC LIMIT $from, $max_results ");
while ($i = mysql_fetch_array($result))
{
echo "<table width='100%' class='maintext'>";
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
echo "results here";
}
echo "</table>";
}
ChangetoPHP Code:while ($i = mysql_fetch_array($result))
{
and that should sort it out.PHP Code:if ($result!==false) {
Haha lol what was I thinking![]()
I don't get why you would need that line at all wouldn't you just output the start and end of your table outside the loop anyway.
And as Rich said above check a result returned
Code:if(mysql_num_rows($result) != 0){ // Loop through results }else{ echo("Naff all there!"); }
Thanks Rich,Originally Posted by Rich
That worked a treat - I thought the first mysql_fetch_array was the culprit - just didn't know what to do instead!
Thanks futureweb as well.
Brett
Brett
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks