Page 1 of 1

Table Problems *Solved*

Posted: Tue Apr 12, 2005 9:35 am
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>";

Posted: Tue Apr 12, 2005 9:42 am
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++;
}

Posted: Tue Apr 12, 2005 9:48 am
by pickle
Your colour assignment statement is a little off:
It should be:

Code: Select all

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

Posted: Tue Apr 12, 2005 9:56 am
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>";

Posted: Tue Apr 12, 2005 9:57 am
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...

Posted: Tue Apr 12, 2005 10:15 am
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.

Posted: Tue Apr 12, 2005 11:20 am
by pickle
Ha - no worries - we all missed it too!