Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help! Making PHP output neater

Status
Not open for further replies.

Maypen

Technical User
Feb 6, 2003
32
GB
hi guys i am a newbie at PHP. I have written a query to show the content of a table in MySQL. The ouput is correct but i would like it to look neater. I have tried using tables but i can only get it to display the first row in the table. This probably sounds simple to you guys but bear with me. If you can help please do so in detail. Thanks! The code is below:

<html>
<body>
<?php

$db_name=&quot;clarendon&quot;;
$table_name=&quot;merchandise&quot;;
$connection=@mysql_connect(&quot;localhost&quot;,&quot;root&quot;,&quot;&quot;)or die(&quot;connect fail&quot;);
$db=@mysql_select_db($db_name,$connection) or die(&quot;dbselect fail&quot;);
$sql=&quot;select stockno,description,size,qty,reorderqty,price,notes from
$table_name&quot;;
$result=@mysql_query($sql,$connection)or die(&quot;execute fails&quot;);
while ($row=@mysql_fetch_array($result)){
$stockno=$row['stockno'];
$description=$row['description'];
$size=$row['size'];
$qty=$row['qty'];
$reorderqty=$row['reorderqty'];
$price=$row['price'];
$notes=$row['notes'];

echo&quot;<p>$stockno,$description,$size,$qty,$reorderqty,$price,$notes&quot;;
}

?>
 
Thanks sleipnir, I tried that but only the first row appeared in the table. I think you are right but i think there is some sort of loop and i dont know how to do that.
 
Try something like this:

echo&quot;<TABLE><TR><TD>StockNo</TD> <TD>Description</TD> <TD>Size</TD> <TD>qty</TD> <TD>reorderqty</TD> <TD>price</TD> <TD>Notes</TD> </TR>&quot;;
while ($row=@mysql_fetch_array($result)){
$stockno=$row['stockno'];
$description=$row['description'];
$size=$row['size'];
$qty=$row['qty'];
$reorderqty=$row['reorderqty'];
$price=$row['price'];
$notes=$row['notes'];
echo&quot;<TR>&quot;;
echo&quot;<TD>&quot;.$stockno.&quot;</TD>&quot;;
echo&quot;<TD>&quot;.$description.&quot;</TD>&quot;;
echo&quot;<TD>&quot;.$size.&quot;</TD>&quot;;
echo&quot;<TD>&quot;.$qty.&quot;</TD>&quot;;
echo&quot;<TD>&quot;.$reorderqty.&quot;</TD>&quot;;
echo&quot;<TD>&quot;.$price.&quot;</TD>&quot;;
echo&quot;<TD>&quot;.$notes.&quot;</TD>&quot;;
echo&quot;</TR>&quot;;
}
echo&quot;</TABLE>&quot;;


BDC.
 
Whenever you create HTML code with PHP you need to make sure that the output is valid HTML.

You need also take browser idiosyncacies into consideration. For example, BDC2's code would not display well in Netscape 4 (yes, there are people using it still) if one of the vars is empty since NS does not display empty table cells. It needs at least a  &nbsp;

The generated HTML code might not dislay because of syntactic errors.

You have several options to do the HTML:

1. Create all output in PHP with echo or print statements.

2. Mix HTML and PHP. That's how it all started. Make a HTML page and then infuse the PHP where needed. You can preview it e.g. in Dremaweaver and check that the HTML code is valid.

3. Seperate HTML and PHP. Use an HTML template class (e.g. fastTemplate) and keep the PHP code free of all dislay issues i.e., make it do the operations you need and let the template take care of the display.

IMO #3 is most fruitful.
 
BDC2-Thanks for your answer. I will try it later today.

DRJ478- Thanks also for your answer.I dont fully understand template class but i will investigate it and try it. Thanks both!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top