Jump to content

Archived

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

Canman2005

4 column results

Recommended Posts

Hi all

I have a php result page which currently displays results one after each other, like

result 1
result 2
result 3
result 4

I use a very simple php query, which is

[code]<?
session_start();
include ("db.php");
$sql ="SELECT * FROM files";

$result = @mysql_query($sql,$connection) or die(mysql_error());
?>[/code]

followed by the code for results

[code]<?
while ($rows = mysql_fetch_array($result)) {
?>
<? print "$rows[name]"; ?><br>
<?
}
?>[/code]

How can I get results to appear in a 4 column wide table, the code for the table would look like

[code]<table>
  <tr>
    <td>result 1</td>
    <td>result 2 </td>
    <td>result 3 </td>
    <td>result 4 </td>
  </tr>
  <tr>
    <td>result 5 </td>
    <td>result 6 </td>
    <td>result 7 </td>
    <td>result 8 </td>
  </tr>
</table>[/code]

rather than appearing one after each other? Is that easy?

Loads of thanks in advance

Dave

Share this post


Link to post
Share on other sites
Use a counter...

[code]
$counter = 0;
while ($rows = mysql_fetch_array($result)) {
   if ($counter == 4) {
      $counter = 0;
      print "</tr><tr>\n";
   }
   print "<td>" . $rows[0] . "</td>\n";
   $counter++;
}
[/code]

Note : You need to adjust accordingly if you intersperse php with html.. (Personally, I use smarty templates)

Share this post


Link to post
Share on other sites
[code]
<?
session_start();
include ("db.php");
$sql ="SELECT * FROM files";

$result = @mysql_query($sql,$connection) or die(mysql_error());
?>
<table>

<?
$i=1;
while ($rows = mysql_fetch_array($result)) {
} if ($i=5) {
    echo("</tr>");
    $i=1;
}
if ($i=1) {
    echo("<tr>");
}
echo("<td>$row[name]</td>");
$i++;
?>
</table>
<br>
<?
}
?>[/code]


I think that should do the job mate :)

Share this post


Link to post
Share on other sites

×

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.