Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I using jquery to add / remove inputs

i use append to add multiple Tr for Date / revenue

also i use another append to add multiple td for revenue in same Tr of date

i Add multiple date inputs and into this table i add multiple revenue inputs

I have to use name="date[]" to can use for loop and insert each one in mysql table

but in sametime there is multiple name="revenue[]"

Here is example

    <form method="post" action ="">
    <table>
<tr>
<td>Date : <input type="text" name="date[]" value="25/07/2013"></td>

<td>Revenue : <input type="text" name="revenue[]" value="1"><br>
    Revenue : <input type="text" name="revenue[]" value="2" ><br>
</td>
</tr>

<tr>
    <td>Date : <input type="text" name="date[]" value="26/07/2013"> </td>

    <td>
    Revenue : <input type="text" name="revenue[]" value="12"><br>
    Revenue : <input type="text" name="revenue[]" value="13"><br>
    Revenue : <input type="text" name="revenue[]" value="14"><br>
    </td>
 </tr>   

    <tr>
    <td>Date : <input type="text" name="date[]" value="27/07/2013"></td>
    <td>
    Revenue : <input type="text" name="revenue[]" value="30"><br>
  </td>
 </tr>  
 </table>
    <br><br><input name="submit" value="submit" type="submit">

    </form>

PHP code

<?php
if(isset($_POST['submit'])){
$date = $_POST['date'];
echo "Results<br>";
for($i = 0; $i<count($date); $i++){
echo "Date : $date[$i] <br>";
print_r($_POST['revenue']);
echo "<br><br>";
}
}
?>

Results is :

Date : 25/07/2013
Array ( [0] => 1 [1] => 2 [2] => 12 [3] => 13 [4] => 14 [5] => 30 )

Date : 26/07/2013
Array ( [0] => 1 [1] => 2 [2] => 12 [3] => 13 [4] => 14 [5] => 30 )

Date : 27/07/2013
Array ( [0] => 1 [1] => 2 [2] => 12 [3] => 13 [4] => 14 [5] => 30 ) 

I wanted results to be like that

Date : 25/07/2013
Array ( [0] => 1 [1] => 2)

Date : 26/07/2013
Array ( [0] => 12 [1] => 13 [1] => 14)

Date : 27/07/2013
Array ( [0] => 30 ) 

i want to insert each date into a table with the multiple revenue in same row

Like In Row

Date : 25/07/2013     Revenue : 1-2
Date : 26/07/2013     Revenue : 12-13,14
Date : 27/07/2013     Revenue : 30

Its very important for me, Thank You Very Much

share|improve this question
Your loops are wrong, but the main problem is, that there is no way to tell which "revenues" belong to what "dates". – MightyPork Aug 7 at 22:52
so is there any way – Metay Jack Aug 7 at 23:00

1 Answer

up vote 2 down vote accepted

When you're creating your date and revenue inputs, name them with the array notation, but include indexes:

<tr>
    <td>Date : <input type="text" name="date[0]" value="25/07/2013"></td>
    <td>
        Revenue : <input type="text" name="revenue[0][]" value="1"><br>
        Revenue : <input type="text" name="revenue[0][]" value="2" ><br>
    </td>
</tr>

<tr>
    <td>Date : <input type="text" name="date[1]" value="26/07/2013"> </td>

    <td>
        Revenue : <input type="text" name="revenue[1][]" value="12"><br>
        Revenue : <input type="text" name="revenue[1][]" value="13"><br>
        Revenue : <input type="text" name="revenue[1][]" value="14"><br>
    </td>
</tr>   

You should then be able to read them from $_POST as $_POST['date'][0] and $_POST['revenue'][0][0], `$_POST['revenue'][0][1], etc.

share|improve this answer
but as i said i using jquery to add / remove multiple inputs for date / revenue , so i think if i used your way, when i coding php , it will cause a problem if $_POST missed – Metay Jack Aug 7 at 23:12
No - you can create these elements in jQuery, with these names. I've just posted the HTML here to show how it works. – Mike W Aug 7 at 23:13
this is the way to do it, i do it all the time. although i do name="update[0][date]", name="update[0][revenue][0]", name="update[0][revenue][1]", etc – nathan hayfield Aug 7 at 23:14
Yes Mike W, i understood you, but look, i will do the following with jquery i will use append : rename revenue input to : revenue[1][] in first Tr of date, then i added with jquery second TR of date and renamed : revenue input to revenue[2][] , what about if i deleted second TR of data which has revenue[2][] , and i will use for loop on date array, so how i can check the revenues of this date ? – Metay Jack Aug 7 at 23:23
Your PHP script can iterate over the dates in one foreach loop, and iterate over the reveues in a second, inner, foreach loop. Exactly how you do that depends on your application. It's really a different question. Have a go at it, and post a new question if you get stuck. – Mike W Aug 7 at 23:28
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.