Outputting multiple data from mysql in single <tr>

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Outputting multiple data from mysql in single <tr>

Post by markusn00b »

I'm creating a 'recently uploaded' script for my website.

What i want is: to pull the url of uploaded images out of the db and insert them in a table. I want 3 images per row.

Problem: The code below just pulls one image per row, and i have puzzled over this, literally for hours, to find the way i would go about doing it - all to no avail!

Anybody got an idea? Thanks :D

Code: Select all

$db = "mahcuz";
$ser = "localhost";
$pass = "";
$usr = "root";
mysql_connect($ser, $usr, $pass) or die ('Error connecting to mysql');
echo "Connected<br />";
 
mysql_select_db($db) or die('error connection');
echo "db connected<br /><br />";
 
$query = "SELECT * FROM `ma_pics` WHERE `private` = 'no' ORDER BY `Util_Uploaded` DESC LIMIT 10";
$res = mysql_query($query);
 
echo "<table>";
while($row = mysql_fetch_array($res))
{
    echo "
    <tr>
        <td>{$row['url']}</td>
    </tr>";
}
echo "</table>";
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

My favorite way is to use floats, this way you want to have to bother with the logic of figuring out building a table.

Code: Select all

while ($row = mysql_fetch_assoc($res)) {
   //each div will take up 33% of the width of it's container, if it cannot fit it will automatically display on the next row
   echo '<div style="float: left; width: 33%;">'. $row['url'] .'</div>';
}

//clear the floats to avoid weird things happening
echo '<div style="clear: both;"></div>';
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Post by markusn00b »

Yeh i thought about that (and probably will do that) but i want the satisfaction of learning something from this..

:P
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

markusn00b wrote:Yeh i thought about that (and probably will do that) but i want the satisfaction of learning something from this..
Then... figure it out. :P

Code: Select all

do {
    echo '<tr>';
    for ($counter = 0; $counter < 3, $data = mysql_fetch_object($result); $counter++) {
        echo '<td>' . $data->whatever . '</td>';
    }
    echo '</tr>';
} while ($data);
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Post by markusn00b »

No idea how that works, but thanks :D
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

markusn00b wrote:Yeh i thought about that (and probably will do that) but i want the satisfaction of learning something from this..

:P
What else is there to learn about it?

A div width set to 33% will take 33% of it's container, 33% + 3 columns = 99%. Since the fourth item won't fit on the first row, it will be forced into the second row in which the process repeats..
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

markusn00b wrote:No idea how that works, but thanks :D
Then you've learned nothing. :P
Make sure you understand it.
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Post by markusn00b »

superdezign wrote:
markusn00b wrote:No idea how that works, but thanks :D
Then you've learned nothing. :P
Make sure you understand it.
In the process :) and thanks!
User avatar
churt
Forum Commoner
Posts: 39
Joined: Wed Oct 04, 2006 9:59 am

Use simple counter variable

Post by churt »

A simple counter with some conditionals will do it.

Code: Select all

$db = "mahcuz";
$ser = "localhost";
$pass = "";
$usr = "root";
mysql_connect($ser, $usr, $pass) or die ('Error connecting to mysql');
echo "Connected<br />";
 
mysql_select_db($db) or die('error connection');
echo "db connected<br /><br />";
 
$query = "SELECT * FROM `ma_pics` WHERE `private` = 'no' ORDER BY `Util_Uploaded` DESC LIMIT 10";
$res = mysql_query($query);
 
echo "<table>";
$ct=1;
while($row = mysql_fetch_array($res))
{
    if($ct==1) echo "<tr>";
    echo "<td>{$row['url']}</td>";
    if($ct==3){ echo "</tr>"; $ct=0; }//Change comparison number to adjust columns per row
    $ct++;
}
echo "</table>";
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Use simple counter variable

Post by John Cartwright »

churt wrote:A simple counter with some conditionals will do it.

Code: Select all

$db = "mahcuz";
$ser = "localhost";
$pass = "";
$usr = "root";
mysql_connect($ser, $usr, $pass) or die ('Error connecting to mysql');
echo "Connected<br />";
 
mysql_select_db($db) or die('error connection');
echo "db connected<br /><br />";
 
$query = "SELECT * FROM `ma_pics` WHERE `private` = 'no' ORDER BY `Util_Uploaded` DESC LIMIT 10";
$res = mysql_query($query);
 
echo "<table>";
$ct=1;
while($row = mysql_fetch_array($res))
{
    if($ct==1) echo "<tr>";
    echo "<td>{$row['url']}</td>";
    if($ct==3){ echo "</tr>"; $ct=0; }//Change comparison number to adjust columns per row
    $ct++;
}
echo "</table>";
A quick note, this snipplet will create invalid html if the number of element is not divisable by 3 because it will be missing a </tr>
User avatar
churt
Forum Commoner
Posts: 39
Joined: Wed Oct 04, 2006 9:59 am

Good practic but not needed

Post by churt »

The last "</tr>" is good practice but not absolutely needed. The "</table>" element at the end will suffice to terminate the table. If you would rather have an ending "<tr>" you could use the following.

Code: Select all

while($row = mysql_fetch_array($res))
{
    if($ct==1) $display.="<tr>";
    $display.="<td>{$row['url']}</td>";
    if($ct==3){ $display.="</tr>"; $ct=0; }//Change comparison number to adjust columns per row
    $ct++;
}
if(substr($display,-5)!=='</tr>') $display.='</tr>';
echo $display."</table>";
I believe that's right but I'm not where I can test it.
Post Reply