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
markusn00b
Forum Contributor
Posts: 298 Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England
Post
by markusn00b » Sat Oct 20, 2007 2:26 pm
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>";
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Sat Oct 20, 2007 2:29 pm
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>';
markusn00b
Forum Contributor
Posts: 298 Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England
Post
by markusn00b » Sat Oct 20, 2007 2:37 pm
Yeh i thought about that (and probably will do that) but i want the satisfaction of learning something from this..
superdezign
DevNet Master
Posts: 4135 Joined: Sat Jan 20, 2007 11:06 pm
Post
by superdezign » Sat Oct 20, 2007 3:05 pm
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);
markusn00b
Forum Contributor
Posts: 298 Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England
Post
by markusn00b » Sat Oct 20, 2007 3:14 pm
No idea how that works, but thanks
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Sat Oct 20, 2007 3:17 pm
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..
superdezign
DevNet Master
Posts: 4135 Joined: Sat Jan 20, 2007 11:06 pm
Post
by superdezign » Sat Oct 20, 2007 3:19 pm
markusn00b wrote: No idea how that works, but thanks
Then you've learned
nothing .
Make sure you understand it.
markusn00b
Forum Contributor
Posts: 298 Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England
Post
by markusn00b » Sat Oct 20, 2007 4:45 pm
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!
churt
Forum Commoner
Posts: 39 Joined: Wed Oct 04, 2006 9:59 am
Post
by churt » Tue Oct 23, 2007 2:22 pm
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>";
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Tue Oct 23, 2007 4:04 pm
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>
churt
Forum Commoner
Posts: 39 Joined: Wed Oct 04, 2006 9:59 am
Post
by churt » Tue Oct 23, 2007 6:40 pm
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.