Different linked files in same php include?

Hello,

Thinking there must be some way to do this. Some conditional php statement? I put my flash menu in a php include. Although I just realized, that each page has its own linked .swf menu/file (e.g. home.swf, aboutus.swf, etc). Each has its own accompanying .swf in order to show the current page in the menu. So… its there some magic way to call each different .swf file depending on the page selected? Or do I just have to take it out of the include? Thanks!

So here is the flash link in the include…

<param name="movie" value="aboutus.swf">

Something like this:


<?php

switch($_SERVER['REQUEST_URI'])
{
    case 'index.php':
        $swf = 'home.swf';
        break;
    case 'about.php':
        $swf = 'aboutus.swf';
        break;
    // etc
}

?>
<param name="movie" value="<?php echo $swf; ?>">

or more concise:

<?php

$swfMap = array(    '/index.php' => 'home.swf',
                    '/about.php' => 'aboutus.swf'
                    // etc
);
$swf = (array_key_exists($_SERVER['REQUEST_URI'], $swfMap)) ? $swfMap[$_SERVER['REQUEST_URI']] : 'home.swf';

?>
<param name="movie" value="<?php echo $swf; ?>">

Cool! Thank you very much. So I could just put that PHP statement in the include and above the embed code right? And that looks like it should work fine with the same include in different directories right? Or only the second one (because of the forward slash)?

Yes the directory you put the include in won’t matter. Just add/change the paths to match whatever $_SERVER[‘REQUEST_URI’] happens to be for each page. If you are going to put that into an include I would wrap it into a function to avoid polluting the global space, i.e. put something like this in your include:

<?php

function getFlashMenuForPage()
{
    $swfMap = array(    '/index.php' => 'home.swf',
                        '/about.php' => 'aboutus.swf'
                        // etc
    );
    return (array_key_exists($_SERVER['REQUEST_URI'], $swfMap)) ? $swfMap[$_SERVER['REQUEST_URI']] : 'home.swf';

?>

then you can call it by:

<param name="movie" value="<?php echo getFlashMenuForPage(); ?>">

Nice! Out of curiosity, how do you mean? “pollute global space”? Like white space?

If you assign a variable outside of a function or a class, it has global scope. So in that example we declare an $swf variable. Now say you include that file, but then include a bunch of other files and one of them has an $swf variable. The former value of $swf would be overwritten. Wrapping it in a function protects that from happening.

Ahhh. Ok thank you very much. Have a good one!

Hello,

OK I was finally just able to fully test these. Unfortunately none of them worked. The function one throws an error. And the other two just land on home.swf each time. Any ideas? Thanks!

I left out a closing } in the function - you’ll need to add that. You’ll also need to modify the $swfMap array to match your urls and flash file names. Beyond that you’ll have to post your code.

Here is the code from the include. Just tested it. This was in my aboutus.html page but it’s showing the home.swf menu. All the names are correct. What do you think?

Also, whats this bit doing? $swfMap[$_SERVER[‘REQUEST_URI’]] : ‘home.swf’; Thanks!

<?php

function getFlashMenuForPage()
{
    $swfMap = array(    '/index.php' => 'home.swf',
                        '/aboutus.php' => 'aboutus.swf'
                        // etc
    );
    return (array_key_exists($_SERVER['REQUEST_URI'], $swfMap)) ? $swfMap[$_SERVER['REQUEST_URI']] : 'home.swf';
}
?>
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="250" height="400">
			<param name="movie" value="<?php echo getFlashMenuForPage(); ?>">
			<param name="wmode" value="opaque">
			<!--[if !IE]>-->
			<object type="application/x-shockwave-flash" data="<?php echo getFlashMenuForPage(); ?>" width="250" height="400">
			<param name="wmode" value="opaque">
			<!--<![endif]-->
				alt content
			<!--[if !IE]>-->
			</object>
			<!--<![endif]-->
			</object>

As I alluded to, you will need to change the $swfMap array to match your urls. So the first array item is:


'/index.php' => 'home.swf' 

you’ll need to alter the ‘/index.php’ to whatever $_SERVER[‘REQUEST_URI’] equals for the given page. not sure what it should be? Put

echo $_SERVER['REQUEST_URI'];

at the top of the page in question and copy/paste that.

That makes the default .swf file '‘home.swf’ in case $_SERVER[‘REQUEST_URI’] does not match anything in the array. Which is probably the case right now because you haven’t customized it to your needs yet.

Oh god. Can’t believe I missed that. I of course thought the names were correct. However they’re not .php extensions. They’re .html extensions. Ok I’ll edit that and I’m sure it will work. Thanks for your patients. I’ll post back with my progress.

OK. That works absolutely perfect now. Thank you very much for the awesome solution! :slight_smile:

Quick question? Since I know nothing of this inner workings… This will work in all browsers right? And do you think it slows anything down - or is that just instantaneous just like a direct link would be?

…also. I have ten other pages that all use the same swf. Do I have to state each ten times? Or is there some trick were I can get away saying it once?

maaan! I’m unfortunately stuck again. In my pages in sub folders I get errors.

I get this error. Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ‘)’ in /home/building/public_html/inc/nav.inc on line 6

I’m running this code.
<?php
function swf()
{
$swfMap = array(
‘/index.html’ => ‘home.swf’,
‘/aboutus.html’ => ‘aboutus.swf’
‘/popup/parents1.html’ => ‘/popup/parents-inner.swf’ // line 6
// etc
);
return (array_key_exists($_SERVER[‘REQUEST_URI’], $swfMap)) ? $swfMap[$_SERVER[‘REQUEST_URI’]] : ‘home.swf’;
}
?>

How can I call the sub folder links within this same include? feeeken path question. I’m embarrassed. But I tried everything.

Scratch my last path question. Im retarded and was missing a cama. I blame it on the include (.inc) has no code highlighting. So I just have the two remaining quick questions then…

  1. Since I know nothing of this inner workings… This will work in all browsers right? And do you think it slows anything down - or is that just instantaneous just like a direct link would be?

  2. I have ten other pages that all use the same swf. Do I have to state each ten times? Or is there some trick were I can get away saying it once?

Thanks!

PHP runs on the server so it is not subject to browser issues. Slowness is definitely not even a remote concern with that little snippet of code.

On this line:

return (array_key_exists($_SERVER[‘REQUEST_URI’], $swfMap)) ? $swfMap[$_SERVER[‘REQUEST_URI’]] : ‘home.swf’;

‘home.swf’ is the default if no match is found in the array. so you could change that to the swf that is on the majority of your pages and make that the default.

That’s a good idea. Thanks a ton aamonkey I owe you one. Take care!

Alright aamonkey I got one more for you. I think this will be the last. In my sub folder (the one needing the same swf x 10) there is a swf called inner.swf. Now I know why it was used. Because it’s links are relative. Meaning it’s links only point to out of the sub folder and all the swfs in the root only point the opposite. In other words there is no default swf that will link to all the pages (root and sub folders). So the default swf (currently home.swf) is of no use. So how would that same PHP snippet look without the fallback swf? I could probably play with it but I’d rather get it from you and know it’s fullproof. Thanks again!

Scratch another one. I figured it out. I used the same code and figured out how to make all the paths work.