logicopinion Posted October 5, 2007 Share Posted October 5, 2007 hello guys, how are you? i have a simple quistion you may help me with so here is my code which prints simple table with the information read from DB: <?php mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("db1") or die(mysql_error()); $query = "SELECT * FROM dbtable ORDER BY id DESC LIMIT 0, 30"; $result = mysql_query($query) or die(mysql_error()); print "<table align=center width=98% class=maintxt border=1 cellpadding=0 cellspacing=0>"; print "<tr>"; print "<td height=40 bordercolordark=\"#CCCCCC\" bordercolorlight=\"#FFFFFF\" align=center bgcolor=#F0F0F0> № </td>"; print "<td bordercolordark=\"#CCCCCC\" bordercolorlight=\"#CCCCCC\" align=center bgcolor=#F0F0F0>ჯგუფი</td>"; print "<td bordercolordark=\"#CCCCCC\" bordercolorlight=\"#CCCCCC\" align=center bgcolor=#F0F0F0>სახელი</td>"; print "<td bordercolordark=\"#CCCCCC\" bordercolorlight=\"#CCCCCC\" align=center bgcolor=#F0F0F0>გვარი</td>"; print "<td bordercolordark=\"#CCCCCC\" bordercolorlight=\"#CCCCCC\" align=center bgcolor=#F0F0F0>ქალაქი</td>"; print "<td bordercolordark=\"#CCCCCC\" bordercolorlight=\"#CCCCCC\" align=center bgcolor=#F0F0F0>სკოლა ნომერი</td>"; print "<td bordercolordark=\"#CCCCCC\" bordercolorlight=\"#CCCCCC\" align=center bgcolor=#F0F0F0>პროფესია</td>"; print "<td bordercolordark=\"#CCCCCC\" bordercolorlight=\"#CCCCCC\" align=center bgcolor=#F0F0F0>საკონტაქტო ტელეფონი</td>"; print "<td bordercolordark=\"#CCCCCC\" bordercolorlight=\"#CCCCCC\" align=center bgcolor=#F0F0F0>პირადი ნომერი</td>"; print "<td bordercolordark=\"#CCCCCC\" bordercolorlight=\"#CCCCCC\" align=center bgcolor=#F0F0F0>ტრენინგის გავლის თარიღი</td>"; print "</tr>"; while($row = mysql_fetch_array($result)) { print "<tr>"; print "<td align=center bordercolordark=\"#CCCCCC\" bordercolorlight=\"#CCCCCC\">"; echo $row['id']; print "</td>"; print "<td align=center bordercolordark=\"#CCCCCC\" bordercolorlight=\"#CCCCCC\">"; echo $row['group_id']; print "</td>"; print "<td align=center bordercolordark=\"#CCCCCC\" bordercolorlight=\"#CCCCCC\">"; echo $row['name']; print "</td>"; print "<td align=center bordercolordark=\"#CCCCCC\" bordercolorlight=\"#CCCCCC\">"; echo $row['s_name']; print "</td>"; print "<td align=center bordercolordark=\"#CCCCCC\" bordercolorlight=\"#CCCCCC\">"; echo $row['city']; print "</td>"; print "<td align=center bordercolordark=\"#CCCCCC\" bordercolorlight=\"#CCCCCC\">"; echo $row['school_id']; print "</td>"; print "<td align=center bordercolordark=\"#CCCCCC\" bordercolorlight=\"#CCCCCC\">"; echo $row['proffesion']; print "</td>"; print "<td align=center bordercolordark=\"#CCCCCC\" bordercolorlight=\"#CCCCCC\">"; echo $row['telephone']; print "</td>"; print "<td align=center bordercolordark=\"#CCCCCC\" bordercolorlight=\"#CCCCCC\">"; echo $row['personal_id']; print "</td>"; print "<td align=center bordercolordark=\"#CCCCCC\" bordercolorlight=\"#CCCCCC\">"; echo $row['date']; print "</td>"; print "</tr>"; } print "</table>"; ?> so as it brings as much rows as much it finds in SQL database, i want to have different color rows, one after one, exaple row1 to be grey, row2 to be white, row3 to be grey ... etc.. can someone help me with that? thank you Quote Link to comment https://forums.phpfreaks.com/topic/71977-php-row-help/ Share on other sites More sharing options...
BlueSkyIS Posted October 5, 2007 Share Posted October 5, 2007 toggle colors while looping over records. there are several ways, here's one: $i = 0; echo "<table align=center width=98% class=maintxt border=1 cellpadding=0 cellspacing=0>"; echo "<tr>"; while ($row = mysql_fetch_array($result)) { if ($i == 0) { $bgcolor = '#CCCCCC'; $i = 1; } else { $bgcolor = '#DDDDDD'; $i = 0; } echo "<td bordercolordark='$bgcolor' bordercolorlight='$bgcolor' align=center bgcolor='#F0F0F0'>ჯგუფი</td>"; // use the selected bgcolor } echo "</TR>"; echo "</TABLE>"; Quote Link to comment https://forums.phpfreaks.com/topic/71977-php-row-help/#findComment-362563 Share on other sites More sharing options...
logicopinion Posted October 5, 2007 Author Share Posted October 5, 2007 thank you but i do not want a "bordercolordark" or "BordercolorLik" i want <td bgcolor=firstcolor></td> <td bgcolor=secondcolor></td> so on repeat... Quote Link to comment https://forums.phpfreaks.com/topic/71977-php-row-help/#findComment-362572 Share on other sites More sharing options...
BlueSkyIS Posted October 5, 2007 Share Posted October 5, 2007 mine was only an example. simply put bgcolor='$bgcolor' wherever you need it. p.s. you'll run into fewer problems if you single-quote your HTML tag parameters. Quote Link to comment https://forums.phpfreaks.com/topic/71977-php-row-help/#findComment-362576 Share on other sites More sharing options...
logicopinion Posted October 5, 2007 Author Share Posted October 5, 2007 i put it but it does not work anyway.. see: <?php mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("db1") or die(mysql_error()); $query = "SELECT * FROM dbtable ORDER BY id DESC LIMIT 0, 30"; $result = mysql_query($query) or die(mysql_error()); $i = 0; echo "<table align=center width=98% class=maintxt border=1 cellpadding=0 cellspacing=0>"; while($row = mysql_fetch_array($result)) { if ($i == 0) { $bgcolor = '#CCCCCC'; $i = 1; } else { $bgcolor = '#FFFFFF'; $i = 0; } print "<tr>"; echo "<td $bgcolor align=center bordercolordark=\"#CCCCCC\" bordercolorlight=\"#CCCCCC\">"; echo $row['id']; print "</td>"; print "<td $bgcolor align=center bordercolordark=\"#CCCCCC\" bordercolorlight=\"#CCCCCC\">"; echo $row['group_id']; print "</td>"; print "<td align=center bordercolordark=\"#CCCCCC\" bordercolorlight=\"#CCCCCC\">"; echo $row['name']; print "</td>"; print "<td align=center bordercolordark=\"#CCCCCC\" bordercolorlight=\"#CCCCCC\">"; echo $row['s_name']; print "</td>"; print "<td align=center bordercolordark=\"#CCCCCC\" bordercolorlight=\"#CCCCCC\">"; echo $row['city']; print "</td>"; print "<td align=center bordercolordark=\"#CCCCCC\" bordercolorlight=\"#CCCCCC\">"; echo $row['school_id']; print "</td>"; print "<td align=center bordercolordark=\"#CCCCCC\" bordercolorlight=\"#CCCCCC\">"; echo $row['proffesion']; print "</td>"; print "<td align=center bordercolordark=\"#CCCCCC\" bordercolorlight=\"#CCCCCC\">"; echo $row['telephone']; print "</td>"; print "<td align=center bordercolordark=\"#CCCCCC\" bordercolorlight=\"#CCCCCC\">"; echo $row['personal_id']; print "</td>"; print "<td align=center bordercolordark=\"#CCCCCC\" bordercolorlight=\"#CCCCCC\">"; echo $row['date']; print "</td>"; print "</tr>"; } print "</table>"; ?> and i did not understand what you meant saying: "p.s. you'll run into fewer problems if you single-quote your HTML tag parameters." my english is not that good. Quote Link to comment https://forums.phpfreaks.com/topic/71977-php-row-help/#findComment-362596 Share on other sites More sharing options...
BlueSkyIS Posted October 5, 2007 Share Posted October 5, 2007 // missing bgcolor parameter. change to: print "<TD bgcolor='$bgcolor'.... I mean put single-quotes around parameters in HTML tags. For instance: <TD bgcolor=#CCCCCC align=left valign=top> should be <TD bgcolor='#CCCCCC' align='left' valign='top'> this is NOT required to be syntactically correct, but it can save you problems down the line, especially if you get into putting values into form fields: <INPUT TYPE='text' NAME='someformfield' VALUE='If this was not quoted, there is no telling what would show up' style='bgcolor=#EEEEEE;'> just a tip, not a requirement... Quote Link to comment https://forums.phpfreaks.com/topic/71977-php-row-help/#findComment-362604 Share on other sites More sharing options...
logicopinion Posted October 5, 2007 Author Share Posted October 5, 2007 thank you dude, i am done. Quote Link to comment https://forums.phpfreaks.com/topic/71977-php-row-help/#findComment-362608 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.