|Please Help Layman Wth Simple? PHP Problem|
| 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?
| 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";
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:
|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.
| 6:26 am on Jun 16, 2012 (gmt 0)|
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! :)
| 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; ?>">