Jump to content


Photo

4 column results


  • Please log in to reply
2 replies to this topic

#1 Canman2005

Canman2005
  • Members
  • PipPipPip
  • Advanced Member
  • 669 posts

Posted 06 March 2006 - 02:18 PM

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

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

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

followed by the code for results

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

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

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

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

Loads of thanks in advance

Dave

#2 XenoPhage

XenoPhage
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts

Posted 06 March 2006 - 02:36 PM

Use a counter...

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

Note : You need to adjust accordingly if you intersperse php with html.. (Personally, I use smarty templates)
--
[a href=\"http://blog.godshell.com\" target=\"_blank\"]XenoPhage[/a]
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Something mysterious is formed, born in the silent void. Waiting alone and unmoving, it is at once still and yet in constant motion. It is the source of all programs. I do not know its name, so I will call it the Tao of Programming.[/quote]

#3 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 06 March 2006 - 02:43 PM

<?
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>
<?
}
?>


I think that should do the job mate :)
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users