Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.