Loop through results/display in double column table

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
Crashin
Forum Contributor
Posts: 223
Joined: Mon May 06, 2002 3:42 pm
Location: Colorado

Loop through results/display in double column table

Post 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.
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

get some more sleep m8 :D

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>
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

Code: Select all

//assumes "$data" is the array of the results
$x = 0;
echo '<table>';
foreach($data as $array)&#123;
  if(($x % 2) == 0)&#123;
  echo '<tr><td>'; &#125; else &#123;
  echo '<td>'; &#125;
  echo $array&#1111;'column'];
  echo $array&#1111;'someothercolumninthetable'];
    if(($x % 2) == 0)&#123;
  echo '</td></tr>'; &#125; else &#123;
  echo '</td>'; &#125;
  $x++;
  &#125;
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.
User avatar
Crashin
Forum Contributor
Posts: 223
Joined: Mon May 06, 2002 3:42 pm
Location: Colorado

Post 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! :lol:
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

Takuma wrote:get some more sleep m8 :D

Code: Select all

&lt;table&gt;
&lt;?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 "
&lt;tr&gt;
  &lt;td&gt;{$result&#1111;$i]}&lt;/td&gt;
  &lt;td&gt;{$result&#1111;$n]}&lt;/td&gt;
&lt;/tr&gt;
   $i += 2;
";
}
?&gt;
&lt;/table&gt;
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...
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

I don't see where is your "|" mark on the code?
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

It represents a new <td>.. think about it, |'s are commonly used like that
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

Mine does it too...

Code: Select all

&lt;?php
table&gt; 
&lt;?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 " 
&lt;tr&gt; 
  &lt;td&gt;{$result&#1111;$i]}&lt;/td&gt; 
  &lt;td&gt;{$result&#1111;$n]}&lt;/td&gt; 
&lt;/tr&gt;
";
   $i += 2; 
} 
?&gt; 
&lt;/table&gt; 
?&gt;
User avatar
Crashin
Forum Contributor
Posts: 223
Joined: Mon May 06, 2002 3:42 pm
Location: Colorado

Post 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)) &#123;
    if(($i % 2) == 0) &#123;?>
		<tr>
			<td width="50%" height="50" valign="top" class="body"><?php
	&#125;
	else &#123;?>
			<td width="50%" height="50" valign="top" class="body"><?php
	&#125;
	$row&#1111;news_title] = stripslashes($row&#1111;news_title]);
	echo $row&#1111;news_title];
	if(($i % 2) == 0) &#123;?>
			</td><?php
	&#125;
	else &#123;?>
			</td>
		</tr><?php
	&#125;
	$i++;
&#125;
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! :)
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

Takuma-goblin :!: :?:

I LOVE it :!:

I have to say Crashin, you are sick! We need to hang out.

Cheers,
BDKR
User avatar
Crashin
Forum Contributor
Posts: 223
Joined: Mon May 06, 2002 3:42 pm
Location: Colorado

Post 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! :D
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

From Colorado eh? Better take off that coat! Your average highs are prolly 20 degrees less than our average lows :!:

Cheers,
BDKR
User avatar
Crashin
Forum Contributor
Posts: 223
Joined: Mon May 06, 2002 3:42 pm
Location: Colorado

Post 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?
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post 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
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

Hey don't mix my name with hob_goblins, ahhhh.... :lol: :wink:

OK, hob_gonblin don't take it seriously :lol:
Post Reply