Jump to content

Recommended Posts

Hi,

 

I posted an earlier article on the CSS forum section but have come to realise the problem is not CSS related and the solution was through PHP so i have given it a go and still seem to have no alternate colors.

 

Can anyone see where i am going wrong with the following code:

 

            <?php

		require_once ('../mysql_connect.php');

		echo ('<table width="294" height="150" border="0" />
			  <thead> 
				<tr>
				<th scope="col">First Name</th>
				<th scope="col">Post Code</th>
				<th scope="col">Team Supported</th>
				</tr>
			  </thead>	
				');

		$color1 = "#FFF"; 
		$color2 = "#CCC"; 
		$row_count = 0; 

		$result = mysql_query("SELECT first_name, post_code, team_supported FROM users ORDER BY user_id DESC LIMIT 5") or die (mysql_error());

		while($row = mysql_fetch_assoc($result))

			  {
		$row_color = ($row_count % 2) ? $color1 : $color2;		  
			  echo ('
					<tbody>	  
			  		<tr>
			  		<td bgcolor="$row_color">' . $row['first_name'] . '</td>
			  		<td bgcolor="$row_color">' . $row['post_code'] . '</td>
			  		<td bgcolor="$row_color">' . $row['team_supported'] . '</td>
					</tr>
			 		</tbody>
					');
			$row_count++;
			  }
			echo "</table>";

		?>

Link to comment
https://forums.phpfreaks.com/topic/204037-table-alternate-row-color/
Share on other sites

Thanks for the help mate,

 

What i thought should be happening is by using this code:

 

$row_color = ($row_count % 2) ? $color1 : $color2;

 

And having specified the colors in previous varials (color1 and color2) it will take what color is needed a placed in the bgcolor ="$row_color" section like below?

 

<td bgcolor="$row_color">' . $row['first_name'] . '</td>

 

I could be so far of the right method of doing this so my bad if its all a bit messed up heh

 

Smee

 

Doh, I missed that in your code but see it now.

 

Just curious what exactly is it putting out? It looks like that should work... Unless your PHP needs the variables more clearly written, ie: ".$color.". I know my PHP will sometimes just output $color instead of the contents of the variable.

 

Your basic idea is right. I've always done it using css though with something like:

echo ($i%2 ? 'odd' : 'even');

 

where $i is the row number.

You seem to be on the right idea to produce what you are after, what I have done in the past (which may or may not be the best method, but it works) is something like the following:

 

$color0 = '#FFF';
$color1 = '#CCC';

$row_count = 0;

while($row = mysql_fetch_assoc($result))
{
    if ($row_count == 1)
    {
        $row_color = $color1;
        $row_count = 0;
    }
    else
    {
        $row_color = $color0;
        $row_count++;
    }
    echo ('<tbody>
                  <tr bgcolor="$row_color">
                      <td>' . $row['first_name'] . '</td>
                      <td>' . $row['post_code'] . '</td>
                      <td>' . $row['team_supported'] . '</td>
                  </tr>
              </tbody>');
    }

 

Basically this will just use color0 if the counter is "0" and increment the counter by 1.  Otherwsie, it will use color1 and rest the counter.  This way the counter will bounce between 0 and 1 all the way through your table alternating the colours which are associated to either the number 0 or the number 1.

echo ($i%2 ? 'odd' : 'even');

 

Only keeps working if you never delete rows.

 

if ($row_count == 1)
    {
        $row_color = $color1;
        $row_count = 0;
    }
    else
    {
        $row_color = $color0;
        $row_count++;
    }

 

Is an unnecessary waste of resources.

 

$color = 'even';
while ($row = mysql_fetch_assoc($result)) {
  echo '<td class="', ($color = ($color === 'even') ? 'odd' : 'even'), '">' ..

Thanks your method seems like it should work perfectly.

 

I implemented it to my script and still do no get alternate colors, It still gives the right MySQL results just no color.

 

It wouldn't be anything to do with CSS that needs adding as well, i mean i tried earlier with the CSS method and that didn't work either.

 

Well anyway here is the new code thanks to JD307.

 

            <?php

		require_once ('../mysql_connect.php');

		echo ('<table width="294" height="150" border="0" />
			  <thead> 
				<tr>
				<th scope="col">First Name</th>
				<th scope="col">Post Code</th>
				<th scope="col">Team Supported</th>
				</tr>
			  </thead>	
				');

		$result = mysql_query("SELECT first_name, post_code, team_supported FROM users ORDER BY user_id DESC LIMIT 5") or die (mysql_error());

$color0 = '#FFF';
$color1 = '#CCC';

$row_count = 0;

while($row = mysql_fetch_assoc($result))
{
    if ($row_count == 1)
    {
        $row_color = $color1;
        $row_count = 0;
    }
    else
    {
        $row_color = $color0;
        $row_count++;
    }
    echo ('<tbody>
                  <tr bgcolor="$row_color">
                      <td>' . $row['first_name'] . '</td>
                      <td>' . $row['post_code'] . '</td>
                      <td>' . $row['team_supported'] . '</td>
                  </tr>
              </tbody>');
    }	echo "</table>";

		?>

Ok,

 

Ignace i changed the section u mentioned but got the following error:

 

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in ***/index.php  on line 88

 

line 88 is:                     

 <td>' . $row['first_name'] . '</td> 

 

I then changed both " to ' like it was before but kept your \'s on $row_color and no errors but no alternate colors.

 

There was me thinking this would be an easy job.

 

            <?php

		require_once ('../mysql_connect.php');

		echo ('<table width="294" height="150" border="0" />
			  <thead> 
				<tr>
				<th scope="col">First Name</th>
				<th scope="col">Post Code</th>
				<th scope="col">Team Supported</th>
				</tr>
			  </thead>	
				');

		$color0 = "#FFF"; 
		$color1 = "#CCC"; 
		$row_count = 0; 

		$result = mysql_query("SELECT first_name, post_code, team_supported FROM users ORDER BY user_id DESC LIMIT 5") or die (mysql_error());

		while($row = mysql_fetch_assoc($result))

{
    if ($row_count == 1)
    {
        $row_color = $color1;
        $row_count = 0;
    }
    else
    {
        $row_color = $color0;
        $row_count++;
    }
	echo ('<tbody>
                  <tr bgcolor=\"$row_color\">
                      <td>' . $row['first_name'] . '</td>
                      <td>' . $row['post_code'] . '</td>
                      <td>' . $row['team_supported'] . '</td>
                  </tr>
              </tbody>');
    }				
echo "</table>";

		?>

Here is an example of what I did on one of my webpages... very simple code.

 

$counter++;
if ($counter % 2) {
              	$bgcolor="#ffffff";
               	} else {
            	   	$bgcolor="#d9ece7";
                }


echo "<td bgcolor='".$bgcolor."'>Test</td>"

 

It alternates every other row with white or light blue.  Try it.

This

<?php
if ($counter % 2) {
              	$bgcolor="#ffffff";
               	} else {
            	   	$bgcolor="#d9ece7";
                }
?>

can be written

<?php
$bgcolor = ($counter % 2)?"#ffffff":"#d9ece7";
?>

 

Ken

What ignace is trying to tell you is that you cannot use variables inside a string that is delimited with single-quote marks:

echo ('<tbody>
                  <tr bgcolor="$row_color">
                      <td>' . $row['first_name'] . '</td>
                      <td>' . $row['post_code'] . '</td>
                      <td>' . $row['team_supported'] . '</td>
                  </tr>
              </tbody>');

will put the literal string $row_color in your output

 

echo ("<tbody>
                  <tr bgcolor='$row_color'>
                      <td>" . $row['first_name'] . "</td>
                      <td>" . $row['post_code'] . "</td>
                      <td>" . $row['team_supported'] . "</td>
                  </tr>
              </tbody>");

will put the value of $row_color in your output

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.