0

In my page, there are multiple $_GET values. ie

if(isset($_GET["projects"]))
{ .....    }
else if(isset($_GET["research"]))
{ ...... }
else if(isset($_GET["publication"]))
{ ..... }

...upto 10 elseif's

Can I shorten this? Can I get these values {projects,research, publication,..} in a variable.?

Nisfan
  • 149
  • 3
  • 15
  • 2
    They are already in a variable, you can just use them as is. If you want to set another variable with those values, you can simply assign normally `$projects = $_GET['projects']`... not sure what you are asking. – Havenard Aug 30 '14 at 05:41
  • 2
    Hard to provide an answer without more details about what you are trying to accomplish. – Mark Miller Aug 30 '14 at 05:42
  • Why not use one variable with "what to do" as the value? The primary cause comes because of the *disjoint operations* (as recognized by `else if`) based on the first of the set of variables which is present.. – user2864740 Aug 30 '14 at 05:42
  • And why are you using `else if` rather than one variable with a value you can check? – Ignacio Vazquez-Abrams Aug 30 '14 at 05:43
  • I need a code like $type=$_GET. – Nisfan Aug 30 '14 at 05:46
  • You are having trouble to do something as simple as assign the value of a variable to another? – Havenard Aug 30 '14 at 05:48
  • if i use, as you say, it will be like this .. : `if($_GET["abc"]) type="abc"; else if($_GET["sss"]) $type="sss"; ....etc. – Nisfan Aug 30 '14 at 05:54
  • Provide more details about what input you expect. Will there only be a single value in `$_GET` with changing key name? Or will there be many values in `$_GET` but one of them will be `projects` or `research` etc.? – deceze Aug 30 '14 at 05:57
  • @NisfanSabith are you trying to check if all values in a form have been filled out?? –  Aug 30 '14 at 06:03
  • The real question is this - is there more data being submitted other than just type=projects or are there sub parts to each one.. like projects being the type, but a secondary line being type_of_project = media... etc... – Jeff Clayton Aug 30 '14 at 06:11
  • actually, am trying to read some values using a textarea with respect to the $_GET value. is, if $_GET='publication' insert the textarea content to db with type='publication'. If the $_GET['project'] then type attribute (of db) has a value of 'project'. I have about 12 values for the type attribute, and now am using an else_if ladder. – Nisfan Aug 30 '14 at 06:17
  • okay - first, I would use a switch statement instead of an else_if ladder in general... if the value of type is set at all in advance and not intuitively based on content. It tends to result in cleaner easier to follow code. – Jeff Clayton Aug 30 '14 at 06:21

6 Answers6

2

Ok so I guess I figured out what you want from your comments. Lets see.

$types = array('projects', 'research', 'publication'); // add as many as you want
$valid = array_intersect_key($_GET, array_flip($types));

if (count($valid) > 1)
    die "More than one category is set, this is invalid.";

if (!$valid)
    die "No category was set, you must choose one.";

foreach ($valid as $type => $value) // just to split this one element array key/value into distinct variables
{
    $value = mysql_real_escape_string($value); // assuming you are using mysql_*
    $sql = "SELECT * FROM table WHERE $type = '$value'";
}

...
Havenard
  • 27,022
  • 5
  • 36
  • 62
0
$projects = $_GET["projects"];

Or just use directly from $_GET, this is an associative array with all the values.

foreach ($_GET as $key => $value){
    if(!empty($value)){
        $type = $key; //if you expect a single item
        $type[] = $key; //if you expect multiple items
    }
}
TeeDeJee
  • 3,756
  • 1
  • 17
  • 24
  • You mean, I can use $type=$_GET; – Nisfan Aug 30 '14 at 05:49
  • If you use $type = $_GET; then you have an array of all your values. But it's just like $_GET. So $type['projects'] equals $_GET['projects'] – TeeDeJee Aug 30 '14 at 05:55
  • I need these get values {projects, publications, ...etc} to $type. So I can use the $type in query. Otherwise I need a elseif ladder. :( – Nisfan Aug 30 '14 at 05:57
  • Added a foreach loop to get all the not empty variables in $type. It could be that you may need to use '' != $value in the if statement – TeeDeJee Aug 30 '14 at 06:14
0

Yes, you can assign these in a variable -

if(isset($_GET["projects"]))
{
    $value = $_GET['projects'];
}
else if(isset($_GET["research"]))
{
    $value = $_GET['research'];
}
else if(isset($_GET["publication"]))
{
    $value = $_GET['publication'];
}
echo $value;
Rakesh kumar
  • 135
  • 6
0

I'm guessing you're expecting a single value in $_GET, like ?projects=foo or ?research=bar. In that case:

$type  = key($_GET);
$value = $_GET[$type];

echo "$type = $value";
deceze
  • 510,633
  • 85
  • 743
  • 889
0

if the intent is to check if values on a form have been answered/filled out you could use

if(isset($_GET['project'] || ... || isset($_GET['publication'])
{
 // Insert Code Here
}
else
{
 // Insert Code Here
}

The above code is assuming the fields are not text fields or textareas if those are the types of inputs then instead of

if(isset($_GET['project']))

use

if($_GET['project'] != "")
0

I didn't completely understood what you are saying but looks like that you are trying to execute something when all $_GET is true. If so then use the code below

if(isset($_GET["projects"]) && isset($_GET["research"]) && isset($_GET["publication"]) )
{ .....    }

Hope this helps you

Utkarsh Dixit
  • 4,267
  • 3
  • 15
  • 38