Jump to content

print into html table


s4salman

Recommended Posts

I use following code to print records from mysql table. but i want to display 5 records in one row.

then next 5 records in 2nd row and so on. How this code will be changed. i m trying but all going wrong making me confused. Can any one help me?

<table border="0" cellspacing="2" cellpadding="2">
<tr>
<th><font face="Arial, Helvetica, sans-serif">Name</font></th>
<th><font face="Arial, Helvetica, sans-serif">Phone</font></th>
<th><font face="Arial, Helvetica, sans-serif">Mobile</font></th>
<th><font face="Arial, Helvetica, sans-serif">Fax</font></th>
<th><font face="Arial, Helvetica, sans-serif">E-mail</font></th>
<th><font face="Arial, Helvetica, sans-serif">Website</font></th>
</tr>


<?
$i=0;
while ($i < $num) {

$first=mysql_result($result,$i,"first");
$last=mysql_result($result,$i,"last");
$phone=mysql_result($result,$i,"phone");
$mobile=mysql_result($result,$i,"mobile");
$fax=mysql_result($result,$i,"fax");
$email=mysql_result($result,$i,"email");
$web=mysql_result($result,$i,"web");
?>

<tr>
<td><font face="Arial, Helvetica, sans-serif"><? echo $first." ".$last; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo $phone; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo $mobile; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo $fax; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><a href="mailto:<? echo $email; ?>">E-mail</a></font></td>
<td><font face="Arial, Helvetica, sans-serif"><a href="<? echo $web; ?>">Website</a></font></td>
</tr>

<?
$i++;
}


echo "</table>";

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/144803-print-into-html-table/
Share on other sites

Why do you want 5 records in 1 row?

 

It is possible, but is there a particular reason why?  To do it your table headings would no longer be valid, you would have to create 4 more duplicates of your th tags.

 

Explain the reasoning behind it, as I see no logical or reasonable reason why you would want it done this way.

<table border="0" cellspacing="2" cellpadding="2">
<!-- commented out cause this would not be useful with new version<tr>
<th><font face="Arial, Helvetica, sans-serif">Name</font></th>
<th><font face="Arial, Helvetica, sans-serif">Phone</font></th>
<th><font face="Arial, Helvetica, sans-serif">Mobile</font></th>
<th><font face="Arial, Helvetica, sans-serif">Fax</font></th>
<th><font face="Arial, Helvetica, sans-serif">E-mail</font></th>
<th><font face="Arial, Helvetica, sans-serif">Website</font></th>
</tr> -->


<?php
$i=0;
$output = "<tr>";
while ($row = mysql_fetch_assoc($result)) {
    if (($i%5) == 0) {
         $output .= "</tr><tr>";
   }

    $output .= <<<OUTPUT
<font face="Arial, Helvetica, sans-serif">{$row['first']} {$row['last']}</font><br />
<font face="Arial, Helvetica, sans-serif">{$row['phone']}</font><br />
<font face="Arial, Helvetica, sans-serif">{$row['mobile']}</font><br />
<font face="Arial, Helvetica, sans-serif">{$row['fax']}</font><br />
<font face="Arial, Helvetica, sans-serif"><a href="mailto:{$row['email']}">E-mail</a></font><br />
<font face="Arial, Helvetica, sans-serif"><a href="{$row['web']}">Website</a></font></td>
OUTPUT;

    $i++;
}

$output .= "</tr>"; // just incase

echo "{$output}</table>";
?>

 

The <<<OUTPUT OUTPUT; is HEREDOC syntax, incase you were wondering. See if the above is what you want.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.