Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: nested loops?

  1. #1
    Regular Coder
    Join Date
    Jul 2006
    Posts
    110
    Thanks
    1
    Thanked 0 Times in 0 Posts

    nested loops?

    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:
    Code:
    <?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?

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    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 Code:
    <?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>';
    ?>

  • #3
    Regular Coder
    Join Date
    Jul 2006
    Posts
    110
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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!

  • #4
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    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.

  • #5
    Regular Coder
    Join Date
    Jul 2006
    Posts
    110
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •