Jump to content

Switch Question


lenny

Recommended Posts

Hello all...I'm making a gallery with images and I have this show image page, with php using the switch statement so all thumbs is linking to it using ?img=something. It works allright and like it's supposed to, but how well does a switch work with many cases(like 100, 200 or more) ?Would a database(or similar) work better, faster and more effective, than a switch with alot of cases in the php file?Also, I'm new to php.I hope you understand what I mean...Thanks... :)

Link to comment
Share on other sites

Yes, a database would work so much better, and you wouldnt have to add new lines of code everytime you added an image to the database.basically you would have to create a database with a few columns, one being an index(id), the other being the filename, and i dont know if your gallery is set so that every user can allow for uploads, but yes, a database would be easier.here is the basic SQL query you can use, add whatever you like and come back later for help:

CREATE TABLE `images`(`img_id` int unique not null auto_increment,`img_name` varchar(255));

Have fun lenny

Link to comment
Share on other sites

Thanks man. I have set up a mysql database but I have some trouble with what code I should use.I got this working but I'm not sure if it's the right way to do it:

<?php$input = $_GET['img'];$con = mysql_connect('stuff','stuff','stuff');if (!$con)  {  die('Could not connect: ' . mysql_error());  }mysql_select_db('stuff', $con);$result = mysql_query("SELECT img_name,width,height FROM images WHERE img_id='$input'");$output = mysql_fetch_array($result);echo '<img src="images/' . $output['img_name'] . '" width="' . $output['width'] . '" height="' . $output['height'] . '" alt="" />';mysql_close($con);?>

Also, I want it to write 'Image not found' if the '?img=' is wrong or something, but I don't know how. Any suggestions?And is there any security things I should know or do? :) Can the '?img=something' contain underscores , dots etc. ?Thanks again for any help... :)

Link to comment
Share on other sites

Good start, to answer your questions, yes the ?img=something can have underscores, but the entire point of the ?img=something is to have something equal to a number, so there wouldnt need to be any spaces.And yes there is a way to have it say "image not found";

<?php$input = $_GET['img'];$con = mysql_connect('stuff','stuff','stuff') or die("connection error<br/>".mysql_error());@mysql_select_db('stuff', $con) or die("Could not select database".mysql_error());$result = mysql_query("SELECT img_name,width,height FROM images WHERE img_id='$input'");if(mysql_num_rows($result)>0){$output = mysql_fetch_array($result);echo '<img src="images/' . $output['img_name'] . '" width="' . $output['width'] . '" height="' . $output['height'] . '" alt="" />';}else{ echo "image not found, no record sets match input";}mysql_close($con);?>

Also, you might want to do a quick check to see if $_GET['img'] is even set. something like if(isset($_GET['img']) && $_GET['img']!=null){//Your base texts here}Should do fine.

Link to comment
Share on other sites

if(isset($_GET['img']) && $_GET['img']!=null){//Your base texts here}
Isn't
if(!empty($_GET['img'])

a shorter variation of that?

Link to comment
Share on other sites

If you go the database route, make sure you escape your browser variables.

$input = mysql_escape_string($_GET['img']);

Actually, I don't know if a database is really the way to go. It involves adding entries when you upload new photos etc.If you properly escape and clean (again: BIG emphasis on ESCAPE and CLEAN) your input, I don't see what's wrong with passing filenames through the browser and fetching the image from a directory. As long as you restrict access to a specific folder (that holds only pictures) then you can live without a database backend. Just upload to that directory and it's all set I've made a gallery with this method that even allows browsing through subdirectories (and ONLY those within that first gallery). Various comments and stuff I put in the EXIF tag.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...