jcran Posted September 22, 2008 Share Posted September 22, 2008 I have a database that I need to process images from. The database has 2 fields for images, one is the number of images the other is a link to the first image. For example, if the number of images = 5 and the image link is http://www.website.com/image.jpg then the script would need to do this. Show the first image from http://www.website.com/image.jpg and parse out the other 4 images like so; http://www.website.com/image2.jpg http://www.website.com/image3.jpg http://www.website.com/image4.jpg http://www.website.com/image4.jpg The problem is, the images will be located in several different places on a different server and the image type could be .bmp, .jpeg, .gif and so on. Can anyone help on this one? Thanks, Jason Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted September 22, 2008 Share Posted September 22, 2008 You mean like.. you could get from the database "there are 5 images" and "the first one is something1.jpg" and you want the last 4, but they can be anywhere on the website? /dir/something2.jpg, /somethingelse/deeper/something3.bmp, /anotherplace/here/there/something4.gif, etc... or what? I'm not quite sure what you need the script to do. Quote Link to comment Share on other sites More sharing options...
jcran Posted September 22, 2008 Author Share Posted September 22, 2008 The field that has the number of images could be anywhere from 1 to 10. This field is just telling how many photos are associated with this listing. Then, the other field is the link to the first photo. So, if there is only 1 photo, basically nothing needs to be done. But, if there are more than one photo, the script would need to add additional links to those photos. The photos will always be in the same directory, for a given listing, but the name of the photo would have a _2 _3 _4 and so on, added to the end. What I meant by different places is; one listing may have photos in http://www.website.com/photos/somethingelse/blahblah/photo.jpg and another listing may have photos in http://www.website.com/photos/somethingelse/anotherdirector/photo.jpg The photos for each unique listing will all be in the same directory though, but will have _# added to it. For example if one listing has 3 photos, the first link will read http://www.website.com/photos/somethingelse/blahblah/photo.jpg and the script will need to create the other two links to be http://www.website.com/photos/somethingelse/blahblah/photo_2.jpg and http://www.website.com/photos/somethingelse/blahblah/photo_3.jpg Thank you for your response Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted September 22, 2008 Share Posted September 22, 2008 Thanks for clarifying, you did a great job of it. what I'd do, is split the string at the final period (.) - see the string manipulation family of functions (http://us.php.net/strings) particularly functions such as .. http://us.php.net/manual/en/function.strrchr.php http://us.php.net/manual/en/function.strstr.php http://us.php.net/manual/en/function.substr.php http://us.php.net/manual/en/function.strrpos.php .. of course you won't need to use all of these functions, but just telling you the ones you should use wouldn't be fun for you! this will yield you http://www.website.com/photos/somethingelse/blahblah/photo jpg As you've noticed these are the components you need for your script - the name of the file, and the file type. After this it's a simple matter of looping through the components how ever many times the script tells you to (see: for($i = 0; $i < $numPhotos; ++$i){}) and appending _num to the end of the name-of-the-file string. You can then recombining it with the file type, add it to an array of file names, and move onto the next file in the listing (if it exists). In the end you'll end up with an array of the filenames required. I figured I'd give it to you in words instead of code as to not steal the fun of writing it yourself feel free to ask questions, we'd love to help. Quote Link to comment Share on other sites More sharing options...
jcran Posted September 22, 2008 Author Share Posted September 22, 2008 I am so new to writing php, I wouldnt know where to begin. I am just an advanced html and pretty good css coder. I would love to learn how to write php, but all I have ever done is cut and paste stuff and I have a pretty good understanding how it works. Could you give me an idea of how to start the script, thank you so much for the help? Quote Link to comment Share on other sites More sharing options...
nadeemshafi9 Posted September 22, 2008 Share Posted September 22, 2008 just read the filename, get the servers name put it together and store it in the db Quote Link to comment Share on other sites More sharing options...
jcran Posted September 22, 2008 Author Share Posted September 22, 2008 I dont need to store anything in the database, the database is already intact with the data. Quote Link to comment Share on other sites More sharing options...
nadeemshafi9 Posted September 22, 2008 Share Posted September 22, 2008 I dont need to store anything in the database, the database is already intact with the data. if you do a if(file_exists("http://".server."/".file)) this will resolve it u can check to see if its on any of the servers and if its a bmp ping etc only then output it corectly comprehendo amigo ? il be back Quote Link to comment Share on other sites More sharing options...
Mr_J Posted September 22, 2008 Share Posted September 22, 2008 Could you give me an idea of how to start the script, thank you so much for the help? <?php Quote Link to comment Share on other sites More sharing options...
jcran Posted September 22, 2008 Author Share Posted September 22, 2008 As sarcastic as that is, it is funny. LOL I am a little more advanced than that, but thanks for the laugh! Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted September 22, 2008 Share Posted September 22, 2008 <?php I lol'd. Anyway, here's something to get you started.... and maybe finished. <?php $numImages = 5; $location = 'http://www.domain.com/test.jpg'; $dotLoc = strrpos($location, '.'); $file = substr($location, 0, $dotLoc); $ext = substr($location, $dotLoc + 1); $imgLocations = array(); for($i = 1; $i <= $numImages; ++$i) { $tmp = $file; if($i != 1) { $tmp .= "_$i"; } $tmp .= ".$ext"; $imgLocations[] = $tmp; } print_r($imgLocations); ?> You didn't get the fun but try to figure out how it works Quote Link to comment Share on other sites More sharing options...
nadeemshafi9 Posted September 23, 2008 Share Posted September 23, 2008 <?php count = count of files filename = filename serverlist = array(server1,server2,server3) filetypes = array(bmp,png,jpg) for(i=0;i<count;i++){ foreach(serverlist as server){ foreach(filetype as type){ if(i==0)if(file_exists(server."/".filename.".".filetype)) echo server."/".filename.".".filetype) if(file_exists(server."/".filename.i.".".filetype)) echo server."/".filename.i.".".filetype) } } } ?> Quote Link to comment Share on other sites More sharing options...
nadeemshafi9 Posted September 23, 2008 Share Posted September 23, 2008 if u already know the types in the filename leave out the types bit and do if(file_exists(server."/".ereg_replace(".", i.".", filename)) echo server."/".ereg_replace(".", i.".", filename) Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted September 23, 2008 Share Posted September 23, 2008 if u already know the types in the filename leave out the types bit and do if(file_exists(server."/".ereg_replace(".", i.".", filename)) echo server."/".ereg_replace(".", i.".", filename) he said he didn't :X besides, those ridiculously long lines are impossible to read not that they aren't impressive Quote Link to comment Share on other sites More sharing options...
nadeemshafi9 Posted September 23, 2008 Share Posted September 23, 2008 if u already know the types in the filename leave out the types bit and do if(file_exists(server."/".ereg_replace(".", i.".", filename)) echo server."/".ereg_replace(".", i.".", filename) he said he didn't :X besides, those ridiculously long lines are impossible to read not that they aren't impressive if your gona do a one liner you dont need brackets and if your explaining somthing you do psudo programming cos its never gona be perfect so to just get the juist of it. thnks for saying its impressive lol i come across these problems daily im a developer full time been for 1 year got first class degree, wot bout u ? Quote Link to comment 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.