[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 571
  • Last Modified:

PHP $_REQUEST using an Array?

Hi there,

I have an array of variables 100,101,102 - which are ID numbers.  I want to be able to call $_REQUEST["102"] - but using $_REQUEST["$_SESSION['ID'][2]"];  This bombs out, so I need to know the correct formatting for doing something like this.

Thanks!
0
maeve100
Asked:
maeve100
1 Solution
 
roynaufalCommented:
you could try something like this instead:

$session_id = $_SESSION['ID'][2];
$_REQUEST[$session_id];


I also think this might be your problem:
$_REQUEST["$_SESSION['ID'][2]"];
should be:
$_REQUEST[$_SESSION['ID'][2]];

I personally use the 1st method though...I don't like too many brackets within each other, so it's nicer and cleaner
0
 
Ali KayahanFull Stack DeveloperCommented:
Also

extract($_SESSION);

$_REQUEST[$id['2']];
0
 
Dave BaldwinFixer of ProblemsCommented:
$_REQUEST is an associative array that by default contains the contents of $_GET, $_POST and $_COOKIE.  All of these are set by a page request from a form in a browser.  If the 'name' key you're asking for wasn't set by the page request, then it will cause an error.  A similar situation exists with $_SESSION variables.  If they are not already set, you will get an error trying to use them.

This $_REQUEST["$_SESSION['ID'][2]"]; tries to use the value of the $_SESSION variable as the name of the $_REQUEST variable.  Are you 'POST'ing the 'id's from a page?  Are the $_SESSION variables already set on a previous page?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
dsmileCommented:
If you want to get value from an array, you need to specify two things:
1. the array --> I can see it's $_REQUEST
2. the key (or what might create the key) --> don't know yet. Can you be more specific?
0
 
maeve100Author Commented:
roynaufal: $_REQUEST[$_SESSION['ID'][2]]; Does not work, the value of the request won't print - even though $_session['id'][2] prints the right value by itself.

dsmile: The $_REQUEST is a simple posted value of a single number.  The key/name of the hidden input is an ID number that I have stored in $_session['id'][1], $_session['id'][2], etc.

DaveBaldwin: You are correct that I am trying to use the session variable as the name of the request variable.  The REQUEST is a posted value of quantity, associated with the ID stored in the session variable.  The Session variables are set up previously in another page.

Thank you!
0
 
Dave BaldwinFixer of ProblemsCommented:
I would make it a two-step process (in part to make it easy for me to keep track).  I also just read on http://us2.php.net/manual/en/function.array.php that you are supposed use curly braces inside double quotes to access array variables.
// two-step
$idn2 = $_SESSION['ID'][2];
$item = $_REQUEST["$idn2"];

// original with curly braces
$_REQUEST["{$_SESSION['ID'][2]}"];

Open in new window

0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now