0

I've googled and tried several things all to no avail. I'm taking some classes in PHP and MySQL and I'm trying to add alternating color to the rows in array results. So far nothing has worked. I'm not sure where to put it. This is the code I have:

//assemble the sql query string to insert a record
$query="SELECT * FROM Vehicles WHERE buyer IS NOT NULL ORDER BY StockID asc";

//open a connection and select the database
$conn=mysql_connect($host, $username, $password);
@mysql_select_db($database, $conn) or die ("Unable to select $database database");

//Execute the sql query
if($result=mysql_query($query, $conn)){
$rows=mysql_numrows($result);
echo"<p>There are $rows cars in the system</p>";
?>  

<table border="0" align="center" border="0" cellspacing="0" style="margin:auto; border-spacing:2px 1px; border-collapse:separate; border:ridge 5px; border-color:#669999;">
<tr>
<td style="background-color:#92DEDC">Stock ID</td>
<td style="background-color:#92DEDC">Reg Number</td>
<td style="background-color:#92DEDC">Make</td>
<td style="background-color:#92DEDC">Model</td>
<td style="background-color:#92DEDC">Year</td>
<td style="background-color:#92DEDC">Cost Price</td>
<td style="background-color:#92DEDC">EmpID</td>
<td style="background-color:#92DEDC">Sell Price</td>
<td style="background-color:#92DEDC">Seller</td>
<td style="background-color:#92DEDC">Buyer</td>
<td style="background-color:#92DEDC">Admin Tasks</td>
</tr>

<?php

$i=0;

while($i<$rows){
    $recordArray=mysql_fetch_array($result);//fetch and display a record
        echo "<tr bgcolor='$bgcolor'>   
                <td >$recordArray[StockID]</td>
                <td>$recordArray[Rego_No]</td>
                <td>$recordArray[Make]</td>
                <td>$recordArray[Model]</td>
                <td>$recordArray[Year]</td>
                <td>$recordArray[Cost_Price]</td>
                <td>$recordArray[employeeid]</td>
                <td>$recordArray[Sale_Price]</td>
                <td>$recordArray[emp_name]</td>
                <td>$recordArray[Buyer]</td>
                <td>
                    <a href='edit-car.php?StockID=$recordArray[StockID] & emp_name=$recordArray[emp_name]'>Edit</a>
                </td>
            </tr>";
        $i++;
}else{
echo "Something went wrong. MySQL reports <b>", mysql_error(),"</b>";
}
?>  
</table>
<?php
mysql_close($conn);
?>
</body></html>

Any assistance would be greatly appreciated. We are only a week into the course and I'm just trying to dress up the results a little.

2
  • 1
    mysql_numrows should be mysql_num_rows .. also these mysql_* is deprecated as of PHP 5.5.0 and not recommended by php communtiy. Prefer using PDO or mysqli to avoid issues like mysql injection Commented Feb 16, 2013 at 5:22
  • Thank you for telling me that. I'm going to talk to our teacher on Wednesday and ask why we are not learning the current stuff. I would much prefer to be learning what is currently being used. Cheers, Spud Commented Feb 16, 2013 at 6:18

4 Answers 4

3

Use the counter:

if($i % 2 == 0) {
  $class = "even";
}
else {
  $class = "odd"
}

Then, add that $class to your template (instead of the repeated background-color things), and write CSS with the .even and .odd selectors.

(my PHP is a bit rusty, but, that's the general idea).

Also, as mithunsatheesh pointed out, the mysql_* set of functions is incredibly dangerous and overall just a bad plan to be using - use PDO instead.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the quick response. We are only working in the browser for now and there is no CSS associated with the page. I'm looking for a PHP way to do it. Cheers, Spud
That's quite frankly a terrible idea - it will be an incredible pain to fix it later. But, the idea is still the same, change the $class to $color and set $color to the different colors in the if{} and else{} and then set that as your background-color value.
@Spud: if you dont want to use css. $class="background:#color1" and echo in tr style
0
<?php

$i=0;

while($recordArray=mysql_fetch_array($result))
{
    if($i % 2 == 0) 
    {
      $bgcolor= "#006666";
   }
   else 
  {
     $bgcolor= "#CC6699";
  }
   $i++;
    echo "<tr bgcolor='$bgcolor'>   
            <td >$recordArray[StockID]</td>
            <td>$recordArray[Rego_No]</td>
            <td>$recordArray[Make]</td>
            <td>$recordArray[Model]</td>
            <td>$recordArray[Year]</td>
            <td>$recordArray[Cost_Price]</td>
            <td>$recordArray[employeeid]</td>
            <td>$recordArray[Sale_Price]</td>
            <td>$recordArray[emp_name]</td>
            <td>$recordArray[Buyer]</td>
            <td>
                <a href='edit-car.php?StockID=$recordArray[StockID] & emp_name=$recordArray[emp_name]'>Edit</a>
            </td>
        </tr>";


   }
 ?>  

3 Comments

Hi, when I try the above code it returns this: Parse error: syntax error, unexpected '}' in C:\xampp\htdocs\mydatabase\cars-sold.php on line 55. What am I doing wrong? Cheers
@Spud there is an ; missing in else part. put a ; in $bgcolor= "#CC6699";
thanks for that. I'm very new to all of this and it's very helpful when someone takes me little steps at a time. I'll be 60 this year and I thought this would be fun. It is when it works, but I've discovered I'm pretty good at breaking it. Cheers, Spud
0

You can use The Idea of Spud and create the string of Style

if($i % 2 == 0) {

$style = "style='background-color:white'";
}
else {
$style = "style='background-color:#eee'";
}`

and can use in place of bgcolor in row tag

<tr $style>

Comments

0

Well,

while... else does not work for one. The typical way to do this would be:

$dbx=new mysqli('host','user','pw');
$q=$dbx->query"SELECT * FROM whatever";
if ($q) {
    while ($f=$q->fetch_assoc()) {
       echo "<tr><td>{$f['whatever']}</td></tr>";
    }
} else {
    echo "Problem";
}

THEN: In css, say you have Put in your css file:

#resultsTable tr:nth-child(even) { background-color:blue; }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.