Jump to content

PHP Photo Gallery - from mysql?


andrewgarn

Recommended Posts

I'm working on a website where one of the features is a photo gallery which new photos can be uploaded into it and photos deleted etc.

 

I have an upload page which can upload the file to a folder, whilst saving the following information to mysql table "photo":

 

photo id (unique prim auto inc)

uploaderid

filename

notes

 

I can quite easily get a query which will select the photos from database via photo id and echo their links onto the page:

$query = ("SELECT * from photo");
$result = mysql_query($query);

while($r=mysql_fetch_array($result)) 
{ 
$uploaderid=$r["uploaderid"];
$filename=$r["filename"];
$notes=$r["notes"];
        ?><img src="<? filename ?>" alt="<? $notes ?>" /><?
}

 

My question is how would I format these images into a gallery type page?

e.g. display two across and 2 down per page, with links created to display the next 4 photos?

 

Sorry if this is confusing, ask if anything needs clarifying.

 

Thanks :)

Link to comment
Share on other sites

<?php
$query = ("SELECT * from photo");
$result = mysql_query($query);

$rows = mysql_num_rows($results);
$i = 0;

echo '<table>';

while($r=mysql_fetch_array($result)) 
{ 
if (($i == 0) || ($i == 2)) {
	echo '<tr>';
}


$uploaderid=$r["uploaderid"];
$filename=$r["filename"];
$notes=$r["notes"];
    sprintf('<td><img src="%s" alt="%s" /></td>', $filename, $notes);


if (($i == 1) || ($i == 3)) {
	echo '</tr>';
}

$i++;
}

echo '</table>';


?>

 

Use sprintf for html with variables its much easier

Link to comment
Share on other sites

Thanks that worked, except for the sprintf part which didnt seem to do anything so i replaced it with my original code (could you tell me the advantage of sprintf over ?> a href <? ? )

 

Is there any way for php to generate thumbnails? or could i just assume all the photos being uploaded are the same dimension ratio and set fixed size for the image code?

 

Thats a little confusing, so what I want to do is:

 

either create a thumbnail when the image is uploaded? no idea if php can do this

 

OR stick dimensions on the image so it doesnt appear at full size on the page?

Link to comment
Share on other sites

Both of those are possibly with PHP, but you need the GD library installed.  Verify that you do.

 

My two servers it needs to work on

 

Server 1

 

GD Support 	enabled
GD Version 	2.0 or higher
FreeType Support 	enabled
FreeType Linkage 	with freetype
FreeType Version 	2.1.10
GIF Read Support 	enabled
GIF Create Support 	enabled
JPG Support 	enabled
PNG Support 	enabled
WBMP Support 	enabled

 

Server 2

GD Support 	enabled
GD Version 	bundled (2.0.34 compatible)
FreeType Support 	enabled
FreeType Linkage 	with freetype
FreeType Version 	2.1.9
T1Lib Support 	enabled
GIF Read Support 	enabled
GIF Create Support 	enabled
JPG Support 	enabled
PNG Support 	enabled
WBMP Support 	enabled
XBM Support 	enabled

 

This seems to be a yes to enabled?

Link to comment
Share on other sites

sprintf just keeps it cleaner example below

 

so say an sql query

 

<?php $sql = "SELECT * FROM `members` WHERE `username` = '". $username ."' AND `password` = '". $password ."' "; ?>

 

this way is alot more cleaner below

 

<?php $sql = sprintf("SELECT * FROM `members` WHERE `username` = '%s' AND `password` = '%s'", $username, $password); ?>

Link to comment
Share on other sites

i done a much simiplar way, i just have <img src="<?php echo $(image_link ?>" width="200px" />

this makes a kindoff thumbnail, i have this inside the link and this is a much simiplar, file efficent and flexible way of doing it

 

What about if the image is 10mb you would be loading that 10mb image and making it 200px that would be a waste of your users bandwidth

Link to comment
Share on other sites

Thank you for your input, for the moment i will use the fixed width, if i get time later in the project I will look into GD but it looks quite complicated from first glance

 

A question on the code for generating the tables though.

 

The if function says i= 0,1,2 or 3 but what happens when i > 3? I dont see it being set back to 0 anywhere?

 

 

Link to comment
Share on other sites

How could the code by modified to generate divs (each containing 4 images) which could be set to block/hide by javascript when clicking on page 1, page 2 links?

 

 

Oh and i'm currently using DB4FREE.net to host my mysql database, which has been very reliable (but incredible slow) up until today where it keeps crashing.

 

Can anyone recommend a good free one?

Link to comment
Share on other sites

Oh and i'm currently using DB4FREE.net to host my mysql database, which has been very reliable (but incredible slow) up until today where it keeps crashing.

 

Can anyone recommend a good free one?

 

Well having show/hide on the page would be easier than creating 10 php pages with the same code?

 

Or can php create pages on the fly? ive never tried that. - UNLESS its something like php get where you can click next and it loads the same page but with new variables?

 

show/hide is probably easier for me

Link to comment
Share on other sites

You NEED to create them on fly.  It's the best way.  If you create all those DIVs and such, it'll be cluttered, and the client will need to download ALL those thumbnails even if they only view 4.

 

Retrieve number of photoid from database?

 

if function that generates links that load the same page, the the photoids of 1-4 first link, 5-8 second link etc?

 

if(isset($_GET["cmd"])) {
if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
{
	//retrieve the ids parsed
                //load their filenames using while function on a query
       }
show link for next page - parsing the relevant photo ids?
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.