morrism35 Posted November 5, 2015 Share Posted November 5, 2015 (edited) I have to build an image gallary using associative arrays. I'm thinking of using something like this. $Images==array("dog"=<"Image/Dog.jpg", "dragon=<"Image/Dragon.jpg"); Does this look correct or am I missing some concept. I'm also assuming I would use some type of looping function along with a img src to display the image. Edited November 5, 2015 by morrism35 Quote Link to comment Share on other sites More sharing options...
benanamen Posted November 5, 2015 Share Posted November 5, 2015 (edited) Is this homework? You only need the image name. foreach is your friend here. Edited November 5, 2015 by benanamen Quote Link to comment Share on other sites More sharing options...
morrism35 Posted November 5, 2015 Author Share Posted November 5, 2015 yes it is and no i'm not asking someone to write it for me. I was planning on using a foreach and display the img in a src img with the index within the tag. I think my logic is correct on this. Quote Link to comment Share on other sites More sharing options...
benanamen Posted November 5, 2015 Share Posted November 5, 2015 You will find the answer here: http://www.w3schools.com/php/php_arrays.asp Quote Link to comment Share on other sites More sharing options...
Barand Posted November 5, 2015 Share Posted November 5, 2015 BTW, "dog"=<"Image/Dog.jpg" should be "dog"=>"Image/Dog.jpg" Quote Link to comment Share on other sites More sharing options...
morrism35 Posted November 5, 2015 Author Share Posted November 5, 2015 Thanks i mistyped the actual syntax. Were i get confused in using images in arrays is in how does the program now were the images are to display.In other programming courses I never used a file on a web server. The paths to folders also confuse me. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted November 5, 2015 Share Posted November 5, 2015 Without helping you with your algorithm, I will point out your syntax errors with this: $Images = array("dog"=>"Image/Dog.jpg", "dragon=>"Image/Dragon.jpg");[/php[ 1 - just one = sign to make the assignment2 - as prev mentioned the array element operator(?) is => NOT =< An image gallery using arrays? Hmmm. Wonder how that is to be used long-term. If one really wants to create a gallery one must have a source for those images. Perhaps a folder? Then where do the labels come from in the array? Or are you building the array from user input in order to eventually save it as a file or a db table? Quote Link to comment Share on other sites More sharing options...
morrism35 Posted November 5, 2015 Author Share Posted November 5, 2015 yeah the images are on my web folder. I got everything to work but the img src syntax to display the image was very confusing. I had a classmate help me with that part of the program, but I'm trying to understand the logic of the syntax as regard to the displaying of the image. I understand the foreach and using the 'next' keyword. What really kills me in learning php is my lack of html knowledge. I've taken the html course but that was over a year ago, so I haven't used it since then. The problem with college is you take so many courses not relating to your major, that it takes away really learning what you really need to learn for your career. echo "<p><a href=\"Images/rat-zodiac.jpg\"><img src=\"Images/rat-zodiac.jpg"; foreach ($Array as $Sign) { echo "\" alt=\"" . $Sign . "\" height=\"125\" width=\"100\"/></a> " . $Sign . "<br /><a href=\"" . key($Array) . "\"><img src=\"" . key($Array); next($Array); Quote Link to comment Share on other sites More sharing options...
ginerjm Posted November 5, 2015 Share Posted November 5, 2015 I have no idea what you are showing us here, but I cleaned it up in order to begin asking questions about it. echo "<p><a href='Images/rat-zodiac.jpg'><img src='Images/rat-zodiac.jpg'"; foreach ($Array as $Sign) { echo "alt='$Sign' height='125' width='100'/>"; echo "</a> $Sign<br />"; echo "<a href='" . key($Array) . "'><img src='" . key($Array); next($Array); One can use single quotes and double quotes to make things easier. Note how I did that in your echos. You start by outputting an anchor tag, using an image as the visible "link" for that anchor. But you leave the img tag incomplete and start a loop on the array to add more code to it. Your loop takes the first array element and adds some more attributes to the img tag and then closes the anchor tag and then outputs the current array value. Then you start a NEW anchor tag but then you lose me. What is the purpose of "key($Array)"? You are already looping on the array so why the next call. And then you grab the key of an element too. Also - why would you want to turn control over to a jpg file with that anchor tag at the start of this code? 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.