Page 1 of 1

Need help dividing while loop into two columns...

Posted: Fri Oct 24, 2008 5:07 pm
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...

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

Posted: Fri Oct 24, 2008 5:13 pm
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 } ?>

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

Posted: Fri Oct 31, 2008 11:26 am
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?