Loop through results/display in double column table
Moderator: General Moderators
Loop through results/display in double column table
Howdy all, my fine-coding friends! Well, here's my question of the day. I'd like to display the results of my query in a double-column table. So, basically, it would kind of look like this:
Result 1 | Result 2
---------------------------
Result 3 | Result 4
---------------------------
Result 5 | Result 6
etc.
I know how to loop through results and display them in a single column table. Maybe I'm too tired (been up since 4:00 am) or just ignorant, but I could use some enlightenment.
Result 1 | Result 2
---------------------------
Result 3 | Result 4
---------------------------
Result 5 | Result 6
etc.
I know how to loop through results and display them in a single column table. Maybe I'm too tired (been up since 4:00 am) or just ignorant, but I could use some enlightenment.
get some more sleep m8
Code: Select all
<table>
<?php
@mysql_connect("localhost","usr","pwd") or die(mysql_error());
@mysql_select_db("db");
$sql = "SELECT * FROM table";
$temp = @mysql_query($sql);
$i = 0;
while($result = mysql_fetch_array($temp)) {
$n = $i + 1;
echo "
<tr>
<td>{$resultї$i]}</td>
<td>{$resultї$n]}</td>
</tr>
$i += 2;
";
}
?>
</table>- hob_goblin
- Forum Regular
- Posts: 978
- Joined: Sun Apr 28, 2002 9:53 pm
- Contact:
Code: Select all
//assumes "$data" is the array of the results
$x = 0;
echo '<table>';
foreach($data as $array){
if(($x % 2) == 0){
echo '<tr><td>'; } else {
echo '<td>'; }
echo $arrayї'column'];
echo $arrayї'someothercolumninthetable'];
if(($x % 2) == 0){
echo '</td></tr>'; } else {
echo '</td>'; }
$x++;
}- hob_goblin
- Forum Regular
- Posts: 978
- Joined: Sun Apr 28, 2002 9:53 pm
- Contact:
if i have a table with columns 'foo' and 'bar', it will just printTakuma wrote:get some more sleep m8![]()
Code: Select all
<table> <?php @mysql_connect("localhost","usr","pwd") or die(mysql_error()); @mysql_select_db("db"); $sql = "SELECT * FROM table"; $temp = @mysql_query($sql); $i = 0; while($result = mysql_fetch_array($temp)) { $n = $i + 1; echo " <tr> <td>{$resultї$i]}</td> <td>{$resultї$n]}</td> </tr> $i += 2; "; } ?> </table>
foo bar
foo bar2
foo bar3
foo bar4
my way, it prints
foo bar | foo bar2
foo bar3 | foo bar4
etc...
- hob_goblin
- Forum Regular
- Posts: 978
- Joined: Sun Apr 28, 2002 9:53 pm
- Contact:
Mine does it too...
Code: Select all
<?php
table>
<?php
@mysql_connect("localhost","usr","pwd") or die(mysql_error());
@mysql_select_db("db");
$sql = "SELECT * FROM table";
$temp = @mysql_query($sql);
$i = 0;
while($result = mysql_fetch_array($temp)) {
$n = $i + 1;
echo "
<tr>
<td>{$resultї$i]}</td>
<td>{$resultї$n]}</td>
</tr>
";
$i += 2;
}
?>
</table>
?>This is what ended up doing the trick:
I had to reverse the last conditions in hob's code to create the closing tags for the cell/row v. just the cell, but that's it. Tak's code worked, but differently from what I needed specifically. Thanks again for your help!
Incidentally, I did get some sleep...finally!
Code: Select all
$query = "SELECT * FROM news WHERE inactive='f' ORDER BY date_modified";
$result = mysql_query($query);
$i = 2;
while($row = mysql_fetch_array($result)) {
if(($i % 2) == 0) {?>
<tr>
<td width="50%" height="50" valign="top" class="body"><?php
}
else {?>
<td width="50%" height="50" valign="top" class="body"><?php
}
$rowїnews_title] = stripslashes($rowїnews_title]);
echo $rowїnews_title];
if(($i % 2) == 0) {?>
</td><?php
}
else {?>
</td>
</tr><?php
}
$i++;
}Incidentally, I did get some sleep...finally!
Yeah, I am suprised.Crashin wrote: You'd be surprised. In the summertime we can get up to 101+ degrees Farenheit. But, I imagine you never get anywhere close to as cold as it gets here in the winter. (-10+ F.) That's when we could use your beaches...you do have beaches, right?
And yeah, we have beaches. And the nice thing is that paradise is cheap
Cheers,
BDKR