Jump to content

php / images


Scott87

Recommended Posts

Just join up here so I'd like to say HI.

 

I'm currently a PHP beginner so I won't pretend to understand stuff :D

 

I'm learning and know the basics so far but I'm having a bit of a dilemma:

 

 

I currently have a banner image, which is static on each page.

 

The pages are generated via a CMS / PHP etc.

 

I want to change the banner image on each page, so e.g. Home - img1, About - img2, Contact - img3 etc.

 

 

Whats the best way to do this?

 

 

Link to comment
Share on other sites

Well you can use a switch.

 

switch($page){
case 'Home':
echo "<img src='img1.jpg' />";
break;
case 'About':
echo "<img src='img2.jpg' />";
break;
default:
echo "<img src='img1.jpg' />";
break;
}

 

Or you can name your images home.jpg, about.jpg then do this:

 

//Grab the page from the address bar
$page = $_GET['page'];

if($page == ""){
$page = "home";
}

$page = strtolower($page); //Make it lowercase, just incase.
echo "<img src='" . $page . ".jpg' />";

 

Hope that makes sense.

 

Or you could use an if statement, which is basically a switch.

Link to comment
Share on other sites

Here's one more idea for you:

<?php
$banners = array(
  'home' => 'image1.jpg',
  'about' => 'image2.jpg',
  'contact' => 'image3.jpg'
);

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

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.