Solved

$_POST to array URGENT 500 POINTS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Posted on 2006-06-08
11
220 Views
Last Modified: 2008-02-20
I have a form that php makes and it displays a certain # of drop downs depending on a varaible. Now on the page that recieves then info how can I use some kind of loop to retrieve the info until there isn't any left. So it will check to see if $_POST['tag1'] is set and if it is get some data from mysql then echo then go back to the beginning of the loop and check for $_POST['tag2'] until if gets to one that isn't set then ends the loop.

Max
0
Comment
Question by:TheMaximumWeasel
  • 7
  • 4
11 Comments
 
LVL 16

Expert Comment

by:dr_dedo
ID: 16865902
foreach ($_POST as $k=>$value){

// put whatever you want here


}
!
0
 
LVL 4

Author Comment

by:TheMaximumWeasel
ID: 16865919
explain that for me????

Max
0
 
LVL 4

Author Comment

by:TheMaximumWeasel
ID: 16865945
how would I use that on with what I explained above.

Max
0
 
LVL 16

Accepted Solution

by:
dr_dedo earned 500 total points
ID: 16865967
in the form action page (that handles the form )

foreach ($_POST as $k=>$value){ // this is a loop through all variables in the POST array

$sql = "select * from table1 where name=$value"; // every element in the post array can be used as $value variable. Do what you want to it


} // the loop will continue as long as POST array still has results
your form may contain some other stuff that are added to post array and you don't want to include in your loop (e.g. a submit button), you can put these variables in array and ignore them
e.g.

$items_to_ignore =array ('submit','email');
foreach ($_POST as $k=>$value){
if (!in_array($k,$items_to_ignore)){
      $sql = "select * from table1 where name=$value";
      // put rest of code here
   }
}
0
 
LVL 4

Author Comment

by:TheMaximumWeasel
ID: 16865989
ok I understand that now but one what is in the value $k

Max
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 4

Author Comment

by:TheMaximumWeasel
ID: 16866013
Also is that act as a loop so if I put $i++ at the end of it, the value will go up each time.

Max
0
 
LVL 4

Author Comment

by:TheMaximumWeasel
ID: 16866017
Also the value will not go upon $i when the part of the array is submit or email.

Max
0
 
LVL 16

Expert Comment

by:dr_dedo
ID: 16866087
foreach ($_POST as $k=>$value){

// $k is the key of the array (i.e. form element name say email, fname, password
// $value is the value of this form element say fnam= xxxx, password=xxxx, email = xxx@xxx.xxx

}
you don't increment anything here, the loop will do it all
0
 
LVL 4

Author Comment

by:TheMaximumWeasel
ID: 16866108
well I need to increment it cause of how I am using it. Each $_POST variable contains a number that is the ID of the tag. So then everytime it loops it appends a string and ads the varialbe heres what I am talking about.

everytime it repeats it ads  "&tag" . $i . "=$value"; to the string and $i is teh variable that increments cause the form is tags 1 through a certain #.

Max
0
 
LVL 4

Author Comment

by:TheMaximumWeasel
ID: 16866464
nevermind I got it to increment correctly by putting it inside the if so it wouldn't increase on something besides the data I want.

Max
0
 
LVL 16

Expert Comment

by:dr_dedo
ID: 16866466
mmm
you can use this
$items_to_ignore =array ('submit','email');
foreach ($_POST as $k=>$value){
if (!in_array($k,$items_to_ignore)){
      echo $value."&tag" . $i . "=$value";
      // put rest of code here
   }
}

or if your form elements are numbered, e.g. arg1, arg2
$c = count($_POST) + 1;
for ($i=1; $i<$c;$i++) {
$key = 'arg'.$i;
if (isset ($_POST[$key])) {

      now you have POST['arg1'], then arg2, etc....

}


}
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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 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…

708 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

19 Experts available now in Live!

Get 1:1 Help Now