Solved

PHP foreach loop on $_Request

Posted on 2013-05-22
4
873 Views
Last Modified: 2013-05-23
Hello,

Ok, say you send this to a php page test.php

test.php?p=abc&w=erw&v1=123,122,342$v2=234,3123,3123&v3=34,3123,123

They get cleaned:
foreach (array_keys($_REQUEST) as $key) {
 $$key = $mysqli->real_escape_string(($_REQUEST[$key]));
 }

Now I have these:

$p,$w,$v1,$v2,$v3,$v4

Forget about the $p & the $w.

I want to now loop through only the $vx (where x is the number)

foreach(of-the-ones-the-start-with-a-v) {

$v=$the-one-used-in-this-loop;

      $arr=explode(',', $v);
      $first=$arr[0];
      $second=$arr[1];
      $third=$arr[2];

And now I do something with the $first,$second,$third

}


What needs to go here:
of-the-ones-the-start-with-a-v
And here:
$v=$the-one-used-in-this-loop;

Thank you for viewing my question, any help appreciated.
0
Comment
Question by:AUCKLANDIT
  • 2
  • 2
4 Comments
 
LVL 6

Expert Comment

by:mickey159
ID: 39190082
How many $vx s do you have? Is this number fixed?
0
 
LVL 1

Author Comment

by:AUCKLANDIT
ID: 39190121
min of 1 max of 60 - will be different each time as a user form where they add rows if needed, up to a max of 60.
0
 
LVL 6

Accepted Solution

by:
mickey159 earned 500 total points
ID: 39190252
Try to use json to send the data. It should look like this:

{"v1":"123,122,342","v2":"234,3123,3123","v3":"34,3123,123"}

Open in new window


Then use the following code:

//$v is the set of $v1,$v2,etc.
$v=json_decode($v);
foreach($v as $val){
//do your work here
}

Open in new window

0
 
LVL 1

Author Comment

by:AUCKLANDIT
ID: 39190905
thank you
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

733 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