ifusion Posted March 25, 2008 Share Posted March 25, 2008 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. <?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 More sharing options...
cunoodle2 Posted March 25, 2008 Share Posted March 25, 2008 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 Link to comment https://forums.phpfreaks.com/topic/97754-php-loop-how-to-style-every-second-row/#findComment-500178 Share on other sites More sharing options...
ifusion Posted March 25, 2008 Author Share Posted March 25, 2008 Cheers for that but i can't quite work it out? How does it know if it is -1 or not etc? Link to comment https://forums.phpfreaks.com/topic/97754-php-loop-how-to-style-every-second-row/#findComment-500183 Share on other sites More sharing options...
ifusion Posted March 25, 2008 Author Share Posted March 25, 2008 This what i've done but all the rows are white?: <?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> Link to comment https://forums.phpfreaks.com/topic/97754-php-loop-how-to-style-every-second-row/#findComment-500188 Share on other sites More sharing options...
cunoodle2 Posted March 25, 2008 Share Posted March 25, 2008 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 Link to comment https://forums.phpfreaks.com/topic/97754-php-loop-how-to-style-every-second-row/#findComment-500192 Share on other sites More sharing options...
ifusion Posted March 25, 2008 Author Share Posted March 25, 2008 Your a legend mate! Cheers! Link to comment https://forums.phpfreaks.com/topic/97754-php-loop-how-to-style-every-second-row/#findComment-500198 Share on other sites More sharing options...
cunoodle2 Posted March 25, 2008 Share Posted March 25, 2008 Please mark this topic as solved Link to comment https://forums.phpfreaks.com/topic/97754-php-loop-how-to-style-every-second-row/#findComment-500202 Share on other sites More sharing options...
wildteen88 Posted March 25, 2008 Share Posted March 25, 2008 You could just use the modulus (%) operator instead: $i = 0; while($i < 10) { $color = ($i%2) ? 'red' : 'green'; echo '<span style="color:'.$color.'">Line #' . ($i+1) . '</span><br />'; $i++; } Link to comment https://forums.phpfreaks.com/topic/97754-php-loop-how-to-style-every-second-row/#findComment-500212 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.