Jump to content

array help


Scott87

Recommended Posts

I have php file which displays images based on what the page name is in the URL.

 

the code is:

 

<?php
$rightbox = array(
  'faqs' => '_img/faqanswers.jpg',
  'contact-us' => '_img/contactsuccessful.jpg',
  'login' => '_img/loginexperience.jpg',
);

$page = isset($_GET['page']) ? $_GET['page'] : 'home'; // default to home page
echo "<img src=\"{$rightbox[$page]}\" />";
?>

 

I'm trying to add more images to each section, like so:

 

$rightbox = array(
  'faqs' => '_img/faqanswers.jpg',
  'contact-us' => '_img/contactsuccessful.jpg', '_img/contactpotential.jpg', '_img/contactwinner.jpg', 
  'login' => '_img/loginexperience.jpg',
);

 

Obviously without any luck, how would I go about inserting the extra images into the code?

 

Cheers

Link to comment
Share on other sites

You could either make each image a different contact us:

 

$rightbox = array(

  'faqs' => '_img/faqanswers.jpg',

  'contact-us' => '_img/contactsuccessful.jpg',

  'contact-us2' => '_img/contactpotential.jpg',

  'contact-us3' =>  '_img/contactwinner.jpg',

  'login' => '_img/loginexperience.jpg',

);

 

or a multidimensional array:

 

$rightbox = array(

  'faqs' => '_img/faqanswers.jpg',

  'contact-us' => array('1' => '_img/contactsuccessful.jpg', '2' => '_img/contactpotential.jpg', '3' => '_img/contactwinner.jpg'),

  'login' => '_img/loginexperience.jpg',

);

 

Link to comment
Share on other sites

Not sure exactly what you're trying to achieve: multiple images for each page

<?php
$rightbox = array(
    'faqs' => '_img/faqanswers.jpg',
    'contact-us' => array('_img/contactsuccessful.jpg', '_img/contactpotential.jpg', '_img/contactwinner.jpg'), 
    'login' => '_img/loginexperience.jpg',
);

$page = isset($_GET['page']) ? $_GET['page'] : 'home'; // default to home page
foreach($rightbox[$page] as $pageImage)
   echo "<img src=\"{$pageImage}\" />";
}
?>

or new pages:

$rightbox = array(
    'faqs' => '_img/faqanswers.jpg',
    'contact-us' => '_img/contactsuccessful.jpg', 
    'contact-potential' => '_img/contactpotential.jpg', 
    'contact-winner' => '_img/contactwinner.jpg', 
    'login' => '_img/loginexperience.jpg',
);

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.