SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Dec 2005
    Location
    Ottawa
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Trying to escape variables in an array

    I have an array with a number of elements that I want to process.

    // create array of columns to use
    $columns = array("Server","LogFile","EventID","EventSource","User");


    In one case, I want to go through it and see if a parameter has been passed through the query string. If so, I create a variable of that name. Manually it works like this:

    //capture parameters from URL if present
    if($_REQUEST['Server']) { $Server = $_REQUEST['Server'];}
    if($_REQUEST['logfile']) { $logfile = $_REQUEST['logfile'];}
    if($_REQUEST['eventid']) { $eventid = $_REQUEST['eventid'];}
    if($_REQUEST['eventsource']) { $eventsource = $_REQUEST['eventsource'];}
    if($_REQUEST['user']) { $user = $_REQUEST['user'];}


    This is inefficient, particularly if the elements in the array are changed, and I'd like to be able to do this through a loop.

    The idea would be to loop through the $columns array and create the variables a la;

    foreach( $columns as $temp) {
    if($_REQUEST['$temp']) {
    $temp = $_REQUEST['$temp'];}
    }


    This doesn't work because the $temp in bright red above needs to be a variable itself.

    Any ideas how I can make this happen?

    Thanks,

    Rob
    Last edited by scaiferw; Dec 22, 2005 at 12:22.

  2. #2
    SitePoint Wizard Dean C's Avatar
    Join Date
    Mar 2003
    Location
    England, UK
    Posts
    2,906
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    extract($_REQUEST); would do the job here. But it has it's security risks, be sure to read the manual (I'd loop through the request array first unsetting any elements that aren't in your initial array of columns to check).

  3. #3
    SitePoint Member
    Join Date
    Dec 2005
    Location
    Ottawa
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dean C
    extract($_REQUEST); would do the job here. But it has it's security risks, be sure to read the manual (I'd loop through the request array first unsetting any elements that aren't in your initial array of columns to check).
    Thanks, I'm a little too new to PHP to be able to decipher the manuale on that one.

    I did see how it would tell me how many items have been passed through the query string, which is one to remember.

    Can you suggest how I might build the foreach loop to create that variables?

    (BTW, do you by chance have a relative known to friends as 'Bunty'? - My wife's family knows such a person on that side of the pond.)

    Many thanks,

    Rob

  4. #4
    SitePoint Wizard Dean C's Avatar
    Join Date
    Mar 2003
    Location
    England, UK
    Posts
    2,906
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I know of someone that one of the family knows who is known as "Bunty". What's their full name ?

  5. #5
    SitePoint Member
    Join Date
    Dec 2005
    Location
    Ottawa
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dean C
    I know of someone that one of the family knows who is known as "Bunty". What's their full name ?
    See PM.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •