0

I am learning PHP as I code. From an example on w3schools it showed using PHP and msql to display database results on a html table. My questions is, I now have too many rows and I could not make them have mismatch colors between rows. I've tried adding style span and font color after <td but it doesn't take it. the entire PHP just don't work if I do so.

<?php $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("my_db", $con); $result = mysql_query("SELECT * FROM Persons"); echo "<table border='1'> <tr> <th>Firstname</th> <th>Lastname</th> </tr>"; while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['FirstName'] . "</td>"; echo "<td>" . $row['LastName'] . "</td>"; echo "</tr>"; } echo "</table>"; mysql_close($con); ?> 

The output of the code above will be:

Firstname Lastname Glenn Quagmire Peter Griffin 

http://www.w3schools.com/php/php_mysql_select.asp

1
  • So, you want alternating colors in the output table? Commented Aug 5, 2011 at 23:55

5 Answers 5

1

Not entirely sure what you mean by color mismatch. Assuming that you mean alternating row colors I'd do the following:

$odd = false; while (...) { echo '<tr class="'.($odd ? "odd" : "even").'">'; ... echo "</tr>"; $odd = !$odd; } 

Now you have the tr element being of class odd or even alternatingly, and may specify some additional background color for one of them in your CSS, e.g.:

tr.odd { background-color: rgba(0, 0, 0, 0.05); } 
Sign up to request clarification or add additional context in comments.

Comments

1

Replace this:

while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['FirstName'] . "</td>"; echo "<td>" . $row['LastName'] . "</td>"; echo "</tr>"; } 

with this:

$i = 0; while($row = mysql_fetch_array($result)) { echo "<tr ". ($i % 2 == 0 ? 'style="background-color:grey;"' : '' .">"; echo "<td>" . $row['FirstName'] . "</td>"; echo "<td>" . $row['LastName'] . "</td>"; echo "</tr>"; $i++; } 

Every other row will have grey color.

Comments

1

Maybe you can use this approach with jquery

<script src="text/javascript"> $('#table tbody tr:odd').addClass('odd'); $('#table tbody tr:even').addClass('even'); </script> 

and then add the styles

.odd { background-color: #color } .even { background-color: #color } 

Comments

1
$class = "even"; while($row = mysql_fetch_array($result)) { if($class == "even") { echo "<tr class='$class'>"; $class = "odd"; } else { echo "<tr class='$class'>"; $class = "even"; } echo "<td>" . $row['FirstName'] . "</td>"; echo "<td>" . $row['LastName'] . "</td>"; echo "</tr>"; } 

CSS

tr.even { background-color:blue;//Pick your own color } tr.odd { background-color:green; } 

Here is a list of color names. If you want a more detailed color choices, click here.

Comments

0

Use

 $flag = 0; while($row = mysql_fetch_array($result)) { if ($flag%2 == 1) echo "<tr bgcolor=#123345>"; else echo echo "<tr bgcolor=#643235>"; echo "<td>" . $row['FirstName'] . "</td>"; echo "<td>" . $row['LastName'] . "</td>"; echo "</tr>"; $flag = $flag +1; } 

2 Comments

sorry. I was just trying to help. I thought it was apparent that I meant replace. Did not mean to confuse anyone.
Thanks for your help, it's really clear for me to understand the work around.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.