General Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingGeneral Programming Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
You eat, breathe and sleep innovation. Build your mobile intelligence with BlackBerry® experts this July. Register Today!
  #1  
Old December 27th, 2002, 05:28 PM
Vasarab69 Vasarab69 is offline
Goldmember
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 71 Vasarab69 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 m 25 sec
Reputation Power: 6
Send a message via AIM to Vasarab69
Post need help with the switch conditional and corresponding function

ok well i am writing a script where, for example, a user clicks a link that is the following

"http://www.mydomain.com/home.php?loc=test", the page will display the template provided in home.php but in the body field of home.php is the variable $content...and that variable is set with the following function:

PHP Code:
function content()
        {
            if (
is_null($loc))
                {
                    
$loc "home";
                }
            else
                {
            
$loc $_GET[loc];
                }
            
$loc_path "/home/vasarab/public_html/loc/";
            require 
$loc_path $loc ".php";
        } 


hopefully that is clear enough but im obviously doing something wrong..so what is it?

and everything is working fine BUT i get tWO instances of the function...i get one that is displayed at the VERY top of the page above everything else and one that is where it should be...i use the switch conditional like so:

PHP Code:
switch ($loc)
    {
        case 
"home":
            
$title    =    "TITLE goes here";
            
$content    =    content();
        break;
        
// add more cases
        //
        
default:
            
$title    =    "TITLE goes here";
            
$content content();
        break;
    } 

and i echo the $content var out in the body of the page (where it should be displayed) with
PHP Code:
echo $content


thanks in advance
__________________
-Alexander

Last edited by Vasarab69 : December 27th, 2002 at 09:41 PM.

Reply With Quote
  #2  
Old December 27th, 2002, 11:04 PM
FrankieShakes FrankieShakes is offline
Frank The Tank!
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Jun 2002
Location: Toronto, Canada
Posts: 1,246 FrankieShakes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
Send a message via ICQ to FrankieShakes Send a message via MSN to FrankieShakes
Are you register_globals on or off? You should try using the $_GET[] superglobal to retrieve the value being passed from the URL...

Also, in your content() function, you should return the value that you want your $content variable to hold.
__________________
____________________________________________
Developer Shed Weekly Writer | DevArticles Forum Moderator
Build Your Own KlipFolio Klip With PHP
FrankManno.com - Under Construction
Design Interactive Group - Under Construction

Reply With Quote
  #3  
Old December 27th, 2002, 11:07 PM
Vasarab69 Vasarab69 is offline
Goldmember
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 71 Vasarab69 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 m 25 sec
Reputation Power: 6
Send a message via AIM to Vasarab69
register_globals are off...and ill try that now and get back to the board with the info

Reply With Quote
  #4  
Old December 27th, 2002, 11:23 PM
Vasarab69 Vasarab69 is offline
Goldmember
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 71 Vasarab69 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 m 25 sec
Reputation Power: 6
Send a message via AIM to Vasarab69
im not sure but i think the problem lies when i set the variable...how do i tell a variable to execute a function when its called? for example, i how do i tell the variable $content to call the function content() when its called...im using

$content = content();

echo $content;

and the function content() includes a specific file which depends on the variable (it would include the file 'test.php' if the address was ../home.php?loc=test)

and i am using the $_GET superglobal to retrieve the value of the "loc" variable...this time ive provided the complete code . . .

Reply With Quote
  #5  
Old December 27th, 2002, 11:35 PM
FrankieShakes FrankieShakes is offline
Frank The Tank!
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Jun 2002
Location: Toronto, Canada
Posts: 1,246 FrankieShakes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
Send a message via ICQ to FrankieShakes Send a message via MSN to FrankieShakes
Quote:
Originally posted by Vasarab69
im not sure but i think the problem lies when i set the variable...how do i tell a variable to execute a function when its called? for example, i how do i tell the variable $content to call the function content() when its called...im using

$content = content();

echo $content;

Well, that's fine... But in your content() function, you need to return a value that will be stored in your $content variable:

PHP Code:
function content()
        {
            if (
is_null($loc))
                {
                    
$loc "home";
                }
            else
                {
            
$loc $_GET[loc];
                }
            
$loc_path "/home/vasarab/public_html/loc/";
            require 
$loc_path $loc ".php";
            return [
b]the value you want stored in $content[/b]
        } 

Reply With Quote
  #6  
Old December 27th, 2002, 11:42 PM
Vasarab69 Vasarab69 is offline
Goldmember
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 71 Vasarab69 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 m 25 sec
Reputation Power: 6
Send a message via AIM to Vasarab69
the value of $content is the actual page to which the variable is referring...

for example, if the browser points to "../home.php?loc=test" then im trying to, basically, grab the file 'test.php' and include in the body of the page using the variable $content...which means im assigning the actual page 'test.php' as the value of content...and test.php contains nothing more than text, so really it could be "test.txt" or any other extension as long as i can insert it into the body of home.php . . . i hope i'm making sense though

Reply With Quote
  #7  
Old December 27th, 2002, 11:54 PM
FrankieShakes FrankieShakes is offline
Frank The Tank!
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Jun 2002
Location: Toronto, Canada
Posts: 1,246 FrankieShakes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
Send a message via ICQ to FrankieShakes Send a message via MSN to FrankieShakes
Okay... I get what you're trying to do...

Try this then:

PHP Code:
function content()
        {
            if (
is_null($loc))
                {
                    
$loc "home";
                }
            else
                {
            
$loc $_GET[loc];
                }
            
$loc_path "/home/vasarab/public_html/loc/";
            
$loc_path =  $loc_path $loc ".php";
            return 
$loc_path



And when calling it... try this:

$content = content();
include ($content);

HTH!

Reply With Quote
  #8  
Old December 28th, 2002, 12:01 AM
Vasarab69 Vasarab69 is offline
Goldmember
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 71 Vasarab69 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 m 25 sec
Reputation Power: 6
Send a message via AIM to Vasarab69
works like a charm! thanks a lot FrankieShakes! jeez u should write an article on this lol

Reply With Quote
  #9  
Old December 28th, 2002, 12:04 AM
FrankieShakes FrankieShakes is offline
Frank The Tank!
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Jun 2002
Location: Toronto, Canada
Posts: 1,246 FrankieShakes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
Send a message via ICQ to FrankieShakes Send a message via MSN to FrankieShakes
No problem... Any time!

Hehehe... I might just do that!

Reply With Quote
  #10  
Old December 28th, 2002, 04:17 AM
Vasarab69 Vasarab69 is offline
Goldmember
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 71 Vasarab69 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 m 25 sec
Reputation Power: 6
Send a message via AIM to Vasarab69
now all that is working but ive encountered a new feature i'd like to add...can i assign a variable as a case? so this way i wont have to make a case for every value of the $loc variable, i would just have to make the file...

with the above code at hand, couldnt i make:

PHP Code:
case $loc:
            
$title    =    "Title goes here";
            
$content    =    content();
        break; 


i could, but its not working..when i use this code it just executes the 'default' case and not the one i'm trying to access . . . any workarounds?

Last edited by Vasarab69 : December 28th, 2002 at 01:15 PM.

Reply With Quote
  #11  
Old December 28th, 2002, 05:51 PM
Vasarab69 Vasarab69 is offline
Goldmember
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 71 Vasarab69 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 m 25 sec
Reputation Power: 6
Send a message via AIM to Vasarab69
or is that not possible?

thanks--

Reply With Quote
  #12  
Old December 28th, 2002, 06:18 PM
FrankieShakes FrankieShakes is offline
Frank The Tank!
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Jun 2002
Location: Toronto, Canada
Posts: 1,246 FrankieShakes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
Send a message via ICQ to FrankieShakes Send a message via MSN to FrankieShakes
I'm not sure if I understand what you mean... In your switch/case statements, you'll need to compare each "case" with the value that's being passed from the link:

ex: loc=home
loc=contact, etc...

Reply With Quote
  #13  
Old December 28th, 2002, 06:22 PM
Vasarab69 Vasarab69 is offline
Goldmember
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 71 Vasarab69 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 m 25 sec
Reputation Power: 6
Send a message via AIM to Vasarab69
right...but isnt there a way to dynamically create a case (not permanently, just on-the-fly) and refer it to an already existing page (this way it would save a lot of time)...

i was thinking something along the lines of this would work:
PHP Code:
switch ($loc)
        case 
$loc;
            
$content content();
        break; 


(since $loc was extracted from the web address using superglobal $_GET[loc]) and remember, the function that is above this in the "source file" defines what file should be included depending on what the value of 'loc' is in the address, or else it just includes the main file (index.php; which is included via the 'default' case in the switch)

if it replaced that replaced the switch variable i previously posted it should work i think but it doesnt, so im not sure what is wrong? also, since that DOES replace the 'old' switch conditional i posted previously, in the code, i kept the 'default' case of course... sorry for wasting so much of your time FrankieShakes but you've played a very big rrole in helping me find my inner-web developer

Last edited by Vasarab69 : December 28th, 2002 at 06:25 PM.

Reply With Quote
  #14  
Old December 28th, 2002, 06:32 PM
FrankieShakes FrankieShakes is offline
Frank The Tank!
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Jun 2002
Location: Toronto, Canada
Posts: 1,246 FrankieShakes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
Send a message via ICQ to FrankieShakes Send a message via MSN to FrankieShakes
Okay,

I think I understand what you mean now... What you'll need to do then is pass the value of $loc to the content() function like so:

PHP Code:
switch ($loc)
        case 
$loc;
            
$content content(