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 7 of 7
  1. #1
    Regular Coder
    Join Date
    Mar 2008
    Posts
    118
    Thanks
    3
    Thanked 9 Times in 9 Posts

    PHP Formatting Data from mySQL

    Hi,

    I am pulling data from an SQL database. The data is file paths for image files on the server, the below code shows how the image tags are assembled. I have no problem assembling the code and showing the pictures, however; if there are more than 4 pictures, the table that the pictures are in expands width-wise ruining my site layout.


    PHP Code:
    <table width="535"><tr>
    <?php

    //SQL Connection...

    $result mysql_query("SELECT * FROM photos WHERE picteam='Amherst'");

    while(
    $row mysql_fetch_array($result))
      {
    echo 
    '<td><img src="' $row['path'] . '" height="83" width="125" /></td>';
      }

    ?>

    </tr></table>
    As I said, the code works, but only for up to 4 pictures, because a new row for the table is not created. If I tried to add the <tr> tags in the loop, a new row would be created for each picture, which I don't want.

    I am simply (or not so simply) looking for a method to automatically create a new row so that there are four pictures per row.

    Thank you for your time,
    Petey
    Last edited by peteyb383; 03-01-2008 at 05:21 AM.

  • #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
    It is simple, really. Use the modulus (&#37 operator, which is like the division operator, but it returns the remainder of the division. For example, $a = 6 / 4 returns 1.5, but using the modulus like this: $a = 6 % 4 will return 2 (the remainder of the division).

    You may be wonding how the hell this applies to your situation?!? Well I'll tell you! If you increment a variable inside your loop that creates the <img> tag, then check the modulus of that variable % 4, it will only be 0 one out of every 4 times! So you can then put it in an "if" statement that says if modulus is 0, print the <tr> tag!

    PHP Code:
    <?php
    print "<table><tr>";
    for (
    $i 1$i <= mysql_num_rows($result); $i++) {
        if((
    $i 4) == 0) {  //time to print the <tr>
            
    print "</tr><tr>";
       }
        print 
    "<td><img . . . . .</td> " // blah blah <img> tag
    }
    print 
    "</tr></table>";
    ?>

  • Users who have thanked Fumigator for this post:

    peteyb383 (03-01-2008)

  • #3
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Hmm I think $i needs to be 0.
    PHP Code:
    <table width="535">
    <tr>
    <?php

    //SQL Connection...

    $result mysql_query("SELECT * FROM photos WHERE picteam='Amherst'") or die(mysql_error());
    $i 0;
    if(
    mysql_num_rows($result) > 0)
    {
        while(
    $row mysql_fetch_array($result))
        {
            if((
    $i 4) == && $i != 0)
            {
                echo 
    "</tr>\n<tr>\n";
            }
            echo 
    '<td><img src="' $row['path'] . '" height="83" width="125" /></td>';
            
    $i++;
        }
    }
    else
    {
        echo 
    "<td>There are no images to show.</td>\n";
    }
    ?>

    </tr></table>
    Last edited by _Aerospace_Eng_; 03-01-2008 at 06:33 PM.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • Users who have thanked _Aerospace_Eng_ for this post:

    peteyb383 (03-01-2008)

  • #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
    You'll have an empty <tr></tr> if you start with 0, if you start with 1 you won't.

  • #5
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Try it. He already starts with a <tr>. Output of your code
    Code:
    <table>
    <tr>
    <td>test</td>
    <td>test</td>
    <td>test</td>
    </tr>
    <tr>
    <td>test</td>
    <td>test</td>
    <td>test</td>
    <td>test</td>
    
    </tr>
    <tr>
    <td>test</td>
    <td>test</td>
    <td>test</td>
    <td>test</td>
    </tr>
    <tr>
    <td>test</td>
    <td>test</td>
    <td>test</td>
    
    <td>test</td>
    </tr>
    <tr>
    <td>test</td>
    <td>test</td>
    <td>test</td>
    <td>test</td>
    </tr>
    <tr>
    <td>test</td>
    <td>test</td>
    
    <td>test</td>
    <td>test</td>
    </tr>
    <tr>
    <td>test</td>
    </tr>
    </table>
    output with the code I posted
    Code:
    <table>
    <tr>
    <td>test</td>
    <td>test</td>
    <td>test</td>
    <td>test</td>
    </tr>
    <tr>
    <td>test</td>
    <td>test</td>
    <td>test</td>
    
    <td>test</td>
    </tr>
    <tr>
    <td>test</td>
    <td>test</td>
    <td>test</td>
    <td>test</td>
    </tr>
    <tr>
    <td>test</td>
    <td>test</td>
    
    <td>test</td>
    <td>test</td>
    </tr>
    <tr>
    <td>test</td>
    <td>test</td>
    <td>test</td>
    <td>test</td>
    </tr>
    <tr>
    <td>test</td>
    
    <td>test</td>
    <td>test</td>
    <td>test</td>
    </tr>
    </table>
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #6
    Regular Coder
    Join Date
    Mar 2008
    Posts
    118
    Thanks
    3
    Thanked 9 Times in 9 Posts
    Thank you, to the both of you for your help. Aerospaces' code works how I need it to, but thanks Fumigator.

    I was thinking of a mod idea when the problem arose, but i just didn't know how to implement it.

    Again thanks for the help,
    Petey

  • #7
    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
    Ah I see where I went wrong; you da man.


  •  

    Posting Permissions

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