Jump to content

Image gallery using associative array


morrism35

Recommended Posts

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 by morrism35
Link to comment
Share on other sites

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 assignment

2 - 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?

Link to comment
Share on other sites

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);
Link to comment
Share on other sites

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?

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.