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
https://forums.phpfreaks.com/topic/145067-array-help/
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
https://forums.phpfreaks.com/topic/145067-array-help/#findComment-761210
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
https://forums.phpfreaks.com/topic/145067-array-help/#findComment-761211
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.