Page 1 of 2
Loop through results/display in double column table
Posted: Thu Sep 26, 2002 3:44 pm
by Crashin
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.
Posted: Thu Sep 26, 2002 4:12 pm
by Takuma
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>
Posted: Thu Sep 26, 2002 4:12 pm
by hob_goblin
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++;
}
hope this helps.. it just checks if "x" is an odd number or not, and if it isn't it will make a new row, so it will be a two column table.
Posted: Thu Sep 26, 2002 4:15 pm
by Crashin
Oh, man...you two are the best. I think I'll name my firstborn Takuma-goblin. On the other hand, maybe I do need some mo' sleep. Thanks!

Posted: Thu Sep 26, 2002 4:17 pm
by hob_goblin
Takuma 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>
if i have a table with columns 'foo' and 'bar', it will just print
foo bar
foo bar2
foo bar3
foo bar4
my way, it prints
foo bar | foo bar2
foo bar3 | foo bar4
etc...
Posted: Thu Sep 26, 2002 4:21 pm
by Takuma
I don't see where is your "|" mark on the code?
Posted: Thu Sep 26, 2002 5:08 pm
by hob_goblin
It represents a new <td>.. think about it, |'s are commonly used like that
Posted: Fri Sep 27, 2002 1:19 am
by Takuma
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>
?>
Posted: Fri Sep 27, 2002 9:16 am
by Crashin
This is what ended up doing the trick:
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++;
}
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!

Posted: Fri Sep 27, 2002 9:33 am
by BDKR
Takuma-goblin
I
LOVE it
I have to say Crashin, you are sick! We need to hang out.
Cheers,
BDKR
Posted: Fri Sep 27, 2002 9:42 am
by Crashin
I think it has a nice ring to it. I could say, "Hey, Takuma-goblin! Get yo' but in here and clean yo' room."
We should hang out! Next time I make it down to Margarita Island we'll get a margarita!

Posted: Fri Sep 27, 2002 2:14 pm
by BDKR
From Colorado eh? Better take off that coat! Your average highs are prolly 20 degrees less than our average lows
Cheers,
BDKR
Posted: Mon Sep 30, 2002 9:27 am
by Crashin
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?
Posted: Mon Sep 30, 2002 10:06 am
by BDKR
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?
Yeah, I am suprised.
And yeah, we have beaches. And the nice thing is that paradise is cheap

Even still, I miss snowboarding and my Subarus. I used to live in California.
Cheers,
BDKR
Posted: Tue Oct 01, 2002 9:44 am
by Takuma
Hey don't mix my name with hob_goblins, ahhhh....
OK, hob_gonblin don't take it seriously
