homepage Welcome to WebmasterWorld Guest from 184.73.104.82
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Please Help Layman Wth Simple? PHP Problem
old_expat

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 4465837 posted 11:27 am on Jun 15, 2012 (gmt 0)

I have a few websites that I have built myself over the years and use PHP on many pages. What I use is really simple stuff like includes, etc.

One a couple of sites I 'store' information that I can echo in navigation menus and multiple places on a page. At the top of each page, I have something like this:


<?php $name = "some name";
$image = "some-image.jpg";
$link = "some link";
$location = "some location";
$num ="6 digit number";
?>

My current problem is that I don't always have the '6 digit number' added to the page -- I have to match a list and add them one at a time. So in those cases, I would like to be able to have an 'echo' or some code that will look for the number, and if '$num' does not exist, then it would use '$image'.

I have about 1500 pages and I'm slowly adding the '$num' to each, but in the interim I would like an option, as the '$num' is a better resource than '$image'.

And one of the 2 images need to load when/after a PHP include adds a navigation menu. Is that a problem?

Thanks :)

 

rocknbil

WebmasterWorld Senior Member rocknbil us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4465837 posted 4:05 pm on Jun 15, 2012 (gmt 0)

Though I don't really get it, I get some of it that might help. Enter the ternary operator - this is a one line equivalent of "if/else" - if condition is true, use the first value, otherwise use the value after the colon. So

in those cases, I would like to be able to have an 'echo' or some code that will look for the number, and if '$num' does not exist, then it would use '$image'.


// First set it to null OR if it exists somewhere
$num=([some condition])?[value from 'some condition']:null;
// then your other variables
$image = "some-image.jpg";
$num =($num)?$num:$image;

As for "some condition",

I have to match a list and add them one at a time.


Programmatically? Let's say you have an include* that "gets" this list and creates an array. In that case you could do this:

// remove the initialize to null mentioned above and just do this one line:
$num=(in_array($num,$your_list))?$num:$image;

*then

And one of the 2 images need to load when/after a PHP include adds a navigation menu.


It shouldn't be a problem, in fact, any variables in your include will become available to your current page. So in the above example, your include could be the common place where you define your "match list" array.

old_expat

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 4465837 posted 6:26 am on Jun 16, 2012 (gmt 0)

Hi rocknbil,

I probably didn't explain my problem very well, and maybe should have said --

1 - I have menus that I include on every page in a given group of pages. I load those with PHP includes.

2 - I want to insert an image that is unique to every page. So the code to do that need to be in the menu.

It looks like your second example is better for me. I'll give it a try. Actually, I'll test both. Thanks much for your help! :)

rocknbil

WebmasterWorld Senior Member rocknbil us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4465837 posted 2:39 pm on Jun 16, 2012 (gmt 0)

$imagedir = '/images/';
$images = array(
'home' => 'home.jpg',
'about' => 'about.jpg',
'contact' => 'contact.jpg'
);

// determine some way to get the page identifier and store in $page
$default_image = "some-image.jpg";
$image=($page and array_key_exists($page,$images))?$images[$page]:$default_image;

<img src="<php echo "$imagedir/$image"; ?>" alt="<?php echo $page; ?>">

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved