PHP looping help needed guys....

Discussion in 'PHP' started by ceylongeek, Nov 14, 2010.

  1. #1
    Guys I need to create a PHP looping section like a gallery.I need to repeat some boxes as to the database content.The number of boxes vary as to the database items.I have attached a image with this for your ease.

    I can get database content to variables.But hard to understand a way to do this repeat part.If you have a idea or a tutorial please post it here.I want to repeat like on my attached picture.I mean 2 horizontal(columns) and other all for vertical(rows).

    Waiting for a solution :confused:
    Thank you.

    loop.jpg
     
    ceylongeek, Nov 14, 2010 IP
  2. vaidikkp

    vaidikkp Peon

    Messages:
    72
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    according to wat u have posted above, i suppose ur requirement is to get some images from a database and then according to the number of images you get, you want to make an organized image gallery (like images in a table).

    you will hv to use nested loops for this. i am not going to write the whole script but just a structure that will help u understand how to go about it:

    $query = mysql_query(<your query comes here>);

    $ctr = 0;
    while ($row = mysql_fetch_array($query))
    {
    $ctr++;

    if (($ctr%2)!=0)
    {
    print "<img src=\"" . $row[<column name for image location> . "\">";
    }
    print "<img src=\"" . $row[<column name for image location> . "\">";
    print "<br />";
    }
    }

    Now the above script gives you just a basic idea of how to go about it. It inserts two images in each row and then changes the row by printing line brake tag (<br />). You can also implement this using a table like this:

    $query = mysql_query(<your query comes here>);

    $ctr = 0;

    <table>
    <tr>
    while ($row = mysql_fetch_array($query))
    {
    $ctr++;

    if (($ctr%2)!=0)
    {
    print "<td><img src=\"" . $row[<column name for image location> . "\"></td>";
    }
    print "<img src=\"" . $row[<column name for image location> . "\"></td>";
    print "<br />";
    print "</tr>";
    print "<tr>";
    }
    }
    </tr>
    </table>

    Now obviously this script can be greatly improved. But now you atleast have an idea how to do it. Also, change the if-condition according to the number of images you need in a row. For example: if you want to print 4 images in a row, then the if-condition will look like this:

    if (($ctr%4)!=0)

    I hope this answers your question! :)
     
    vaidikkp, Nov 14, 2010 IP
  3. ceylongeek

    ceylongeek Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3

    Absolutely that's what I wanted brother.Thank you very much for your fast tutorial.Friend I understood everything roughly and I will start modifying the script to my requirements.Finally after I launch my site I'll definitely send you a private message with a link to my site.So you can be happy for your help.


    Thank you very much dude.
     
    ceylongeek, Nov 14, 2010 IP