Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/71977-php-row-help/
Share on other sites

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'>&#4335;&#4306;&#4323;&#4324;&#4312;</td>"; // use the selected bgcolor

}
echo "</TR>";
echo "</TABLE>";

Link to comment
https://forums.phpfreaks.com/topic/71977-php-row-help/#findComment-362563
Share on other sites

 

 

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.

Link to comment
https://forums.phpfreaks.com/topic/71977-php-row-help/#findComment-362596
Share on other sites

// 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...

Link to comment
https://forums.phpfreaks.com/topic/71977-php-row-help/#findComment-362604
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.