Page 2 of 2

Posted: Tue Aug 01, 2006 10:20 am
by RobertGonzalez
You're telling it to do that. You are saying, in your code, for each item in the loop, open a row, add a cell, close the row. What you get is a table with many rows that are one cell wide.

Posted: Tue Aug 01, 2006 10:38 am
by GM
aussie_clint wrote:ok, its out putting it into a table now :) , but all in one colum

eg

row1
row2
row3
row1
row2
row3

Code: Select all

<?PHP
//connect to the mysql database
$connection=mysql_connect("localhost","computer_test","test1");
mysql_select_db("computer_price",$connection);
$query="SELECT p.PartNumber, p.Description, (p.IncTax*1.3) AS Price FROM `price list` p";
$data_resource=mysql_query($query,$connection);

$row1 = "#cfdafg";
$row2 = "#ff0000";
$rowcolor = $row1;

//Defining HTML table
$table="<table cellspacing=0 cellpadding=0 border=1 width=400>";
      while ($row = mysql_fetch_array($data_resource)) {
$table.=sprintf('<tr bgcolor="%s"><td>'.$row['PartNumber'].'</td></tr>', $rowcolor);
$table.=sprintf('<tr bgcolor="%s"><td>'.$row['Description'].'</td></tr>', $rowcolor);
$table.=sprintf('<tr bgcolor="%s"><td>'.$row['Price'].'</td></tr>', $rowcolor);
if ($rowcolor == $row1) $rowcolor = $row2;
if ($rowcolor == $row2) $rowcolor = $row1;

      }
//Closing HTML table
$table.="</table>";
//Displying table and closing mysql database if not need 
echo $table;
mysql_close();
?>
Wants to be something like:

Code: Select all

$table="<table cellspacing=0 cellpadding=0 border=1 width=400>";
      while ($row = mysql_fetch_array($data_resource)) {
$table.=sprintf('<tr bgcolor="%s"><td>'.$row['PartNumber'].'</td>', $rowcolor);
$table.='<td>'.$row['Description'].'</td>';
$table.='<td>'.$row['Price'].'</td></tr>';
if ($rowcolor == $row1) $rowcolor = $row2;
if ($rowcolor == $row2) $rowcolor = $row1;

      }
//Closing HTML table
$table.="</table>";
Personally, I'd do the changing background in a different way too:

Code: Select all

$rowcount = 0;
$table="<table cellspacing=0 cellpadding=0 border=1 width=400>";
      while ($row = mysql_fetch_array($data_resource)) {
$rowcount++;

// if the rowcount is even, use #cfdafg, else use #ff0000
$rowcolor = $rowcount % 2 == 0 ? "#cfdafg" : "#ff0000";

$table.="<tr bgcolor="$rowcolor"><td>".$row['PartNumber']."</td>";
$table.="<td>".$row['Description']."</td>";
$table.="<td>".$row['Price']."</td></tr>";

      }
//Closing HTML table
$table.="</table>";
...but that's just a personal preference. My way would allow you to for instance change the colour on every 3rd row, or fourth row etc. without having to change the code too much.

EDIT: Tidied up the code a little bit, and added comments

Posted: Tue Aug 01, 2006 10:53 am
by aussie_clint
Thanks every one, all is working now