Lambneck Posted February 18, 2009 Share Posted February 18, 2009 I have a forum type page with content being posted and I would like to create the contrasting effect like that seen on most forums where one post's subject link has a light background and the next post's link above it has a dark and then light and so on. Does anyone know how to create such an effect? Quote Link to comment Share on other sites More sharing options...
premiso Posted February 18, 2009 Share Posted February 18, 2009 PHP Operators Look at the Modulus Operator $a % $b Modulus Remainder of $a divided by $b. For every other row with for example $i as being the incrementer: for ($i=0;$i<10;$i++) { $color='black'; if (($i%2) == 0) { $color = 'red'; } echo 'The color is ' . $color . '<br />'; } Quote Link to comment Share on other sites More sharing options...
allworknoplay Posted February 18, 2009 Share Posted February 18, 2009 Put this in your TR tags.... Obviously change the 2 colors to whatever you want. Here it is black and white. $row['color'] % 2 ? "#000000" : "#FFFFFF" Quote Link to comment Share on other sites More sharing options...
Lambneck Posted February 18, 2009 Author Share Posted February 18, 2009 hey, I really appreciate the great responses, but could I get a little more detail in the explanation? Im sorry, I just dont understand ??? Quote Link to comment Share on other sites More sharing options...
premiso Posted February 18, 2009 Share Posted February 18, 2009 Modulus the % operator determines if a number, $i divided by another number, 2 has a remainder. If that number returned is == 0 then the number of $i is divisible by 2 without a remainder. So if $i is one, one dived by two has a remainder of .5, thus that number is "odd" if $i is 4, 4 divided by two has no remainder, so that would return 0 hence the number is even. Look up on Modulus via Google for more information on it. Quote Link to comment Share on other sites More sharing options...
Lambneck Posted February 18, 2009 Author Share Posted February 18, 2009 premiso thank you man, now i can better understand what your script is doing. but how to apply it to the html table row backgrounds? Quote Link to comment Share on other sites More sharing options...
premiso Posted February 18, 2009 Share Posted February 18, 2009 premiso thank you man, now i can better understand what your script is doing. but how to apply it to the html table row backgrounds? No clue, you failed to provide any section of code. I explained it for you as you asked. I am not psychic and cannot read minds (unfortunately). Quote Link to comment Share on other sites More sharing options...
limitphp Posted February 18, 2009 Share Posted February 18, 2009 or you can use this: if ($number & 1) //odd else //even Quote Link to comment Share on other sites More sharing options...
Lambneck Posted February 18, 2009 Author Share Posted February 18, 2009 just a basic table where each row would have say three columns. and a new row is generated with each form submission something like: <table> <tr> <td></td> <td></td> <td></td> </tr> </table> Quote Link to comment Share on other sites More sharing options...
premiso Posted February 18, 2009 Share Posted February 18, 2009 echo "<table>"; for ($i=0;$i<10;$i++) { $color='black'; if (($i%2) == 0) $color = 'red'; echo '<tr bgcolor="' . $color . '"> <td>The</td> <td>Color</td> <td>Is ' . $color . '</td> </tr>'; } echo "</table>"; You are still failing to provide a specific example. Hence the generic code examples. Quote Link to comment Share on other sites More sharing options...
Lambneck Posted February 19, 2009 Author Share Posted February 19, 2009 That code generates duplications of each forum topic that is posted. They shows up 10 times in the forum index. if i change for ($i=0;$i<10;$i++) { $color='black'; to for ($i=0;$i<2;$i++) { $color='black'; each topic shows up 2 times. So it looks like: Third Topic(black) Third Topic(red) Second Topic(black) Second Topic(red) First Topic(black) First Topic(red) The colors do alternate between duplications but I want to remove the duplications and have the colors alternate between each individual topic. Know what I mean? Quote Link to comment Share on other sites More sharing options...
Lambneck Posted February 19, 2009 Author Share Posted February 19, 2009 Ive changed the code up a bit but the resulting problem is still the same. Each topic displayed is repeated as many times as the value of $num Can anyone see what Im doing wrong with this? <?php $result = mysql_query("SELECT submission_id, col_1, name, submission_date FROM $table WHERE topic_id = '0' ORDER BY submission_id DESC"); if (!$result) { die("Query to show fields from table failed:".mysql_error()); } while($row = mysql_fetch_array($result)) { $sql = "SELECT * FROM $table WHERE topic_id = " . $row['submission_id']; $comments = mysql_query($sql) or die("Error ". mysql_error(). " with query ". $sql); $count = mysql_num_rows($comments); $num = mysql_numrows($result); //start loop for($i=0;$i<$num;$i++){ if($i % 2 ==0) { $class="even"; } else { $class="odd"; } echo '<tr class="' . $class . '">'; echo "<td><strong>"; echo '<a href="topic.php?id='.$row['submission_id'].'">'.stripslashes(htmlspecialchars($row['col_1'])).'</a>'; echo "</strong></td>"; echo '<td class="forumtd"><em>'; echo stripslashes(htmlspecialchars($row['name'])); echo "</em></td>"; echo '<td class="forumtd">'; echo $count; echo "</td>"; echo '<td class="forumtd">'; echo date("l M dS, Y", $row['submission_date']); echo "</td>"; echo "</tr>"; } } mysql_free_result($result); ?> Quote Link to comment Share on other sites More sharing options...
allworknoplay Posted February 19, 2009 Share Posted February 19, 2009 Ive changed the code up a bit but the resulting problem is still the same. Each topic displayed is repeated as many times as the value of $num Can anyone see what Im doing wrong with this? <?php $result = mysql_query("SELECT submission_id, col_1, name, submission_date FROM $table WHERE topic_id = '0' ORDER BY submission_id DESC"); if (!$result) { die("Query to show fields from table failed:".mysql_error()); } while($row = mysql_fetch_array($result)) { $sql = "SELECT * FROM $table WHERE topic_id = " . $row['submission_id']; $comments = mysql_query($sql) or die("Error ". mysql_error(). " with query ". $sql); $count = mysql_num_rows($comments); $num = mysql_numrows($result); //start loop for($i=0;$i<$num;$i++){ if($i % 2 ==0) { $class="even"; } else { $class="odd"; } echo '<tr class="' . $class . '">'; echo "<td><strong>"; echo '<a href="topic.php?id='.$row['submission_id'].'">'.stripslashes(htmlspecialchars($row['col_1'])).'</a>'; echo "</strong></td>"; echo '<td class="forumtd"><em>'; echo stripslashes(htmlspecialchars($row['name'])); echo "</em></td>"; echo '<td class="forumtd">'; echo $count; echo "</td>"; echo '<td class="forumtd">'; echo date("l M dS, Y", $row['submission_date']); echo "</td>"; echo "</tr>"; } } mysql_free_result($result); ?> I don't understand why you are doing that work when I gave you a one line code that will do it all... Am I just not understanding your question correctly? $color = "$class % 2 ? '#000000' : '#FFFFFF' "; echo '<tr bgcolor='".$color."'>'; Quote Link to comment Share on other sites More sharing options...
Lambneck Posted February 19, 2009 Author Share Posted February 19, 2009 allworknoplay sorry i didnt mean to ignore you at all its just that I dont understand the code you wrote so I went with what premiso suggested because it made more sense to me at the time. anyway could you kinda break down what your code is doing? what is the value of $class here? I was trying to use class for css as bgcolor i guess is depreciated. Quote Link to comment Share on other sites More sharing options...
allworknoplay Posted February 19, 2009 Share Posted February 19, 2009 allworknoplay sorry i didnt mean to ignore you at all its just that I dont understand the code you wrote so I went with what premiso suggested because it made more sense to me at the time. anyway could you kinda break down what your code is doing? what is the value of $class here? I was trying to use class for css as bgcolor i guess is depreciated. $class = "$i % 2 ? 'EVEN' : 'ODD' "; this is a one line IF conditional. $class would be your "$i" if you were using a FOR loop. Then you are basically saying, if $i is divisible by 2 then show EVEN, if not show ODD. So for your code since you are using CSS... for($i=0;$i<$num;$i++){ $class = "$i % 2 ? 'EVEN' : 'ODD' "; echo '<tr class="' . $class . '">'; echo "<td><strong>"; Now doesn't that look simpler? Quote Link to comment Share on other sites More sharing options...
Lambneck Posted February 19, 2009 Author Share Posted February 19, 2009 Ah that is an easier way to do it! Unfortunately I am still having the same problem though. Why does it make each <tr> repeat as many times as the value of $num? if the value of $num is 2 then i get a result like the following: <tr>third topic</tr> <tr>third topic</tr> <tr>second topic</tr> <tr>second topic</tr> <tr>first topic</tr> <tr>first topic</tr> Quote Link to comment Share on other sites More sharing options...
allworknoplay Posted February 19, 2009 Share Posted February 19, 2009 Ah that is an easier way to do it! Unfortunately I am still having the same problem though. Why does it make each <tr> repeat as many times as the value of $num? if the value of $num is 2 then i get a result like the following: <tr>third topic</tr> <tr>third topic</tr> <tr>second topic</tr> <tr>second topic</tr> <tr>first topic</tr> <tr>first topic</tr> I don't think there's any issue with the FOR loop, it looks fine. What does your CSS look like? Quote Link to comment Share on other sites More sharing options...
Lambneck Posted February 19, 2009 Author Share Posted February 19, 2009 Heres the rest of the code but i dont see how the css could effect how the php echo's the <tr>'s. ??? #forumtable { width:100%; } .forumtitle{ color:white; background-color:#3A3A3A; } .forumtd{ padding-left:25px; padding-right:25px; } .odd{ background-color:#F3F3F3; } .even{ background-color:white; } <table id="forumtable"> <tbody> <tr class="forumtitle"> <th><span class="">Topic</span></th> <th><span class="">Poster</span></th> <th><span class="">Comments</span></th> <th><span class="">Date</span></th> </tr> <?php include 'php/topics.php'; ?> </tbody> </table> Quote Link to comment Share on other sites More sharing options...
allworknoplay Posted February 19, 2009 Share Posted February 19, 2009 Heres the rest of the code but i dont see how the css could effect how the php echo's the <tr>'s. ??? #forumtable { width:100%; } .forumtitle{ color:white; background-color:#3A3A3A; } .forumtd{ padding-left:25px; padding-right:25px; } .odd{ background-color:#F3F3F3; } .even{ background-color:white; } <table id="forumtable"> <tbody> <tr class="forumtitle"> <th><span class="">Topic</span></th> <th><span class="">Poster</span></th> <th><span class="">Comments</span></th> <th><span class="">Date</span></th> </tr> <?php include 'php/topics.php'; ?> </tbody> </table> that looks fine. Show us your current code now that displays the double-rows.... There's something that's duplicating each row... Quote Link to comment Share on other sites More sharing options...
Lambneck Posted February 19, 2009 Author Share Posted February 19, 2009 <?php $result = mysql_query("SELECT submission_id, col_1, name, submission_date FROM $table WHERE topic_id = '0' ORDER BY submission_id DESC"); if (!$result) { die("Query to show fields from table failed:".mysql_error()); } while($row = mysql_fetch_array($result)) { $sql = "SELECT * FROM $table WHERE topic_id = " . $row['submission_id']; $comments = mysql_query($sql) or die("Error ". mysql_error(). " with query ". $sql); $count = mysql_num_rows($comments); $num = mysql_numrows($result); for($i=0;$i<$num;$i++){ $class = "$i % 2 ? 'EVEN' : 'ODD' "; echo '<tr class="' . $class . '">'; echo '<td class="forumtd"><strong>'; echo '<a href="topic.php?id='.$row['submission_id'].'">'.stripslashes(htmlspecialchars($row['col_1'])).'</a>'; echo "</strong></td>"; echo '<td class="forumtd"><em>'; echo stripslashes(htmlspecialchars($row['name'])); echo "</em></td>"; echo '<td class="forumtd">'; echo $count; echo "</td>"; echo '<td class="forumtd">'; echo date("l M dS, Y", $row['submission_date']); echo "</td>"; echo "</tr>"; } } mysql_free_result($result); ?> Quote Link to comment Share on other sites More sharing options...
allworknoplay Posted February 19, 2009 Share Posted February 19, 2009 <?php $result = mysql_query("SELECT submission_id, col_1, name, submission_date FROM $table WHERE topic_id = '0' ORDER BY submission_id DESC"); if (!$result) { die("Query to show fields from table failed:".mysql_error()); } while($row = mysql_fetch_array($result)) { $sql = "SELECT * FROM $table WHERE topic_id = " . $row['submission_id']; $comments = mysql_query($sql) or die("Error ". mysql_error(). " with query ". $sql); $count = mysql_num_rows($comments); $num = mysql_numrows($result); for($i=0;$i<$num;$i++){ $class = "$i % 2 ? 'EVEN' : 'ODD' "; echo '<tr class="' . $class . '">'; echo '<td class="forumtd"><strong>'; echo '<a href="topic.php?id='.$row['submission_id'].'">'.stripslashes(htmlspecialchars($row['col_1'])).'</a>'; echo "</strong></td>"; echo '<td class="forumtd"><em>'; echo stripslashes(htmlspecialchars($row['name'])); echo "</em></td>"; echo '<td class="forumtd">'; echo $count; echo "</td>"; echo '<td class="forumtd">'; echo date("l M dS, Y", $row['submission_date']); echo "</td>"; echo "</tr>"; } } mysql_free_result($result); ?> Ok you have to issues. 1) you have a FOR loop within a WHILE loop. 2) Don't nest your select queries within a loop!!! Let me try to fix this for you.. Quote Link to comment Share on other sites More sharing options...
allworknoplay Posted February 19, 2009 Share Posted February 19, 2009 Wait a minute, you have two queries here, what are you trying to do exactly? Quote Link to comment Share on other sites More sharing options...
Lambneck Posted February 19, 2009 Author Share Posted February 19, 2009 the second query is for a counter that displays how many comments are made to each topic Quote Link to comment Share on other sites More sharing options...
allworknoplay Posted February 19, 2009 Share Posted February 19, 2009 Try this code, it may not work, but try it anyways... <?php $result = mysql_query("SELECT submission_id, col_1, name, submission_date FROM $table WHERE topic_id = '0' ORDER BY submission_id DESC"); if (!$result) { die("Query to show fields from table failed:".mysql_error()); } $i = 0; while($row = mysql_fetch_array($result)) { $sql = "SELECT * FROM $table WHERE topic_id = " . $row['submission_id']; $comments = mysql_query($sql) or die("Error ". mysql_error(). " with query ". $sql); $count = mysql_num_rows($comments); $num = mysql_num_rows($result); $class = "$i % 2 ? 'EVEN' : 'ODD' "; echo '<tr class="' . $class . '">'; echo '<td class="forumtd"><strong>'; echo '<a href="topic.php?id='.$row['submission_id'].'">'.stripslashes(htmlspecialchars($row['col_1'])).'</a>'; echo "</strong></td>"; echo '<td class="forumtd"><em>'; echo stripslashes(htmlspecialchars($row['name'])); echo "</em></td>"; echo '<td class="forumtd">'; echo $count; echo "</td>"; echo '<td class="forumtd">'; echo date("l M dS, Y", $row['submission_date']); echo "</td>"; echo "</tr>"; $i++; } mysql_free_result($result); ?> Quote Link to comment Share on other sites More sharing options...
Lambneck Posted February 19, 2009 Author Share Posted February 19, 2009 Nope, the table and its contents load perfectly but the background colors are not there. :-\ Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.