Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PHP foreach loop on $_Request

Posted on 2013-05-22
4
Medium Priority
?
934 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
[X]
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
  • 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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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…
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…
Suggested Courses

609 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