Displaying Images from MySQL Database in Rows and Columns

P

phpgirl

Guest
As the title says, I have images under different IDs in a MySQL database that I would like to display 4 per row and 12 total on a page. So far the page is set up like this:

PHP:
<?php

$hostname = "localhost";
$user = "xyz";
$pass = "xxxxx";
$dbname = "whatever";

$connection = mysql_connect("$hostname" , "$user" , "$pass");
$db = mysql_select_db($dbname , $connection) or die ("Cannot select db");

$query = "select * from guestbook order by date desc ";
$result = mysql_query($query) or die(mysql_error());

// dynamic navigation variables
$screen = $_GET['screen'];
$PHP_SELF = $_SERVER['PHP_SELF'];

$rows_per_page=12;
$total_records=mysql_num_rows($result);
$pages = ceil($total_records / $rows_per_page);

if (!isset($screen))
$screen=0;
$start = $screen * $rows_per_page;
$query .= "LIMIT $start, $rows_per_page";
$result= mysql_query($query) or die
("Could not execute query : $query." . mysql_error());

while ($row = mysql_fetch_array($result))
{
    extract($row);
    // display data here..
}

// create the dynamic links
if ($screen > 0) {
$j = $screen - 1;
$url = "$PHP_SELF?screen=$j";
echo "<a href=\"$url\">Prev</a>";
}


// page numbering links now
for ($i = 0; $i < $pages; $i++) {
$url = "$PHP_SELF?screen=" . $i;
$j = $i + 1;
echo " | <a href=\"$url\">$j</a> | ";
}

if ($screen < $pages-1) {
$j = $screen + 1;
$url = "$PHP_SELF?screen=$j";
echo "<a href=\"$url\">Next</a>";
}

Except, the above code only works when you want something displayed one per row and ___ per page (deemed $rows_per_page). Since I would like 4 per row, I need help modifying the code to display that accordingly. Any help would be greatly appreciated. Thank you very much for at least taking the time to read through my problem. =)
 
phpgirl,
I'm sorry I don't have a code example to give you, all my work was for my previous employer.

I think what you are seeing is because you dumping out the records, instead of parsing each and every image (while determining whether each image position should be put in a new row, is being added to the current row, or is being used at the end of the current row.

To make it work, you will need to have PHP generate the html tags used for beginning new rows, inserting table cells, and then ending rows. Of course, you have to manage the so called printing or echoing of this information before and after when each image is displayed.

Once you do that, you will be able to create tables. Of course, you want the table rows to be created dynamically, that way if you have one image that runs into an extra page, you won't end up with 47 extra table cells that are empty.

Just create a 3x3 table, drop some words (aaa, bbb, ccc, ddd, eee, fff, ggg, hhh, & iii) into all the cells, and then reverse engineer how the html code is used to assemble that.

You should be able to figure out the rest. One of the other issues I tackled, was wrapping my head around when an item was considered a new row image, additional image, or end row image. If you can figure that part out as well, you'll be in good shape.

Good luck. :D
 
I know this is an old thread, but I am trying to achieve the same results. Here's my code:

PHP:
while ($row = mysql_fetch_array($result))

{

echo "&nbsp;";
echo "<td>";
echo "<img src=\"images/$row[element_5]\" border=\"1\" width='$maxwidth' alt=\"$row[element_1]\" title=\"$row[element_1]\">";
echo "<p align=\"center\"/>"; 
echo "<a href=\"$row[element_2]\"> $row[element_1]</a>";

}

echo "</table>";
mysql_close($connection);

I need to create multiple rows and have images fill the screen based on the resolution.
 
Back
Top