doumeng3188 2014-07-22 20:01
浏览 28

动态Jquery形式为PHP多维数组

I am trying to create a dynamic form with jquery that creates an array that I can retrieve after submitting the form.

I believe where I am going wrong is the array. Unfortunately this is the first time I have tried multidimensional arrays. And I am trying to select values from different layers.

So the fiddle below is what I have been able to get to so far.

JSFiddle

As you should be able to see my top array is 'events', these are numbered in the fiddle as 1 and 2. Then if 'cycle' is selected, jquery will add a div. Within the div are inputs 'start point X:' and 'Z:', there is also a 'Add point' button that will add a row of form fields each time it is clicked.

For reference I am using codeigniter, so my controller has

    function code(){

    $data['global'] = $this->input->post('global');
    $data['event'] = $this->input->post('event');
    $this->load->view('view', $data);}

So I believe the problem is in my array markup. I can quite happily retrieve the values within the seperate 'global' array in my (codeigniter) view with $global['home_x'] or $global['home_z'].

I can also retrieve $event[$j]['x_start'] and $event[$j]['z_start'] by looping with:

$event_count = count($event);
for ($j = 1; $j <= $event_count; $j++){

if($event[$j]['type'] == "cycle"){
echo number_format($event[$j]['x_start'], 3);
echo number_format($event[$j]['z_start'], 3);
}

However when I come to loop through the 'point' layer of the array it starts to go wrong. So continuing from above I am doing:

        if(isset($event[$j]['point'])){

        $point_count = count($event[$j]['point']);

for ($i = 1; $i <= $point_count; $i++ ) {

            if($event[$j]['point'][$i]['type'] == "move"){
                echo "Move" . number_format($event[$j]['point'][$i]['x_end'], 3) . number_format($event[$j]['point'][$i]['z_end'], 3);
            }

            else if($event[$j]['point'][$i]['type'] == "arc"){
                echo "Arc" . number_format($event[$j]['point'][$i]['x_end'], 3) . number_format($event[$j]['point'][$i]['z_end'], 3) . number_format($event[$j]['point'][$i]['radius'], 3);
            }

            else if($event[$j]['point'][$i]['type'] == "position"){
                echo "Position" . number_format($event[$j]['point'][$i]['x_end'], 3) . number_format($event[$j]['point'][$i]['z_end'], 3);
            }

        }

    }

}

I want each event to have an independent point key. As at the moment with my var point = 1 and point++ I am just adding 1 onto the key each time the add point button is pressed, whether it is event 1 or 2.

So I need something like this:

event - 1 - x_start
                 z_start
                 point - 1 - type
                                 x_end
                                 z_end
                                 radius
                 point - 2 - type
                                 x_end
                                 z_end
                                 radius
            2 - x_start
                 z_start
                 point - 1 - type
                                 x_end
                                 z_end
                                 radius
                 point - 2 - type
                                 x_end
                                 z_end
                                 radius

Instead of:

event - 1 - x_start
                 z_start
                 point - 1 - type
                                 x_end
                                 z_end
                                 radius
                 point - 2 - type
                                 x_end
                                 z_end
                                 radius
            2 - x_start
                 z_start
                 point - 3 - type
                                 x_end
                                 z_end
                                 radius
                 point - 4 - type
                                 x_end
                                 z_end
                                 radius

If there is the possibility that the 'point' key number can reorder itself as well when the 'Delete point' button is pressed that would be perfect. So, for example, when point 1 is deleted point 2 becomes point 1.

Lastly I need the events to be dynamic as well, so when a hypothetical 'add event' button is pressed it adds a new event. And vice versa with a delete button. But I have only looked at the for loop in the view page at the moment.

I hope that I explained this well enough!!!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度