Jump to content

4 column results


Canman2005

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