Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-25-2012, 07:13 PM   PM User | #1
nick2price
Regular Coder

 
Join Date: May 2009
Posts: 104
Thanks: 47
Thanked 1 Time in 1 Post
nick2price is an unknown quantity at this point
Retrieiving form data from array

Hi guys. I have a form where I have dynamic fields, so I dont know how many fields there will be. To handle this, each tr has td's like so
Code:
<td><input type="text" placeholder="Giving" class="selector" name="giving2[]" autocomplete="off"/></td>
<td><input type="text" placeholder="Getting" class="selector" name="getting2[]" autocomplete="off"/></td>
So, the name is an array, and it is the same in all rows. At the moment, to retrieve the data, I am trying
Code:
foreach ($_POST['giving2'] as $item) {
	$giving += $item;
}
foreach ($_POST['getting2'] as $item) {
    $getting += $item;
}

$body = "
===== Enquiry: $reference =====\n
Enqury made: ".date("d/m/y H:i:s",time())."\n\n

Giving: $giving\n\n
Getting: $getting\n\n"
;
Then I send it via email. The output I am getting for these fields is 0 though, so its not giving me the correct values. Am I missing something here or doing something wrong?


Cheers
nick2price is offline   Reply With Quote
Old 09-25-2012, 08:26 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,641
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
What is the datatype of the values put into those fields?
If they are numeric, you can run a simple array_sum() on it. If they are not numeric, you intend to concatenate them using the . operator instead of the +, but then you should look at using implode() instead of iterating with foreach.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
nick2price (09-25-2012)
Old 09-25-2012, 08:35 PM   PM User | #3
nick2price
Regular Coder

 
Join Date: May 2009
Posts: 104
Thanks: 47
Thanked 1 Time in 1 Post
nick2price is an unknown quantity at this point
Thanks for the reply, sorry its gone a bit over the top of me. What I showed is one row, its really more like
Code:
<tr>
   <td><input type="text" placeholder="Giving" class="selector" name="giving2[]" autocomplete="off"/></td>
   <td><input type="text" placeholder="Getting" class="selector" name="getting2[]" autocomplete="off"/></td>
</tr>

<tr>
   <td><input type="text" placeholder="Giving" class="selector" name="giving2[]" autocomplete="off"/></td>
   <td><input type="text" placeholder="Getting" class="selector" name="getting2[]" autocomplete="off"/></td>
</tr>

...
And I also have a javascript method where more rows with the same fields can be added, so I will never be sure as to the total number of rows. So what I have done is made it so all names are an array. The field inputed into these fields is text. The reason I used a foreach loop is because wouldnt I need to loop the array which holds my values, especially if I dont know how many there will be?

My overall aim is to take all the inputs from all the fields, and send them in an email. So, the values from row 1 will display first Giving[0] Getting[0], then row 2 etc. As I say though, I cannot do it like this because I dont know how many there will be.

Nick
nick2price is offline   Reply With Quote
Old 09-25-2012, 10:05 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,641
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
You mean you want to match up the two arrays so data from the first one matches with the data in the second?
There are a number of ways to do this. I'll iterate only.
PHP Code:
$sResult "";
if (
count($_POST['giving2']) == count($_POST['getting2']))
{
    while ((
$curGiv current($_POST['giving2'])) && ($curGet current($_POST['getting2'])))
    {
        
$sResult .= sprintf("Giving: %s\nGetting:%s\n\n"$curGiv$curGet);
        
next($_POST['giving2']);
        
next($_POST['getting2']);
    }

Something like that would work. HTML could be massaged as well to represent a multidimensional array of getting and giving together.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:52 PM.


Advertisement
Log in to turn off these ads.