andrewgarn Posted May 13, 2008 Share Posted May 13, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/105501-php-photo-gallery-from-mysql/ Share on other sites More sharing options...
Loldongs Posted May 13, 2008 Share Posted May 13, 2008 <?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 Quote Link to comment https://forums.phpfreaks.com/topic/105501-php-photo-gallery-from-mysql/#findComment-540413 Share on other sites More sharing options...
andrewgarn Posted May 14, 2008 Author Share Posted May 14, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/105501-php-photo-gallery-from-mysql/#findComment-540469 Share on other sites More sharing options...
DarkWater Posted May 14, 2008 Share Posted May 14, 2008 Both of those are possibly with PHP, but you need the GD library installed. Verify that you do. Quote Link to comment https://forums.phpfreaks.com/topic/105501-php-photo-gallery-from-mysql/#findComment-540471 Share on other sites More sharing options...
andrewgarn Posted May 14, 2008 Author Share Posted May 14, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/105501-php-photo-gallery-from-mysql/#findComment-540476 Share on other sites More sharing options...
DarkWater Posted May 14, 2008 Share Posted May 14, 2008 Yes, you have it enabled. Now, if you could get Barand in here, he'd be the better authority on GD. He seems to know a LOT about it. Google "php gd thumbnail tutorial" in the meantime. Quote Link to comment https://forums.phpfreaks.com/topic/105501-php-photo-gallery-from-mysql/#findComment-540478 Share on other sites More sharing options...
DeanWhitehouse Posted May 14, 2008 Share Posted May 14, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/105501-php-photo-gallery-from-mysql/#findComment-540488 Share on other sites More sharing options...
DarkWater Posted May 14, 2008 Share Posted May 14, 2008 Except the image might be skewed like that. It should work for most purposes though. Quote Link to comment https://forums.phpfreaks.com/topic/105501-php-photo-gallery-from-mysql/#findComment-540489 Share on other sites More sharing options...
Loldongs Posted May 14, 2008 Share Posted May 14, 2008 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/105501-php-photo-gallery-from-mysql/#findComment-540490 Share on other sites More sharing options...
Loldongs Posted May 14, 2008 Share Posted May 14, 2008 Take a look at this tutorial on how to resize images with php using GD http://icant.co.uk/articles/phpthumbnails/ Quote Link to comment https://forums.phpfreaks.com/topic/105501-php-photo-gallery-from-mysql/#findComment-540498 Share on other sites More sharing options...
Loldongs Posted May 14, 2008 Share Posted May 14, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/105501-php-photo-gallery-from-mysql/#findComment-540500 Share on other sites More sharing options...
DarkWater Posted May 14, 2008 Share Posted May 14, 2008 Yeah, that's what I was getting at, Loldongs. GD is much better. Quote Link to comment https://forums.phpfreaks.com/topic/105501-php-photo-gallery-from-mysql/#findComment-540502 Share on other sites More sharing options...
andrewgarn Posted May 14, 2008 Author Share Posted May 14, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/105501-php-photo-gallery-from-mysql/#findComment-540504 Share on other sites More sharing options...
Loldongs Posted May 14, 2008 Share Posted May 14, 2008 Well if your only displaying 4 images per page it wont be an issue Quote Link to comment https://forums.phpfreaks.com/topic/105501-php-photo-gallery-from-mysql/#findComment-540508 Share on other sites More sharing options...
andrewgarn Posted May 14, 2008 Author Share Posted May 14, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/105501-php-photo-gallery-from-mysql/#findComment-540512 Share on other sites More sharing options...
DarkWater Posted May 14, 2008 Share Posted May 14, 2008 Now that's different. We thought you were going to paginate it, not load it all and hide it. Quote Link to comment https://forums.phpfreaks.com/topic/105501-php-photo-gallery-from-mysql/#findComment-540513 Share on other sites More sharing options...
andrewgarn Posted May 14, 2008 Author Share Posted May 14, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/105501-php-photo-gallery-from-mysql/#findComment-540518 Share on other sites More sharing options...
DarkWater Posted May 14, 2008 Share Posted May 14, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/105501-php-photo-gallery-from-mysql/#findComment-540521 Share on other sites More sharing options...
andrewgarn Posted May 14, 2008 Author Share Posted May 14, 2008 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? } Quote Link to comment https://forums.phpfreaks.com/topic/105501-php-photo-gallery-from-mysql/#findComment-540524 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.