Page 1 of 2

Alternating row color in mysql output

Posted: Tue Aug 01, 2006 8:46 am
by aussie_clint
Pimptastic | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


hi

i started off with a script wich i got working yesterday thanks to the grate people on this net work, now im wanting to have alternationg row colors.

this here workes fine:

Code: Select all

<?PHP
//connect to the mysql database
$connection=mysql_connect("localhost","computer_test","test1");
mysql_select_db("computer_price",$connection);
$query="SELECT p.PartNumber, p.Description, (p.IncTax*1.3) AS Price FROM `price list` p";
$data_resource=mysql_query($query,$connection);

//Defining HTML table
$table="<table cellspacing=0 cellpadding=0 border=1 width=400>";
      while ($row = mysql_fetch_array($data_resource)) {
$table.="<tr>";
$table.="<td>".$row['PartNumber']."</td>";
$table.="<td>".$row['Description']."</td>";
$table.="<td>".$row['Price']."</td>";
$table.="</tr>";
$row_count++;
      }
//Closing HTML table
$table.="</table>";
//Displying table and closing mysql database if not need 
echo $table;
mysql_close();
?>
now im trying to implement

Code: Select all

$row1 = "#cfdafg";
$row2 = "#ff0000";
$rowcolor = $row1;

while ($myrow = mysql_fetch_array($result)) {
printf'<tr bgcolor="%s"><td>whateverdata</td></tr>', $rowcolor);
if ($rowcolor == $row1) $rowcolor = $row2;
if ($rowcolor == $row2) $rowcolor = $row1;
}
and now this is my attempt to do so, but its not happening

Code: Select all

<?PHP
//connect to the mysql database
$connection=mysql_connect("localhost","computer_test","test1");
mysql_select_db("computer_price",$connection);
$query="SELECT p.PartNumber, p.Description, (p.IncTax*1.3) AS Price FROM `price list` p";
$data_resource=mysql_query($query,$connection);

$row1 = "#cfdafg";
$row2 = "#ff0000";
$rowcolor = $row1;


//Defining HTML table
$table="<table cellspacing=0 cellpadding=0 border=1 width=400>";
      while ($row = mysql_fetch_array($data_resource)) {
$table.="<tr>";
$table.="<tr bgcolor="%s"><td>".$row['PartNumber']."</td></tr>", $rowcolor;
$table.="<tr bgcolor="%s"><td>".$row['Description']."</td>/tr>", $rowcolor;
$table.="<tr bgcolor="%s"><td>".$row['Price']."</td></tr>", $rowcolor;
$table.="</tr>";
if ($rowcolor == $row1) $rowcolor = $row2;
if ($rowcolor == $row2) $rowcolor = $row1;

      }
//Closing HTML table
$table.="</table>";
//Displying table and closing mysql database if not need 
echo $table;
mysql_close();
?>
can anyone help?

thanks
clint


Pimptastic | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Tue Aug 01, 2006 8:49 am
by JayBird
Change line like this

Code: Select all

$table.="<tr bgcolor="%s"><td>".$row['PartNumber']."</td></tr>", $rowcolor;
to

Code: Select all

$table.=printf("<tr bgcolor="%s"><td>".$row['PartNumber']."</td></tr>", $rowcolor);

Posted: Tue Aug 01, 2006 9:03 am
by aussie_clint
thanks, but there is still something rong in the code, it come up in line 16

Code: Select all

<?PHP
//connect to the mysql database
$connection=mysql_connect("localhost","computer_test","test1");
mysql_select_db("computer_price",$connection);
$query="SELECT p.PartNumber, p.Description, (p.IncTax*1.3) AS Price FROM `price list` p";
$data_resource=mysql_query($query,$connection);

$row1 = "#cfdafg";
$row2 = "#ff0000";
$rowcolor = $row1;

//Defining HTML table
$table="<table cellspacing=0 cellpadding=0 border=1 width=400>";
      while ($row = mysql_fetch_array($data_resource)) {
$table.="<tr>";
$table.=printf("<tr bgcolor="%s"><td>".$row['PartNumber']."</td></tr>", $rowcolor); //line16
$table.=printf("<tr bgcolor="%s"><td>".$row['Description']."</td>/tr>", $rowcolor);
$table.=printf("<tr bgcolor="%s"><td>".$row['Price']."</td></tr>", $rowcolor);
$table.="</tr>";
if ($rowcolor == $row1) $rowcolor = $row2;
if ($rowcolor == $row2) $rowcolor = $row1;

      }
//Closing HTML table
$table.="</table>";
//Displying table and closing mysql database if not need 
echo $table;
mysql_close();
?>

Posted: Tue Aug 01, 2006 9:04 am
by JayBird
you need to escape the quotes is the printf() functions

Posted: Tue Aug 01, 2006 9:17 am
by RobertGonzalez
You need to escape your quotes, always, when they are inside other quotes of the same type (with the exception of certain regex matching, I think).

Code: Select all

$table.=printf("<tr bgcolor=\"%s\"><td>".$row['Price']."</td></tr>", $rowcolor);
Alternatively,

Code: Select all

$table.=printf('<tr bgcolor="%s"><td>'.$row['Price'].'</td></tr>', $rowcolor);

Posted: Tue Aug 01, 2006 9:28 am
by aussie_clint
i am realy new to this, sorry people's

i just edited my script and it just did a output of everything, one after the other, not in a table or any color


ZD750MZIP 750MB ZIP DISK/tr>25.74ZD750IIOMEGA 750MB INTERNAL ZIP DRIVE/tr>217.36ZD750EIOMEGA 750MB EXTERNAL ZIP DRIVE USB/tr>371.80ZD250MZIP....


Code: Select all

<?PHP
//connect to the mysql database
$connection=mysql_connect("localhost","computer_test","test1");
mysql_select_db("computer_price",$connection);
$query="SELECT p.PartNumber, p.Description, (p.IncTax*1.3) AS Price FROM `price list` p";
$data_resource=mysql_query($query,$connection);

$row1 = "#cfdafg";
$row2 = "#ff0000";
$rowcolor = $row1;

//Defining HTML table
$table="<table cellspacing=0 cellpadding=0 border=1 width=400>";
      while ($row = mysql_fetch_array($data_resource)) {
$table.="<tr>";
$table.=printf('<tr bgcolor="%s"><td>'.$row['PartNumber'].'</td></tr>', $rowcolor);
$table.=printf('<tr bgcolor="%s"><td>'.$row['Description'].'</td>/tr>', $rowcolor);
$table.=printf('<tr bgcolor="%s"><td>'.$row['Price'].'</td></tr>', $rowcolor);
$table.="</tr>";
if ($rowcolor == $row1) $rowcolor = $row2;
if ($rowcolor == $row2) $rowcolor = $row1;

      }
//Closing HTML table
$table.="</table>";
//Displying table and closing mysql database if not need 
echo $table;
mysql_close();
?>

Posted: Tue Aug 01, 2006 9:30 am
by JayBird
Check the closing <tr> tag in this line

Code: Select all

$table.=printf('<tr bgcolor="%s"><td>'.$row['Description'].'</td>/tr>', $rowcolor);

Posted: Tue Aug 01, 2006 9:33 am
by RobertGonzalez
Excellent Everah. Pick a line to copy and pick the one that has a freaking HTML error in it. Boy, I'll tell you, I can be a real genius sometimes... :oops:

Posted: Tue Aug 01, 2006 9:45 am
by aussie_clint
i fixed that error and im still getting the same result

Posted: Tue Aug 01, 2006 9:47 am
by JayBird
hmmm...

why are you starting a table row, then starring another

Code: Select all

$table.="<tr>";
$table.=printf('<tr bgcolor="%s"><td>'.$row['PartNumber'].'</td></tr>', $rowcolor);
$table.=printf('<tr bgcolor="%s"><td>'.$row['Description'].'</td></tr>', $rowcolor);
$table.=printf('<tr bgcolor="%s"><td>'.$row['Price'].'</td></tr>', $rowcolor);
$table.="</tr>";
remove

Code: Select all

$table.="<tr>";
and

Code: Select all

$table.="</tr>";

Posted: Tue Aug 01, 2006 9:50 am
by feyd
I'd shift the concatenation out of the control string being sent to printf() and instead make it apart of the data inserted.

Also, printf() directly prints, not returns the resulting string. sprintf() does that.

Posted: Tue Aug 01, 2006 9:51 am
by JayBird
feyd wrote:Also, printf() directly prints, not returns the resulting string. sprintf() does that.
You know what, he's right :wink:

Missed that bad boy

Posted: Tue Aug 01, 2006 10:04 am
by aussie_clint
i must look like a idot here, but once i get this to work i will be ok, as this is the only php script i will be needing for now

im not sure how to do what fedy is saying, if some one can show me that would be grate

but i removed the $table.="<tr>"; line and </tr>, but im still getting all the results one after another

here is what i got at the moment

Code: Select all

<?PHP
//connect to the mysql database
$connection=mysql_connect("localhost","computer_test","test1");
mysql_select_db("computer_price",$connection);
$query="SELECT p.PartNumber, p.Description, (p.IncTax*1.3) AS Price FROM `price list` p";
$data_resource=mysql_query($query,$connection);

$row1 = "#cfdafg";
$row2 = "#ff0000";
$rowcolor = $row1;

//Defining HTML table
$table="<table cellspacing=0 cellpadding=0 border=1 width=400>";
      while ($row = mysql_fetch_array($data_resource)) {
$table.=printf('<tr bgcolor="%s"><td>'.$row['PartNumber'].'</td></tr>', $rowcolor);
$table.=printf('<tr bgcolor="%s"><td>'.$row['Description'].'</td></tr>', $rowcolor);
$table.=printf('<tr bgcolor="%s"><td>'.$row['Price'].'</td></tr>', $rowcolor);
if ($rowcolor == $row1) $rowcolor = $row2;
if ($rowcolor == $row2) $rowcolor = $row1;

      }
//Closing HTML table
$table.="</table>";
//Displying table and closing mysql database if not need 
echo $table;
mysql_close();
?>

Posted: Tue Aug 01, 2006 10:06 am
by JayBird
Change printf to sprintf

Posted: Tue Aug 01, 2006 10:17 am
by aussie_clint
ok, its out putting it into a table now :) , but all in one colum

eg

row1
row2
row3
row1
row2
row3

Code: Select all

<?PHP
//connect to the mysql database
$connection=mysql_connect("localhost","computer_test","test1");
mysql_select_db("computer_price",$connection);
$query="SELECT p.PartNumber, p.Description, (p.IncTax*1.3) AS Price FROM `price list` p";
$data_resource=mysql_query($query,$connection);

$row1 = "#cfdafg";
$row2 = "#ff0000";
$rowcolor = $row1;

//Defining HTML table
$table="<table cellspacing=0 cellpadding=0 border=1 width=400>";
      while ($row = mysql_fetch_array($data_resource)) {
$table.=sprintf('<tr bgcolor="%s"><td>'.$row['PartNumber'].'</td></tr>', $rowcolor);
$table.=sprintf('<tr bgcolor="%s"><td>'.$row['Description'].'</td></tr>', $rowcolor);
$table.=sprintf('<tr bgcolor="%s"><td>'.$row['Price'].'</td></tr>', $rowcolor);
if ($rowcolor == $row1) $rowcolor = $row2;
if ($rowcolor == $row2) $rowcolor = $row1;

      }
//Closing HTML table
$table.="</table>";
//Displying table and closing mysql database if not need 
echo $table;
mysql_close();
?>