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

Link to comment
Share on other sites

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

Link to comment
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.