passing variable in url

He has: 688 posts

Joined: Feb 2001

Here's my code

<?php
$query
= \"SELECT * FROM alumni WHERE state IN ($location)\";
?>

If I add this line above the query line, this will work correctly (and we'll call it the Pacific region):
<?php
$location
= \"'WA','OR','CA','NV'\";
?>

That's all fine, but what if I want to pass along a variable within the url (ie. "file.php?region=pacific") and then make the $loacation variable dependent on what's written in the url?

For instance, here's what I want to do in 'half-code' with the url containing "?region=something". I want to know how the properly syntax and format this concept:

----------------------------

if $region = pacific then $location = $pacific
$pacific = "'WA','OR','CA','NV'";

if $region = central then $location = $newengland
$newengland = "'CT','RI','MA','VT','NH','ME'";

and so forth in this style

----------------------------

How should I format/syntax the above "desired effect"?
Or am I going about this all wrong anyway, and maybe there's a better way to be doing this?

THANKS!

He has: 296 posts

Joined: May 2002

If I understand you correctly you don't want the state, just the region. If that's it then here is how I would do it:

<?php
// Define Locations...
$pacific = \"'WA','OR','CA','NV'\";
$newengland = \"'CT','RI','MA','VT','NH','ME'\";
//....... Add More if Desired

if (!isset(
$region)) {
 
$location = $pacific; // Default Region/Location
} else if (
$region == 'pacific') {
 
$location = $pacific;
} else if (
$region == 'centeral') {
 
$location = $newengland;
}
// ....... Continue Code
?>

[James Logsdon]

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

I would use an array...

<?php
$location
['pacific'] = \"'WA','OR','CA','NV'\";
$location['newengland'] = \"'CT','RI','MA','VT','NH','ME'\";

if (!in_array(
$region, array_keys($location))) {
 
$region = \"pacific\"; // Default Region/Location
}

$query = \"SELECT * FROM alumni WHERE state IN (\".$location[$region].\")\";
?>

Mark Hensler
If there is no answer on Google, then there is no question.

He has: 688 posts

Joined: Feb 2001

Thanks. Both look like they do exactly what I was looking for. I'll give 'em both a try.

Smiling

He has: 688 posts

Joined: Feb 2001

Both ways worked, but I happen to go with Mark's method. Works like a charm which is great because this was the last piece to my functionality puzzle and now I can work on design. Thanks again.

By the way, I'm proud to say that from your example which showed me the formatting, I was able to figure out how to do something else on my own. Yea, I'm a programmer now! Okay not quite - but I'm still proud that I found a solution to this problem: I also want to have individual states as a possible variable, but how do I do this without having to setup a different line for each state? Here's what I put at the end after the 'named regions':

<?php
$location
['self'] = \"'$region'\";

if (!in_array(
$region, array_keys($location))) {
 
$region = \"self\";
}
?>

Laughing out loud :D Laughing out loud

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

Very nice. Congrats!

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.