Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

PHP $_REQUEST using an Array?

Posted on 2010-11-18
6
490 Views
Last Modified: 2012-05-10
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
Comment
Question by:maeve100
6 Comments
 
LVL 3

Expert Comment

by:roynaufal
ID: 34169385
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
 
LVL 14

Expert Comment

by:ali_kayahan
ID: 34169421
Also

extract($_SESSION);

$_REQUEST[$id['2']];
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 34169953
$_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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 13

Expert Comment

by:dsmile
ID: 34170479
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
 

Author Comment

by:maeve100
ID: 34175750
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
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 34175819
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
This article discusses how to create an extensible mechanism for linked drop downs.
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.

829 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