webguync Posted January 17, 2008 Share Posted January 17, 2008 Hello, I am extracting data into an HTML table and I would like to make one of the table rows alternate colors by assigning a variety of CSS classes, but I am not sure how to do that within the PHP code I am using below: any suggestions? // sending query $result = mysql_query("SELECT * FROM {$table}"); if (!$result) { die("Query to show fields from table failed"); } $fields_num = mysql_num_fields($result); echo "<h1>JBossworld 2008 Agenda: {$table}</h1>"; echo "<table border='1'><tr id='headers'>"; // printing table headers for($i=0; $i<$fields_num; $i++) { $field = mysql_fetch_field($result); echo "<td>{$field->name}</td>"; } echo "</tr>\n"; // printing table rows while($row = mysql_fetch_row($result)) { echo "<tr>"; // $row is array... foreach( .. ) puts every element // of $row to $cell variable foreach($row as $cell) echo "<td>$cell</td>"; echo "</tr>\n"; } mysql_free_result($result); ?> </table> Link to comment https://forums.phpfreaks.com/topic/86537-need-to-assign-css-class-to-tag/ Share on other sites More sharing options...
inet411 Posted January 17, 2008 Share Posted January 17, 2008 Not sure if this is the 'correct' way but I would do something like: inside of your loop put this: $tdcolor = $tdcolor +1; if ($tdcolor % 2 == 0 ){ echo "<td class='even'>"; } else { echo "<td class='odd'>"; } The if tdcolor %2 is an odd or even script, so if it is even it will show one color - odd the other. Link to comment https://forums.phpfreaks.com/topic/86537-need-to-assign-css-class-to-tag/#findComment-442155 Share on other sites More sharing options...
drummer101 Posted January 17, 2008 Share Posted January 17, 2008 edit: misread inet411 is correct $tdcolor is going to be mysql_num_rows($query) Link to comment https://forums.phpfreaks.com/topic/86537-need-to-assign-css-class-to-tag/#findComment-442161 Share on other sites More sharing options...
revraz Posted January 17, 2008 Share Posted January 17, 2008 It's a good way along with if ($color == $color1){ $color = $color2; }ELSE{ $color = $color1; } Not sure if this is the 'correct' way but I would do something like: Link to comment https://forums.phpfreaks.com/topic/86537-need-to-assign-css-class-to-tag/#findComment-442163 Share on other sites More sharing options...
webguync Posted January 17, 2008 Author Share Posted January 17, 2008 thanks, what if I want to assign more than two different CSS classes to the <td>'s though? Can that be done? Link to comment https://forums.phpfreaks.com/topic/86537-need-to-assign-css-class-to-tag/#findComment-442164 Share on other sites More sharing options...
webguync Posted January 17, 2008 Author Share Posted January 17, 2008 also, where within the current code block would I need to place $tdcolor = $tdcolor +1; if ($tdcolor % 2 == 0 ){ echo "<td class='even'>"; } else { echo "<td class='odd'>"; } Link to comment https://forums.phpfreaks.com/topic/86537-need-to-assign-css-class-to-tag/#findComment-442186 Share on other sites More sharing options...
drummer101 Posted January 17, 2008 Share Posted January 17, 2008 also, where within the current code block would I need to place where ever you're currently echoing your <td> Link to comment https://forums.phpfreaks.com/topic/86537-need-to-assign-css-class-to-tag/#findComment-442212 Share on other sites More sharing options...
Aureole Posted January 17, 2008 Share Posted January 17, 2008 <?php $tdcolor = $tdcolor +1; if ($tdcolor % 2 == 0 ){ echo "<td class='even'>"; } else { echo "<td class='odd'>"; } ?> ...can be shortened to: <?php $tdcolor += 1; echo($tdcolor % 2 == 0) ? '<td class="even">' : '<td class="odd">'; ?> Link to comment https://forums.phpfreaks.com/topic/86537-need-to-assign-css-class-to-tag/#findComment-442220 Share on other sites More sharing options...
inet411 Posted January 18, 2008 Share Posted January 18, 2008 <?php $tdcolor = $tdcolor +1; if ($tdcolor % 2 == 0 ){ echo "<td class='even'>"; } else { echo "<td class='odd'>"; } ?> ...can be shortened to: <?php $tdcolor += 1; echo($tdcolor % 2 == 0) ? '<td class="even">' : '<td class="odd">'; ?> Sorry to thread highjack but I tried your modified version of the code I wrote and it worked great. I don't understand though the ? and the : . Could you explain that as if I was a two year old? What I mean is the logic behind it..... I'm thinking the ? means if true do something : <--- (if false) something else and I'll also assume the +=1 then is the same as $whatever = $whatever + 1; Am I correct in my assumptions? Link to comment https://forums.phpfreaks.com/topic/86537-need-to-assign-css-class-to-tag/#findComment-442381 Share on other sites More sharing options...
Aureole Posted January 19, 2008 Share Posted January 19, 2008 You would be correct in your assumptions. Link to comment https://forums.phpfreaks.com/topic/86537-need-to-assign-css-class-to-tag/#findComment-443400 Share on other sites More sharing options...
resago Posted January 19, 2008 Share Posted January 19, 2008 instead of adding, I would use *-1 then you could better assign the classes, because the names would be either 1 or -1. Link to comment https://forums.phpfreaks.com/topic/86537-need-to-assign-css-class-to-tag/#findComment-443572 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.