How to save table values into array?
-
I have table in wordpress admin panel: <tr> <td><input style="width:80%" type="text" name="ism" value="" /></td> <td><input style="width:80%" type="text" name="par" value="" /></td> <td><input style="width:80%" type="text" name="mar" value="" /></td> <td><input style="width:80%" type="text" name="akc" value="-" /></td> <td><a style="cursor:pointer;" onclick="nqsaddtr(this)">Add</a></td> </tr> It looks like: Table can have unlimited number of rows. I want to save all values in array and save results in database. I have a code (it's not working): $per = array(); foreach($_POST['ism'] as $prod) { $ism = $_POST['ism'][$i]; $par = $_POST['par'][$i]; $mar = $_POST['mar'][$i]; $akc = $_POST['akc'][$i]; $per = array('post_id'=>$post_id,'ismatavimai'=>$ism,'parametras'=>$par,'marke'=>$mar,'akcijos'=>$akc); } /* update */ if (!empty($prev)) $wpdb->update($wpdb->produktas,$upd,array('post_id'=>$post_id)); else { $upd['post_id'] = $post_id; $wpdb->insert($wpdb->produktas,$per); } It saves empty first row (without any values, just post id). $per = array(); $ism = $_POST['ism']; $par = $_POST['par']; $mar = $_POST['mar']; $akc = $_POST['akc']; $per = array('post_id'=>$post_id,'ismatavimai'=>$ism,'parametras'=>$par,'marke'=>$mar,'akcijos'=>$akc); This one is working, but of course it saves results just of first row. How I can make everything work?
-
Answer:
Do: $yourArr = array(); $i = 0; foreach($_POST['ism'] as $prod) { $ism = $_POST['ism'][$i]; $par = $_POST['par'][$i]; $mar = $_POST['mar'][$i]; $akc = $_POST['akc'][$i]; $per[$i]["post_id"] = $post_id; $per[$i]['ismatavimai'] =$ism; $per[$i]['parametras']=$par; $per[$i]['marke']=$mar; $per[$i]['akcijos']=$akc; $i++; }
Lina at Stack Overflow Visit the source
Other answers
you need to generate dynamic names for your input box, like <tr> <td><input style="width:80%" type="text" name="ism1" value="" /></td> </tr> <tr> <td><input style="width:80%" type="text" name="ism2" value="" /></td> </tr> <tr> <td><input style="width:80%" type="text" name="ism3" value="" /></td> </tr> then you can extract it like $ism1 = $_POST['ism1']; $ism2 = $_POST['ism2']; basically you need to add your code in loop
Manoj Nakum
Your form elements are not generating an array for the post variable. Try: <td><input style="width:80%" type="text" name="ism[]" value="" /></td> <td><input style="width:80%" type="text" name="par[]" value="" /></td> <td><input style="width:80%" type="text" name="mar[]" value="" /></td> <td><input style="width:80%" type="text" name="akc[]" value="-" /></td> <td><a style="cursor:pointer;" onclick="nqsaddtr(this)">Add</a></td>
Steve Buzonas
Related Q & A:
- How to change javascript values to php?Best solution by stackoverflow.com
- How to store different values with the same key in memcache?Best solution by stackoverflow.com
- How to copy only values in Excel?Best solution by Stack Overflow
- How do I add to an array from a static method?Best solution by stackoverflow.com
- How can display multiple values to single column?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.