Faqts : Computers : Programming : Languages : PHP : Database Backed Sites : MySQL

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

1 of 3 people (33%) answered Yes
Recently 0 of 1 people (0%) answered Yes

Entry

I wish to test the values and change the colour of a <td> tag accordingly while looping through and designing a table from MySQL

Jan 6th, 2004 09:21
Bill Sanders, Stephanie Lemaire,


// instantiate variables (edit as necessary)
$host = ($host) ? $host : "my_host";
$user = ($user) ? $user : "my_user";
 $pwd = ($pwd)  ? $pwd  : "my_password";
  $db = ($db)   ? $db   : "my_db";
$link = mysql_connect($host,$user,$pwd) or die("Could not connect to 
Host.");
mysql_select_db($db) or die("Could not select database.");
//provide your own query
$query = "select * from tableName limit 25"; 
$query = stripslashes($query);
$result = mysql_query($query) or die("Could not run query.");
$numfields = mysql_num_fields($result);
  echo "<table cellpadding='2' cellspacing='0' border='1'>\n";
  echo "\t<tr>\n";
  for ($i=0; $i<$numfields; $i++) { 
    echo ("\t\t<th>".mysql_field_name($result,$i)."</th>\n"); 
  }
  echo "\t</tr>\n";
  $j=0;
  while ($answer = mysql_fetch_array($result)) {
    echo "\t<tr>\n";
    for ($k=0; $k<$numfields; $k++) { 
      echo "\t\t<td";
//here's where you test your condition
      if ($answer[$k]>10) {echo " style='color:red'";} 
      echo ">" .$answer[$k]."</td>\n"; 
    }
    echo ("\t</tr>\n"); 
    $j++;
/*// used to repeat header row after 7 rows of results
    if ($j%7==0) { 
    echo "\t<tr>\n";
      for ($m=0; $m<$numfields; $m++) { 
        echo ("\t\t<th>".mysql_field_name($result,$m). "</th>\n"); 
      } 
    echo "\t</tr>\n";
    } */
  }
  echo "</table>\n";