Forum Moderators: coopster

Message Too Old, No Replies

PHP/MYSQL database results problem

rowcount,alternating row color

         

1planetone

9:12 pm on May 14, 2006 (gmt 0)

10+ Year Member



Hi there,
I am having little problem while I try to retrieve
data from mysql table, everything works fine but the
first row of the table display empty, here is how it is look like:

----------------------------------
empty ¦ results ok
----------------------------------
results ok ¦ results ok
----------------------------------
results ok ¦ results ok
----------------------------------
results ok ¦ results ok
----------------------------------
results ok ¦ results ok
----------------------------------

here is my code

<?php
include_once ("dbase/mysql_connect.php");
?>

<table>

<?php

$limit = 25;
$query_count = "SELECT * FROM Rb_fiftyMl_bottle";
$result_count = mysql_query($query_count);
$totalrows = mysql_num_rows($result_count);

if(empty($page)){
$page = 1;
}

$limitvalue = $page * $limit - ($limit);
$query = "SELECT * FROM Rb_fiftyMl_bottle LIMIT $limitvalue, $limit";
$result = mysql_query($query) or die("Error: " . mysql_error());

if(mysql_num_rows($result) == 0){
echo("Nothing to Display!");
}

$bgcolor = "#CCCCCC"; // light gray

echo("<center><table>");

while($row = mysql_fetch_array($result)){
if ($bgcolor == "#CCCCCC"){
$bgcolor = "#FFF4CA";
}else{
$bgcolor = "#CCCCCC";
$product_name = $row['product_name'];
$product_id = $row['product_id'];
}

echo("<tr bgcolor=".$bgcolor.">");
echo("<td><a href=\"products_details.php?product_id=$product_id\">$product_name</a></td>");
echo("<td>");
echo($row["product_price"]);
echo("</td></tr>");
}

echo("</table>");

if($page!= 1){
$pageprev = $page-1;

echo("<a href=\"$PHP_SELF?page=$pageprev\">&larr;</a>&nbsp;");
}else{
echo("&larr; &nbsp;");
}

$numofpages = $totalrows / $limit;

for($i = 1; $i <= $numofpages; $i++){
if($i == $page){
echo($i."&nbsp;");
}else{
echo("<a href=\"$PHP_SELF?page=$i\">$i</a>&nbsp;");
}
}

if(($totalrows % $limit)!= 0){
if($i == $page){
echo($i."&nbsp;");
}else{
echo("<a href=\"$PHP_SELF?page=$i\">$i</a>&nbsp;");
}
}

if(($totalrows - ($limit * $page)) >= 0){
$pagenext = $page+1;

echo("<a href=\"$PHP_SELF?page=$pagenext\">&rarr;</a>");
}else{
echo("&rarr;");
}

mysql_free_result($result);

?>

</table>

eelixduppy

10:00 pm on May 14, 2006 (gmt 0)



Welcome to WebmasterWorld!

Your problem lies within the following code:


while($row = mysql_fetch_array($result)){
if ($bgcolor == "#CCCCCC"){
$bgcolor = "#FFF4CA";
}else{
$bgcolor = "#CCCCCC";
$product_name = $row['product_name'];
$product_id = $row['product_id'];
}

echo("<tr bgcolor=".$bgcolor.">");
echo("<td><a href=\"products_details.php?product_id=$product_id\">$product_name</a></td>");
echo("<td>");
echo($row["product_price"]);
echo("</td></tr>");
}

You initially set $bgcolor to "#CCCCCC" which makes the if statement true, therefore not giving $product_name and $product_id any values (both of which are printed out to the screen later). To fix this, you have to change the way you set the background color, or you have to include the variable declarations in that statement also. This is what i would do:


$count = 0;
while($row = mysql_fetch_array($result)){
if (($count % 2) == 0){
$bgcolor = "#FFF4CA";
}else{
$bgcolor = "#CCCCCC";
}

$product_name = $row['product_name'];
$product_id = $row['product_id'];

echo("<tr bgcolor=".$bgcolor.">");
echo("<td><a href=\"products_details.php?product_id=$product_id\">$product_name</a></td>");
echo("<td>");
echo($row["product_price"]);
echo("</td></tr>");
$count++;
}

good luck!

eelix

1planetone

10:18 pm on May 14, 2006 (gmt 0)

10+ Year Member



Thanks! eelixduppy,

t'was Brilliant, everything is working fine.

thanks a lot