Gallery Table Loop

phpnutphpnut Junior MemberShared Hoster
Hey guys, I'm in a bind and thought this'd be the best place to ask for help.
I have a MySQL database containing image URLs that I need displayed in a table that is three images wide (for thumbnails). My dilemma is that I cannot think of how to create a loop that will stop after three rows of the database, add variables into HTML table code, and then restart to do another three rows. Any kind of help would be greatly appreciated.
My ongoing projects...
www.naturesmagazine.com
www.energyreform.org *new domain*
www.photographyavenue.com
----
You may also remember me as imnewtophp...

Comments

  • PythonPython Forum Leader The Royal RAM
    [PHP]<?php

    echo '<table width="100%" border="0" cellspacing="0" cellpadding="5"><tr>';

    $res = mysql_query(YOUR QUERY);
    $count = 0;
    while($data = mysql_fetch_array($res))
    {
    $count++;
    if($count % 4 == 0)
    echo '</tr><tr>';

    echo '<td width="33%">';

    ?>
    IMAGE HERE
    <?php
    echo '</td>';
    }

    echo '</tr></table>';
    ?>[/PHP]

    Give that a go...

    The Royal Ram

  • phpnutphpnut Junior Member Shared Hoster
    I've put together something that looks like it should work, but it isn't for some reason...

    [PHP]<?php
    $gallery = strip_tags($_GET);
    $dbhost = "****";
    $dbuser = "****";
    $dbpass = "****";
    $db = "siteinfo";
    $connect = mysql_connect($dbhost,$dbuser,$dbpass)
    or die("No can do");
    $selectdb = mysql_select_db($db,$connect)
    or die("sorry");
    $query = "SELECT id,thumb,gallery FROM photographs WHERE gallery = '$gallery'";
    $res = mysql_query($query)
    or die("haha");
    $cnt = mysql_num_rows($res);
    echo "<table border='0' cellspacing='1' cellpadding='1'>";
    for($i=0; $i<$cnt; $i++)
    {

    echo "<tr>";


    for($cols=0; $cols<3; $cols++)
    {
    $row = mysql_fetch_array($res,MYSQL_ASSOC);

    echo '<td>';
    echo $row;
    echo '</td>';
    }

    echo '</tr>';
    }
    echo "</table>";

    ?>[/PHP]
    My ongoing projects...
    www.naturesmagazine.com
    www.energyreform.org *new domain*
    www.photographyavenue.com
    ----
    You may also remember me as imnewtophp...
  • PythonPython Forum Leader The Royal RAM
    Try my code ;)

    It should work - I was using that on a previous site I was working on.

    The Royal Ram

  • phpnutphpnut Junior Member Shared Hoster
    I've tried your code too python, but nothing's happening...
    [PHP]<?php
    echo*'<table*width="100%"*border="0"*cellspacing="0"*cellpadding="5"><tr>';
    $gallery = $_GET;
    $dbhost = "*****";
    $dbuser = "*****";
    $dbpass = "*****";
    $db = "siteinfo";
    $connect = mysql_connect($dbhost,$dbuser,$dbpass)
    or die("No can do");
    $selectdb = mysql_select_db($db,$connect)
    or die("sorry");
    $query = "SELECT id,gallery,tinythumb FROM photographs WHERE gallery = 'wildlife'";
    $res = mysql_query($query);
    $count*=*0;
    while($data*=*mysql_fetch_array($res))
    {
    ****$count++;
    ****if($count*%*4*==*0)
    ********echo*'</tr><tr>';

    ****echo*'<td*width="33%">';
    ****
    echo "{$data}";
    echo*'</td>';
    }

    echo*'</tr></table>';
    ?>[/PHP]
    My ongoing projects...
    www.naturesmagazine.com
    www.energyreform.org *new domain*
    www.photographyavenue.com
    ----
    You may also remember me as imnewtophp...
  • BeachieBeachie Moderator: Raging Bender Link Clerk
    are you getting something that doesn't look right, or nothing at all? if nothing, then check you don't have a bug elsewhere (sql error??).

    this should work, but i haven't tested it:
    [php]
    <?php
    $gallery = strip_tags($_GET);
    $dbhost = "****";
    $dbuser = "****";
    $dbpass = "****";
    $db = "siteinfo";
    $connect = mysql_connect($dbhost,$dbuser,$dbpass) or die("No can do");
    $selectdb = mysql_select_db($db,$connect) or die("sorry");

    $query = "SELECT id,thumb,gallery FROM photographs WHERE gallery = '$gallery'";
    $pics = mysql_query($query);
    ?>
    <table width="200" border="0">
    <?
    $counter = 0;
    while ($pic = mysql_fetch_array($pics))
    {
    $counter++;
    if ($counter == 1) print("<tr>");
    ?>
    <td><? print($pic["thumb"]) ?></td>
    <?
    if ($counter == 3) { print("</tr>"); $counter = 0; }
    }
    // close the table row if we ran out of data
    if ($counter > 0) print("</tr>");
    ?>
    </table>[/php]
    Gadget.com | Everybody.com | BulletinBoard.com | Stupidity.com | Aquatic.com | Cuddly.com | Blogs.org | Newcomers.com | Wealth.org
Sign In or Register to comment.