[SOLVED] How to show evry other row in diferent color

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

Post Reply
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

[SOLVED] How to show evry other row in diferent color

Post by ddragas »

How to show evry other row in diferent color

colors should be:

#CCCC99 and #CCCCCC

Code: Select all

<?php

include("con_db.php");

$query_count2 = mysql_query("select city, mjesto, count(*) as 'broj_iznajmljivaca' 
from apartmani
where Zupanija = '$zupanija_smjestaja' and vrsta_smjestaja='$vrsta'  and kategorija='$kategorija'
group by city, mjesto
order by city ") or
die (mysql_error());


//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
			while ($result_row2 = mysql_fetch_assoc($query_count2)){
			$totalrows2 = $result_row2['broj_iznajmljivaca']; 
			$grad = $result_row2["city"];
			$mjesto =  $result_row2["mjesto"];
			
		
			echo "<table border = 1 table width="100%">"; 
			if($vrsta=="apartman"){
			echo "<td width="40%"align="left">" . $font .  $grad  . " (" . $mjesto. ")" . "</td><td width="20%"align="center">" . $font . '<a href="result_app.php?grad='.urlencode($grad).'&mjesto=' . urlencode($mjesto) .'&vrsta=' . urlencode($vrsta) .'">' .  $totalrows2 .  '</a>' . "</td>";}
			if($vrsta=="soba"){
			echo "<td width="40%"align="left">" . $font .  $grad  . " (" . $mjesto. ")" . "</td><td width="20%"align="center">" . $font . '<a href="result_soba.php?grad='.urlencode($grad).'&mjesto=' . urlencode($mjesto) .'&vrsta=' . urlencode($vrsta) .'">' .  $totalrows2 .  '</a>' . "</td>";}
			if($vrsta=="agroturizam"){
			echo "<td width="40%"align="left">" . $font .  $grad  . " (" . $mjesto. ")" . "</td><td width="20%"align="center">" . $font . '<a href="result_agroturizam.php?grad='.urlencode($grad).'&mjesto=' . urlencode($mjesto) .'&vrsta=' . urlencode($vrsta) .'">' .  $totalrows2 .  '</a>' . "</td>";}
			echo "</table>"; 
			}
mysql_free_result($query_count2);
mysql_close($conn); 


<?php

?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

something like

Code: Select all

<?php

include("con_db.php");

$query_count2 = mysql_query("select city, mjesto, count(*) as 'broj_iznajmljivaca' 
from apartmani
where Zupanija = '$zupanija_smjestaja' and vrsta_smjestaja='$vrsta'  and kategorija='$kategorija'
group by city, mjesto
order by city ") or
die (mysql_error());


//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
			$color = '#CCCC99';
			while ($result_row2 = mysql_fetch_assoc($query_count2)){
			$totalrows2 = $result_row2['broj_iznajmljivaca']; 
			$grad = $result_row2["city"];
			$mjesto =  $result_row2["mjesto"];
			
		
			echo "<table border = 1 table width="100%" bgcolor="$color">"; 
			if($vrsta=="apartman"){
			echo "<td width="40%"align="left">" . $font .  $grad  . " (" . $mjesto. ")" . "</td><td width="20%"align="center">" . $font . '<a href="result_app.php?grad='.urlencode($grad).'&mjesto=' . urlencode($mjesto) .'&vrsta=' . urlencode($vrsta) .'">' .  $totalrows2 .  '</a>' . "</td>";}
			if($vrsta=="soba"){
			echo "<td width="40%"align="left">" . $font .  $grad  . " (" . $mjesto. ")" . "</td><td width="20%"align="center">" . $font . '<a href="result_soba.php?grad='.urlencode($grad).'&mjesto=' . urlencode($mjesto) .'&vrsta=' . urlencode($vrsta) .'">' .  $totalrows2 .  '</a>' . "</td>";}
			if($vrsta=="agroturizam"){
			echo "<td width="40%"align="left">" . $font .  $grad  . " (" . $mjesto. ")" . "</td><td width="20%"align="center">" . $font . '<a href="result_agroturizam.php?grad='.urlencode($grad).'&mjesto=' . urlencode($mjesto) .'&vrsta=' . urlencode($vrsta) .'">' .  $totalrows2 .  '</a>' . "</td>";}
			echo "</table>"; 
			$color = ($color === '#CCCC99' ? '#CCCCCC' : '#CCCC99');
			}
mysql_free_result($query_count2);
mysql_close($conn); 


?>
Additionally, you aren't putting table rows in there. In fact, you're writing out several individual tables..
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

Post by ddragas »

Thank you
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

Post by ddragas »

you're rigt

I'm making two individual tables. What am I doing wrong?

I want to make one table, and make rows in that table


Here is complete code

Code: Select all

<?php

include("con_db.php");

$query_count2 = mysql_query("select city, mjesto, count(*) as 'broj_iznajmljivaca' 
from apartmani
where Zupanija = '$zupanija_smjestaja' and vrsta_smjestaja='$vrsta'  and kategorija='$kategorija'
group by city, mjesto
order by city ") or
die (mysql_error());


//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
			$color = '#CCCC99'; 
			while ($result_row2 = mysql_fetch_assoc($query_count2)){
			$totalrows2 = $result_row2['broj_iznajmljivaca']; 
			$grad = $result_row2["city"];
			$mjesto =  $result_row2["mjesto"];
			
			$color = ($color === '#CCCC99' ? '#CCCCCC' : '#CCCC99');
			echo "<table border = -1 table width="100%">"; 
			if($vrsta=="apartman"){
			echo "<td width="40%"align="left" bgcolor= "  . $color  . ">" . $font .  $grad  . " (" . $mjesto. ")" . "</td><td width="20%"align="center" bgcolor= "  . $color  . ">" . $font . '<a href="result_app.php?grad='.urlencode($grad).'&mjesto=' . urlencode($mjesto) .'&vrsta=' . urlencode($vrsta) .'">' .  $totalrows2 .  '</a>' . "</td></tr>";}
			echo "</table>"; 
	//		$color = ($color === '#CCCC99' ? '#CCCCCC' : '#CCCC99');
			}
mysql_free_result($query_count2);
mysql_close($conn); 

?>

<?php

?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

move the table echo outside the while loop, add an echo for <tr></tr> around the echo(s) still inside the while loop.
Post Reply