How to save values to an array?

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

Was this solution helpful to you?

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:

Just Added Q & A:

Find solution

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.