Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Get correct variable contents from page using $_GET param

Posted on 2010-01-02
10
Medium Priority
?
237 Views
Last Modified: 2013-12-13
I have the following

$var1 = "this is the first one";
$var2 = "this is the second one";
$var3 = "this is the third one";

I want to select the variable text, based on a GET parameter.    so for example if the get variable is set to '2', then I want it to echo "This is the second one"  The _GET will be dynamically set.

I didnt want to use if statements here.
Thank you.
0
Comment
Question by:debbieau1
  • 4
  • 3
  • 2
  • +1
10 Comments
 

Accepted Solution

by:
sohibe1911 earned 1000 total points
ID: 26164137

$i = $_GET['anything'];
switch ($i) {
    case 1:
        echo $var1;
        break;
    case 2:
        echo $var2;
        break;
    case 3:
        echo $var3;
        break;
}

Open in new window

0
 
LVL 11

Expert Comment

by:VanHackman
ID: 26164140

Assuming that you use the variable "Text" in the $_GET array, and that variable have just a numeric value (1,2,3);

Just:



<?php

$var1 = "this is the first one";
$var2 = "this is the second one";
$var3 = "this is the third one";

// Here we get it...
$text = $(var.$_GET["Text"]);

// Here we show it...

echo ( ((!empty($text)) ? $text : "Text Not Found...") );


?>

Open in new window

0
 
LVL 1

Author Comment

by:debbieau1
ID: 26164165
thanks for your quick response but I get a parse error on line 8, which is the problem I was having
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 1000 total points
ID: 26164271
This tests out OK for me.  Please install it and see if you understand all the moving parts.  If not, please post back and we can try to help.  Best regards, ~Ray
<?php // RAY_temp_switch_GET.php
error_reporting(E_ALL);
echo "<pre>\n";

// PREDEFINED VARS
$var1 = "this is the first one\n";
$var2 = "this is the second one\n";
$var3 = "this is the third one\n";

// IS THERE A GET STRING?
if (!empty($_GET["q"]))
{
    switch ($_GET["q"])
    {
        case 1:
            echo $var1;
            break;
        case 2:
            echo $var2;
            break;
        case 3:
            echo $var3;
            break;
        default:
            echo "PLEASE ENTER ONLY 1, 2, OR 3";
            break;
    }
}

// DROP OUT OF PHP AND PUT UP THE HTML FORM
?>
<form>
PLEASE ENTER 1, 2, OR 3 HERE:
<input name="q" />
<input type="submit" />
</form>

Open in new window

0
 
LVL 11

Expert Comment

by:VanHackman
ID: 26164467
Sorry, I didn't put some quotes. Any way, I think that my method is pretty much simple and efficient. ;)

Remember: Assuming that you use the variable "Text" in the $_GET array, and that variable have just a numeric value (1,2,3);

Here the correct code:
<?php

$var1 = "this is the first one";
$var2 = "this is the second one";
$var3 = "this is the third one";

// Here we get it...
$text = $("var".$_GET["Text"]);

// Here we show it...

echo ( ((!empty($text)) ? $text : "Text Not Found...") );


?>

Open in new window

0
 
LVL 1

Author Comment

by:debbieau1
ID: 26164645
Yes it is simple but unfortunately I still get a parse error.  
 
It refers to the ( on this line
$text = $("var".$_GET["option"]);  
0
 
LVL 1

Author Closing Comment

by:debbieau1
ID: 31672054
thank you
0
 
LVL 11

Expert Comment

by:VanHackman
ID: 26165466
The people can't wait a little bit... U_U
<?php

$var1 = "this is the first one";
$var2 = "this is the second one";
$var3 = "this is the third one";

// Here we get it...
$text = ("var".$_GET["Text"]);

// Here we show it...

echo ( ((!empty($$text)) ? $$text : "Text Not Found...") );


?>

Open in new window

0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 26167042
Thanks for the points; it's a good question and a good teaching example for the right way to do it.  As far as the "efficiency" of code like that goes, all I can say is that the speed of your server is about 100,000,000 times faster than a motivated typist.  So you can optimize this code all you want, you still cannot make a difference of more than 1/100,000,000 to your client audience.  They pay me to tell them stuff like that ;-)

Best regards, ~Ray
0
 
LVL 11

Expert Comment

by:VanHackman
ID: 26167988
U_U

Oh sad confusion... "Efficiency" is not completely about execution time, it is about Productivity too...
So... Use 17 lines to solve a problem that can be solved just in 2 lines, means that a "motivated typist" can write the "2 lines solution" 8.5 times in the same time that another "motivated typist" use to write the "17 lines" solution once.   Time is money, that is what I would to say. =)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question