Page 1 of 1
Outputting multiple data from mysql in single <tr>
Posted: Sat Oct 20, 2007 2:26 pm
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
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>";
Posted: Sat Oct 20, 2007 2:29 pm
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>';
Posted: Sat Oct 20, 2007 2:37 pm
by markusn00b
Yeh i thought about that (and probably will do that) but i want the satisfaction of learning something from this..

Posted: Sat Oct 20, 2007 3:05 pm
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.
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);
Posted: Sat Oct 20, 2007 3:14 pm
by markusn00b
No idea how that works, but thanks

Posted: Sat Oct 20, 2007 3:17 pm
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..

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..
Posted: Sat Oct 20, 2007 3:19 pm
by superdezign
markusn00b wrote:No idea how that works, but thanks

Then you've learned
nothing.

Make sure you understand it.
Posted: Sat Oct 20, 2007 4:45 pm
by markusn00b
superdezign wrote:markusn00b wrote:No idea how that works, but thanks

Then you've learned
nothing.

Make sure you understand it.
In the process

and thanks!
Use simple counter variable
Posted: Tue Oct 23, 2007 2:22 pm
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>";
Re: Use simple counter variable
Posted: Tue Oct 23, 2007 4:04 pm
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>
Good practic but not needed
Posted: Tue Oct 23, 2007 6:40 pm
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.