Solved

PHP foreach loop on $_Request

Posted on 2013-05-22
4
832 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
Comment Utility
How many $vx s do you have? Is this number fixed?
0
 
LVL 1

Author Comment

by:AUCKLANDIT
Comment Utility
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
Comment Utility
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
Comment Utility
thank you
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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 …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now