I am posting from one page to another a list of products
at present I passing through the name,Price,Unit,and Quantity. I want to capture the list in an array.
I want the array to store the products together in the array. I have only a basic understanding of arrays as I have never really used them much before.
I want the be able to store them in an array called $fishlist.
I want to be able to access $fishlist[0] and be able to access the name,price,unit and quantity ordered.
currently I have this when I use the print_r command. how to i loop though and add the 4 parts of the individual fish into a group in the array and how then would i be able to access them later?
Array
(
[FishName_9] => Blue Prawn
[FishPrice_9] => 40.00
[FishUnit_9] => KG
[FishID_9] =>
[FishName_1] => Hoki
[FishPrice_1] => 12.00
[FishUnit_1] => KG
[FishID_1] =>
[FishName_2] => Sol
[FishPrice_2] => 15.00
[FishUnit_2] => KG
[FishID_2] =>
[blacboardorder] => Submit Order
[ck_csv] => Array
(
[serverName] => localhost
[username] => ********
[password] => ********
)
[PHPSESSID] => 1dde1bb83c8926818edb22fba3acff37
)
How to capture Post Variables?
Moderator: General Moderators
Re: How to capture Post Variables?
It would appear as though you have multiple forms on one page to come up with FishID_1, FishID_2, etc? Out of curiosity, what does your form look like (please don't post 100 lines of code, just a general representation of your form).Mariusx wrote:I
I want the be able to store them in an array called $fishlist.
I want to be able to access $fishlist[0] and be able to access the name,price,unit and quantity ordered.
currently I have this when I use the print_r command. how to i loop though and add the 4 parts of the individual fish into a group in the array and how then would i be able to access them later?
Now, if I get your meaning, the array index $fishlist[0] would contain the elements:values in FishID_1, FishName_1, etc and $fishlist[1] contains FishID2, etc, correct? If all the elements are consistent, you might do something like this:
(untested)
Code: Select all
// creates an empty array, $fishlist
$fishlist= array();
// create a counter to iterate through
// define NUMBER_OF_FISH_ELEMENTS in your script!
for ( $i=0; $i<NUMBER_OF_FISH_ELEMENTS; $i++) {
// loops through your $_POST array
foreach ( $_POST AS $key => $value ) {
// a relatively simply regular expression
// matches on FishID1, FishName6, etc
$pregex= "/^Fish(ID|Name|Price|Unit){1}\_($i){1}$/";
// run the regular expression against the index
if ( preg_match($pregex, $key) ) {
// if it matches, assign the next $fishlist element to the array
// $fishlist[0] should become FishID_1 => x, FishName_1 => Hoki
$fishlist[$i] = $key => $value;
}
}
}Can you specify what you mean by 'access them later'? Later as in a week from now or just on another script within the same session? If you want to store them for future reference, you want to look at an RDBMS like MySQL, Postgresql, SQLite, etc or perhaps store them in a flatfile database. If all you want to do is access them within the same session, assign them to the $_SESSION superglobal and use sessions in your scripts (which it would appear as though you do, with the PHPSESSID value there).
Take a look at the PHP Manual sections on arrays, array functions, and session handling functions.