Friday, 15 January 2010

php - Add alternating color to mysql_fetch_array row results -



php - Add alternating color to mysql_fetch_array row results -

i've googled , tried several things no avail. i'm taking classes in php , mysql , i'm trying add together alternating color rows in array results. far nil has worked. i'm not sure set it. code have:

//assemble sql query string insert record $query="select * vehicles buyer not null order stockid asc"; //open connection , select database $conn=mysql_connect($host, $username, $password); @mysql_select_db($database, $conn) or die ("unable select $database database"); //execute sql query if($result=mysql_query($query, $conn)){ $rows=mysql_numrows($result); echo"<p>there $rows cars in 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 , display 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 appreciated. week course of study , i'm trying dress results little.

<?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>"; } ?>

php

No comments:

Post a Comment