...

View Full Version : nested loops?



aprestong
08-22-2006, 09:29 PM
Hi everyone-

I am using FileMaker as a database and trying to create a table to display three columns of books on a website.
I am using php and a foreach() statement.
I imagine that in order to have three columns (instead of one) I need to do some sort of nested loop - but I can't seem to wrap my head around it.
Here is my code I'm using:


<?php
foreach( $lookupResult['data'] as $key => $searchData ){
?>
<table width="33%" border="1" cellpadding="6">
<tr>
<td width="106" height="167">
<img src="FX/image_proxy.php?FXimage=<? echo vignereEncryptURL($searchData['Cover'][0]); ?>"> </tr>
<tr>
<td height="212"><pre><b><?php echo $searchData['Title'][0];
?></b></pre>
<pre><?php echo $searchData['Contact'][0];
?></pre></td>
</tr>
</table>
<?php
echo '<br>';
echo '<br>';
}
?>


Does anyone have a suggestion for me that would allow me to display this in three columns instead of one?

Fumigator
08-22-2006, 09:55 PM
You don't really need another loop. You just need to control when the "<tr>" and "</tr>" tags are printed. I would use the modulus operator on an incrementing variable like this.

I had to combine the book cover and book title into the same <td> tag to make this work, but there are other ways to format the data.



<?php
$i = 0;
<table width="33%" border="1" cellpadding="6">
foreach( $lookupResult['data'] as $key => $searchData ){
?>
<?php
if ($i == 0) {
print "<tr>\n";
}
?>
<td>
<img src="FX/image_proxy.php?FXimage=<? echo vignereEncryptURL($searchData['Cover'][0]); ?>"> <br />
<pre><b><?php echo $searchData['Title'][0];
?></b></pre>
<pre><?php echo $searchData['Contact'][0];
?></pre></td>

<?php
$i = ($i + 1) % 3;
if ($i == 0) {
print "</tr>\n";
}
?>
<?php
}
</table>
echo '<br>';
echo '<br>';
?>

aprestong
08-22-2006, 10:04 PM
Hi Fumigator-

Thanks for the reply - - I tried this out, but I'm still getting them in one column down the line....
I can sort of follow what you mean by controlling the <tr> tags - but I still can't seem to get it.... Your code is helping me think correctly, but it still isn't working...
thanks for the help!

Fumigator
08-22-2006, 10:08 PM
I just spotted two errors in the code I posted-- the <table> and </table> tags need to either be outside <?php ?> tags or they need to be echoed.

In dealing with this stuff I find it helpful to look at the page source to make sure PHP is outputting what I think it should be outputting. Try that; if it's not making sense to you then post the page source here and we can take a look.

aprestong
08-22-2006, 10:12 PM
Nice... I didn't realize what could be found by looking at the page source...
I just put the table tags outside php and its working wonderfully.
Thanks!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum