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:
  #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: 16
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,240 FrankieShakes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 18
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: 16
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: 16
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,240 FrankieShakes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 18
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: 16
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,240 FrankieShakes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 18
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: 16
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,240 FrankieShakes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 18
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: 16
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: 16
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,240 FrankieShakes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 18
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: 16
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,240 FrankieShakes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 18
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($loc);
        break;


function 
content($loc)
        {
            
the content function here... 



Try that... See if it works!

No problem... That's what I'm here for! I don't mind helping out at all! If you have any other problems, let me know!

Reply With Quote
  #15  
Old December 28th, 2002, 06:43 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: 16
Send a message via AIM to Vasarab69
ok that works beautifully....but now when i specify a bogus value in the address bar such as "index.php?loc=blablabla", i get an error saying that is cannot include '/home/vasarab/loc/blablabla.php' because it doesnt exist (failed opening etc. . . for inclusion)

is there a way to check if the file exists, and if it doesnt than just reset the variable to home (i mean, reset the $loc variable to the value of "home")?

i am just wondering if you know a way to code something like this off of the top of your head? in the meantime ill search the php.net manual for something because i have no clue as to how to go about this one

Reply With Quote
  #16  
Old December 30th, 2002, 06:08 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: 16
Send a message via AIM to Vasarab69
well ive solved my own problem but lets get this post off the board for good, eh?

when i try to redirect the user (if the file to which the variable points to doesn't exist) i use the "header" function and, of course, it outputs that the headers were already sent...

ive included the entire file and in the 'index.php' file i call the content like so:

PHP Code:
 $content content($loc);
include (
$content); 
Attached Files
File Type: txt loc_picker.php.txt (1,004 Bytes, 699 views)

Reply With Quote
  #17  
Old December 30th, 2002, 07:27 AM
wAr-AnGeL wAr-AnGeL is offline
Forum Security
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Location: Behind You
Posts: 479 wAr-AnGeL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 m 50 sec
Reputation Power: 17
Send a message via ICQ to wAr-AnGeL Send a message via AIM to wAr-AnGeL
did you also put in ob_end_flush(); ?
__________________




"Only Linux users see the end of crashes."
- Pl4t0

Reply With Quote
  #18  
Old December 30th, 2002, 07:48 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: 16
Send a message via AIM to Vasarab69
does that go at the end of each file thats involved or just loc_picker.php?

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingGeneral Programming Help > need help with the switch conditional and corresponding function


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap