Table Problems *Solved*

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mohson
Forum Contributor
Posts: 372
Joined: Thu Dec 02, 2004 6:58 am
Location: London

Table Problems *Solved*

Post by mohson »

can anyone see why my alternating colors are not appearing in my table

Code: Select all

// Query to extract records from database.
$result = mysql_query ("$query LIMIT $limit") or die ("Error in query: $result".mysql_error());



// Define your colors for the alternating rows

$color1 = "0099FF";$color2 = "0000FF";
$color = $color2;


echo "<table width=\"50%\" border=\"0\" cellpadding=\"2\" cellspacing=\"2\">
	<tr>
	<td>			<b>Record<b>				</td>
	<td> 			<b>Id</b>					</td>
    <td 			<b>Name</b>					</td>
	<td>			<b>Organisation</b>			</td>
	<td>			<b>Website</small></b>		</td></tr>"; 


while ($row = mysql_fetch_object($result)) 






{($color==$color2)? $color = $color1 : $color = $color2;




echo "<tr bgcolor=\"$color\"><td>".$count . '</td>
<td>'.$row->stu_id.'</td>
<td>'.$row->name.'</td>
<td>'. $row->organisation .'</td>
<td>'. $row->website .'</tr>';



$count += 1;
}

echo "</table>";
Last edited by mohson on Tue Apr 12, 2005 10:28 am, edited 1 time in total.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

That looks like a really weird way to do it.

I do it like this

Code: Select all

$loop = 0;
while (/* whatever */) {
    if ($loop%2) {
        $color = 'Blue';
    } else {
        $color = 'Red';
    }
    echo "<tr><td bgcolor=\"$color\">Content</td></tr>";
    $loop++;
}
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Your colour assignment statement is a little off:
It should be:

Code: Select all

$color = ($color == $color2) ? $color1 : $color2;
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
mohson
Forum Contributor
Posts: 372
Joined: Thu Dec 02, 2004 6:58 am
Location: London

Post by mohson »

That is really strange because I do exactly the same thing on this other project ive been working on and it works absolutely perfectly, im not sure about changing the color statement as suggested by pickle as it works perfectly fine belowm can anyone see why my code above doesnt work, as this one does?:

Code: Select all

// Define your colors for the alternating rows

$color1 = "#ADD8E6";$color2 = "#E0FFFF";
$color = $color2;


echo "<table width=\"50%\" border=\"0\" cellpadding=\"2\" cellspacing=\"2\">
	<tr>
	<td 			<b><small>RecNo</small></b>		</td>
    <td 			<b><small>OID</small></b>		</td>
	<td><a href=\"orderorgorg.html\"><b><small>Organisation</small></b></a></td>
	<td>			<b><small>Notes</small></b>		</td>
	<td>											</td></tr>"; 


while ($row = mysql_fetch_object($sql)) 






{($color==$color2)? $color = $color1 : $color = $color2;




echo "<tr bgcolor=\"$color\"><td>".$count . '</td>
<td>'.$row->org_id.'</td>
<td>'. '<a href=http://'.$row->web_url.'>'.$row->orgname . '</a></td>
<td>'. $row->notes .'</td>
<td><a href="editorganisations.html?org_id=' .$row->org_id. '">edit</tr>';


$count += 1;
}

echo "</table>";
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

d11wtq wrote:That looks like a really weird way to do it.
Not weird, in fact most coding I have seen is done this way.
Although I would change it to

Code: Select all

if ($colour==$colour2) {
  $colour=$colour1;
} else {
  $colour=$colour2);
}
(which may solve the problem).

I prefer to use

Code: Select all

$loop = 0;
while (/* whatever */) {
    $row_class_no=($loop%2)+1;
    echo "<tr class="rowstyle$row_class_no"><td >Content</td></tr>";
    $loop++;
}
Then the change is CSS based not hardcoded...
mohson
Forum Contributor
Posts: 372
Joined: Thu Dec 02, 2004 6:58 am
Location: London

Post by mohson »

thanks for the help guys but I realised the problem I was missing the '#' when I assigned my colors - somtimes things can be soo simple yet seem soo complicated.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Ha - no worries - we all missed it too!
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply