Solved

PHP foreach loop on $_Request

Posted on 2013-05-22
4
865 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

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

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

839 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