Alternating row color in mysql output

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

aussie_clint
Forum Commoner
Posts: 41
Joined: Mon Jul 31, 2006 9:14 am
Location: Brisbane, Australia
Contact:

Alternating row color in mysql output

Post 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]
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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);
aussie_clint
Forum Commoner
Posts: 41
Joined: Mon Jul 31, 2006 9:14 am
Location: Brisbane, Australia
Contact:

Post 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();
?>
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

you need to escape the quotes is the printf() functions
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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);
aussie_clint
Forum Commoner
Posts: 41
Joined: Mon Jul 31, 2006 9:14 am
Location: Brisbane, Australia
Contact:

Post 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();
?>
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Check the closing <tr> tag in this line

Code: Select all

$table.=printf('<tr bgcolor="%s"><td>'.$row['Description'].'</td>/tr>', $rowcolor);
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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:
aussie_clint
Forum Commoner
Posts: 41
Joined: Mon Jul 31, 2006 9:14 am
Location: Brisbane, Australia
Contact:

Post by aussie_clint »

i fixed that error and im still getting the same result
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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>";
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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
aussie_clint
Forum Commoner
Posts: 41
Joined: Mon Jul 31, 2006 9:14 am
Location: Brisbane, Australia
Contact:

Post 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();
?>
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Change printf to sprintf
aussie_clint
Forum Commoner
Posts: 41
Joined: Mon Jul 31, 2006 9:14 am
Location: Brisbane, Australia
Contact:

Post 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();
?>
Post Reply