Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Form Posting while Looping through arrays

Posted on 2006-04-12
4
Medium Priority
?
158 Views
Last Modified: 2008-02-26
Hello

I am trying to make a modification to my X-Cart shopping cart.  I am trying to setup a page that displays all of my products and a shopper can say that they want 2 of this product, 3 of these, 5 of these... When they are all done, they click add to cart and it adds all of their selections.  Now I have the page setup, but I need to get each element from these arrays to post seperately.  For each item purchased, their are 4 input's that go along with it.  SO, I need it to post those four items then go to the next set and post those four...and so on and so forth. Here is the code for my input types:
<INPUT type="hidden" name="productid[]" value="<?php echo $x_productid;?>">
<INPUT type="hidden" name="mode[]" value="add">
<input type="hidden" name="product_options[]" value="<?php echo $x_optionid ;?>">
<input type="text" name="amount[]" size="5" maxlength="3" value="">
To sum it up...I need productid[0],mode[0],prouduct_options[0], amount[0] to all post to a cart.php, then loop through with arrays[1]..

Your help is much appreciated.
0
Comment
Question by:koolie
  • 2
4 Comments
 
LVL 15

Expert Comment

by:babuno5
ID: 16438766
well i m not clear with what you want
but here is a suggestion
For each product use a check box and then only those checked boxes will be passed to cart.php
for those checked boxes values get the corresponding values from the productid[checkboxvalue],mode[checkboxvalue],prouduct_options[checkboxvalue], amount[checkboxvalue]
0
 
LVL 9

Expert Comment

by:blue_hunter
ID: 16438842
are you mean somethings like
$_POST["productid"][0]
$_POST["productid"][1]

$_POST["mode"][0]
$_POST["mode"][1]
and etc, in your php scripts?

0
 
LVL 5

Author Comment

by:koolie
ID: 16438882
to blue_hunter:
something like that,but x-cart requires me to send it the value $mode so any array numbering and their script wont recognize it.  so i need to loop through the array and then reassign its name. I could send cart.php the array and then process the loop, but sending $_POST["mode][any number] wont work.
0
 
LVL 9

Accepted Solution

by:
blue_hunter earned 750 total points
ID: 16442132
as we mention before, you can get the value with
$_POST["productid"][0]
$_POST["productid"][1]

now, to loop through the variable and reassign the values in the array to another array
example code:

if(!empty($_POST["productid"])){
$productid = array();
foreach($_POST["productid"] as $k => $d){
$productid[] = $d;
}
}

same goes to others.

hope this clearly answer your question
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
This article discusses how to implement server side field validation and display customized error messages to the client.
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month21 days, 5 hours left to enroll

810 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