Jump to content

PHP loop - How to style every second row?


ifusion

Recommended Posts

Hi,

 

I've created a HTML table that loops the data from my mysql database. I want the first row to be blue and the second row to be white for example, like the picture below using CSS to style it.

 

tables0272nh1.gif

 

<?php


$mycon = mysql_connect('localhost', 'ifusion', 'sweet1') or die(mysql_error());
$db_con = mysql_select_db('kieran') or die(mysql_error());

if($mycon)
{
echo "Connected 2 mysql";
}

if($db_con)
{
	echo "Connected 2 db";
}

$result = mysql_query("SELECT id, title, budget, url FROM jobs") or die(mysql_error());
while($row = mysql_fetch_array($result)){

 }
?>

<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css" />

</head>

<body>
<?php echo $result = mysql_query("SELECT id, title, budget, url FROM jobs") or die(mysql_error());

$counter = 1;

echo '<table border="0" id="maint">';
echo '<tr>';
echo '<td>Number</td>';
echo '<td>Title</td>';
echo '<td>Budget</td>';
echo '<td>Url</td>';
echo '</tr>';
while($row = mysql_fetch_array($result)){ 	
echo '<tr>';
echo "<td>$counter.</td>";
echo "<td>{$row['title']}</td>";
echo "<td>{$row['budget']}</td>";
echo "<td>{$row['url']}</td>";
echo '</tr>';



$counter++;

}
echo '</table>';
?>

</body>

</html>

Link to comment
https://forums.phpfreaks.com/topic/97754-php-loop-how-to-style-every-second-row/
Share on other sites

I'm only adding a few lines of code right in the middle of yours...

 

echo '</tr>';
$int = 1;  //added by cunoodle2

while($row = mysql_fetch_array($result))
{
	$int = $int * -1;  //added by cunoodle2

	if($int == -1)  //added by cunoodle2
	 	echo "white line";  //added by cunoodle2
	else  //added by cunoodle2
	 	echo "blue line";  //added by cunoodle2
echo '<tr>';
echo "<td>$counter.</td>";

 

You will ofcourse have to modify where in your code that goes but it will do the trick

This what i've done but all the rows are white?:

 

tableznw2.jpg

 

<?php


$mycon = mysql_connect('localhost', 'ifusion', 'sweet1') or die(mysql_error());
$db_con = mysql_select_db('kieran') or die(mysql_error());

if($mycon)
{
echo "Connected 2 mysql";
}

if($db_con)
{
	echo "Connected 2 db";
}

$result = mysql_query("SELECT id, title, budget, url FROM jobs") or die(mysql_error());
while($row = mysql_fetch_array($result)){

 }
?>

<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css" />

</head>

<body>
<?php echo $result = mysql_query("SELECT id, title, budget, url FROM jobs") or die(mysql_error());

$counter = 1;

echo '<table border="0" id="maint">';
echo '<tr>';
echo '<td>Number</td>';
echo '<td>Title</td>';
echo '<td>Budget</td>';
echo '<td>Url</td>';
echo '</tr>';


while($row = mysql_fetch_array($result)){ 	

$int = 1;

$int = $int * -1;

if($int == -1)
{
	echo '<tr>';
	echo "<td class='white'>$counter.</td>";
	echo "<td class='white'>{$row['title']}</td>";
	echo "<td class='white'>{$row['budget']}</td>";
	echo "<td class='white'>{$row['url']}</td>";
	echo '</tr>';
}
else {
	echo '<tr>';
	echo "<td class='blue'>$counter.</td>";
	echo "<td class='blue'>{$row['title']}</td>";
	echo "<td class='blue'>{$row['budget']}</td>";
	echo "<td class='blue'>{$row['url']}</td>";
	echo '</tr>';
}


$counter++;

}
echo '</table>';
?>

</body>


</html>

A few things..

 

1.  You need to set $int to 1 BEFORE the while loop like this...

 

<?php
$int = 1;

while($row = mysql_fetch_array($result))
{ 	

$int = $int * -1;
?>

 

Now lets trim down lots of this code in the body of the loop...

<?php
if($int == -1)
	$color = "white";
else
	$color = "blue";

echo '<tr>';
echo "<td class='$color'>$counter.</td>";
echo "<td class='$color'>{$row['title']}</td>";
echo "<td class='$color'>{$row['budget']}</td>";
echo "<td class='$color'>{$row['url']}</td>";
echo '</tr>';
}
?>

 

Try that out

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.