Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Manage Variables by URI
Old 05-03-2009, 12:28 PM Manage Variables by URI
GeekSpecialties's Avatar
Super Talker

Posts: 132
Name: Leonard
Location: Minnesota, USA
Trades: 0
I want to set a couple (probably 4) of variables depending which page the visitor is on.
I have a bout 13 pages and thinking therem ust be a cleaner way to do this.

I'm new with PHP but this is what I managed to put together so far.

PHP Code:
<?php
         $headimg 
"home.jpg";
        
$contactpage "/contact.php";
        
$homepage "/index.php";
        
$imgaddress "http:/MyWebsite.com/images/";
        
        
$currentpage $_SERVER['REQUEST_URI'];
        if(
$homepage==$currentpage
            
$headimg "home.jpg";
            
$var2 "sidebar.php";
        elseif (
$contactpage==$currentpage)
            
$headimg "test2.jpg";
            
$var2 "sidebar2.php";
?>  
<div id="header">
<?php
echo " <img src=\"$imgAddress$headimg\" />";
?>
</div> 
<?php include($var2); ?>
GeekSpecialties is offline
Reply With Quote
View Public Profile Visit GeekSpecialties's homepage!
 
 
Register now for full access!
Old 05-03-2009, 04:08 PM Re: Manage Variables by URI
Super Talker

Posts: 130
Trades: 0
Your if check is always going to set $var2 = "sidebar2.php". The reason is when you do an IF without curly braces, it only takes the very next statement. Below is how you should write the if statement.

PHP Code:
        if($homepage==$currentpage) { 
            
$headimg "home.jpg";
            
$var2 "sidebar.php";
        } elseif (
$contactpage==$currentpage) {
            
$headimg "test2.jpg";
            
$var2 "sidebar2.php";
        } 
__________________
flann

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
flann is offline
Reply With Quote
View Public Profile
 
Old 05-03-2009, 06:23 PM Re: Manage Variables by URI
GeekSpecialties's Avatar
Super Talker

Posts: 132
Name: Leonard
Location: Minnesota, USA
Trades: 0
Thanks.
I'm wondering if there is a better way than elseif?
Other wise I will have 13 elseif conditions.
GeekSpecialties is offline
Reply With Quote
View Public Profile Visit GeekSpecialties's homepage!
 
Old 05-03-2009, 07:07 PM Re: Manage Variables by URI
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Alternative to if..elseif..else

PHP Code:
switch($currentpage)
{
     case 
$homepage:
          
$heading "home.jpg";
          
$var2 "sidebar.php";
     break;
     case 
$contactpage:
          
$headimg "test2.jpg";
          
$var2 "sidebar2.php";
     break;
     .
     .
     .
     default:
     
//default case (analogous to else)

Do you really have 13 seperate sidebar.php files? You might want to look into a way to consolidate those.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by NullPointer; 05-03-2009 at 07:37 PM..
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 05-03-2009, 11:32 PM Re: Manage Variables by URI
GeekSpecialties's Avatar
Super Talker

Posts: 132
Name: Leonard
Location: Minnesota, USA
Trades: 0
What would be the advantage of using switch?

No I don't have 13 sidebars, the variables will change either images or text on different parts of included sections.

For example, the header section is an include, 90% of the header I want the same on every page, but I want an image and sub-text to be relevant to the page content.
GeekSpecialties is offline
Reply With Quote
View Public Profile Visit GeekSpecialties's homepage!
 
Old 05-03-2009, 11:39 PM Re: Manage Variables by URI
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Switch is just an alternative syntax. I prefer using switch statements in cases where a single variable can take several forms, but it really just depends on what you prefer.

I don't know of any advantage performance wise. If there is any difference I'm sure it is neglegable.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 05-04-2009, 05:58 PM Re: Manage Variables by URI
GeekSpecialties's Avatar
Super Talker

Posts: 132
Name: Leonard
Location: Minnesota, USA
Trades: 0
Anybody else have any ideas?
I want to get this done this week and don't want to redo it later.
GeekSpecialties is offline
Reply With Quote
View Public Profile Visit GeekSpecialties's homepage!
 
Reply     « Reply to Manage Variables by URI
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.26302 seconds with 12 queries