Jump to content

[SOLVED] alternating table background color


Lambneck

Recommended Posts

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?

Link to comment
Share on other sites

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 />';
}

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

 

Link to comment
Share on other sites

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);
?>

 

 

Link to comment
Share on other sites

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."'>';

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

 

 

 

 

 

 

 

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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?

 

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

<?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);
?>

Link to comment
Share on other sites

<?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..

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

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.