Need help dividing while loop into two columns...

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

Post Reply
RiceDaddy7
Forum Newbie
Posts: 2
Joined: Fri Oct 24, 2008 5:02 pm

Need help dividing while loop into two columns...

Post by RiceDaddy7 »

Hi everyone. I am in desperate, desperate need of help here. I'm trying to split my while loop into two columns. How do you do that?

Here is my exact code:

---

echo '
<table align="left" cellspacing="0" cellpadding="0" id="wording">
';

while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo "
<tr>
<td><img src=\"show_image.php?image={$row['thumbnail']}\"></img></td>
<td width=\"20px\"></td>
</tr>
";
}

mysql_free_result ($result);

echo '</table>';

---

Thank so much...really need help...
User avatar
Syntac
Forum Contributor
Posts: 327
Joined: Sun Sep 14, 2008 7:59 pm

Re: Need help dividing while loop into two columns...

Post by Syntac »

I use a method where you first get the array, then use a for loop on it. Example:

Code: Select all

<?php
 
$array = array( "foo", "bar", "baz", "foobar" );
 
for( $i = 0; $i < count( $array ); $i += 2 )
{
 
?>
    <tr>
        <td><?php echo $array[$i]; ?></td>
<?php if( isset( $array[$i+1] ) ) { ?>
        <td><?php echo $array[$i+1]; ?></td>
<?php } ?>
    </tr>
<?php } ?>
Should work fine for what you're trying to do.

[EDIT] If you're using an associative array, try this:

Code: Select all

<?php
 
$mysql_array = mysql_fetch_array( $result, MYSQL_ASSOC );
$array = array_keys( $mysql_array );
 
for( $i = 0; $i < count( $array ); $i += 2 )
{
 
?>
    <tr>
        <td><?php echo $mysql_array[$array[$i]]; ?></td>
<?php if( isset( $array[$i+1] ) ) { ?>
        <td><?php echo $mysql_array[$array[$i+1]]; ?></td>
<?php } ?>
    </tr>
<?php } ?>
RiceDaddy7
Forum Newbie
Posts: 2
Joined: Fri Oct 24, 2008 5:02 pm

Re: Need help dividing while loop into two columns...

Post by RiceDaddy7 »

Is there a way to do this on a while loop though instead of an array? I have already have the code perfected as a while loop on single column format. I do not want to overhaul. Can someone please help?
Post Reply