数组未按顺序设置数据

时间:2017-08-24 21:51:57

标签: php codeigniter

我想将所有复选框值存储到具有相关menuid的数据库,但我的数组不存储根据menuid的数据。我附上了我正在使用的所有代码,如果有任何机构可以帮助我,我会提前感谢。

  

查看档案

<div class="col-md-8">
    <div class="panel panel-default">
        <div class="panel-heading bg-white">
            <strong>Users Groups</strong><br>
            <small class="text-muted">Create User Groups</small>
        </div>
        <div class="panel-body">
            <div class="col-md-12">
                <div class="form-group">
                    <label for="exampleInputEmail1">User Group Title*</label>
                    <input value="" type="text" name="title" class="form-control" id="shop_name" placeholder="Ex: xyz shop">
                </div>
            </div>
            <div class="col-md-12">
                <div class="table-responsive">
                    <table id="example" class="table table-hover b-t b-b">
                        <thead>
                            <tr>
                                <th>Item</th>
                                <th>Add</th>
                                <th>Edit</th>
                                <th>Delete</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $n = 1;
                            foreach ( $list_users as $row) { ?>
                                <tr>

                                    <?php if ($row['IsChild'] != null and  $row['IsChild'] != 1) { ?>
                                        <td><?php echo $row['alias']; ?></td>

                                        <td>
                                            <input  value="<?php echo $row['id']; ?>" type="text" name="menu_id[]">
                                            <input checked style="opacity:0;" value="<?php echo $row['id']; ?>--0--a" id="add1<?php echo $n;?>" type="checkbox" name="add[]">
                                            <input value="<?php echo $row['id']; ?>--1--a" id="add<?php echo $n;?>" type="checkbox" name="add[]">
                                        </td>
                                        <td>
                                            <input checked style="opacity:0;" value="<?php echo $row['id']; ?>--0--e" id="edit1<?php echo $n;?>" type="checkbox" name="edit[]">
                                            <input value="<?php echo $row['id']; ?>--1--e" id="edit<?php echo $n;?>" type="checkbox" name="edit[]">
                                        </td>

                                        <td>
                                            <input checked style="opacity:0;" value="<?php echo $row['id']; ?>--0--d" id="delete1<?php echo $n;?>" type="checkbox" name="delete[]">
                                            <input value="<?php echo $row['id']; ?>--1--d" id="delete<?php echo $n;?>" type="checkbox" name="delete[]">
                                        </td>
                                    <?php } ?>
                                </tr>
                            <?php $n++; }   ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="col-md-4">
     <div class="panel panel-default">
        <div class="panel-heading bg-dark">
           <strong>Publish</strong><br>
            <small class="text-muted">Click to publish</small>
        </div>
        <div class="panel-body">
            <p><strong>Status: </strong> </p>
            <p><strong>Date Created: </strong></p>
            <p><strong>Date Updated: </strong></p>
        </div>
        <div class="panel bg-light" style="margin-bottom: 0px; padding: 10px; text-align: right;" >
            <input value="Update Now" style="padding: 7px 20px; border: 0px; border-radius: 10px;" type="submit" class=" btn-success">
        </div>
    </div>
</div>

  

控制器功能

function save_user_groups()
    {

        //For generating user_group_master_id manually
        $this->load->model('settings/model_users');
        $banner_id_main = $this->model_users->get_user_master_id();
        foreach ($banner_id_main as $row)
        {
            $user_group_m_id = $row['user_group_master_id'];
        }

        $title   = $this->input->post('title');
        $menu_id = $this->input->post('menu_id');
        $add     = $this->input->post('add');
        $edit    = $this->input->post('edit');
        $delete  = $this->input->post('delete');

        $count = count($menu_id);

        //array for insert main banner detail in database
        $user_group_master = array(
            'user_group_master_id' => $user_group_m_id,
            'user_group_title' => $title
        );

        $g_rights=array();

        for($i=0; $i < $count; $i++)
        {
            $g_rights[] = array(
                'user_group_master_id' => $user_group_m_id,
                'MenuID' => $menu_id[$i],
                'chkadd'=>$add[$i],
                'chkedit'=>$edit[$i],
                'chkdel' => $delete[$i]
            );
        }



        print_r($user_group_master);
        print_r($g_rights);
        exit();

        $data['rights'] = $g_rights ;

        $this->load->model('settings/model_users');

        if ($create = $this->model_users->save_user_group($user_group_master, $g_rights))
        {
            $data['product_created'] = 'thank you';
            $this->load->view('setting/users' , $data);
        }
    }
  

数组的结果   菜单ID未在chkadd,chkedit,chkdelete中正确填充,请帮助

Array
(
    [user_group_master_id] =&gt; 1
    [user_group_title] =&gt; 
)
Array
(
    [0] =&gt; Array
        (
            [user_group_master_id] =&gt; 1
            [MenuID] =&gt; 51
            [chkadd] =&gt; 51--0--a
            [chkedit] =&gt; 51--0--e
            [chkdel] =&gt; 51--0--d
        )

    [1] =&gt; Array
        (
            [user_group_master_id] =&gt; 1
            [MenuID] =&gt; 10
            [chkadd] =&gt; 51--1--a
            [chkedit] =&gt; 51--1--e
            [chkdel] =&gt; 10--0--d
        )

    [2] =&gt; Array
        (
            [user_group_master_id] =&gt; 1
            [MenuID] =&gt; 11
            [chkadd] =&gt; 10--0--a
            [chkedit] =&gt; 10--0--e
            [chkdel] =&gt; 11--0--d
        )

    [3] =&gt; Array
        (
            [user_group_master_id] =&gt; 1
            [MenuID] =&gt; 12
            [chkadd] =&gt; 11--0--a
            [chkedit] =&gt; 10--1--e
            [chkdel] =&gt; 12--0--d
        )

    [4] =&gt; Array
        (
            [user_group_master_id] =&gt; 1
            [MenuID] =&gt; 14
            [chkadd] =&gt; 12--0--a
            [chkedit] =&gt; 11--0--e
            [chkdel] =&gt; 14--0--d
        )

    [5] =&gt; Array
        (
            [user_group_master_id] =&gt; 1
            [MenuID] =&gt; 15
            [chkadd] =&gt; 14--0--a
            [chkedit] =&gt; 11--1--e
            [chkdel] =&gt; 15--0--d
        )

    [6] =&gt; Array
        (
            [user_group_master_id] =&gt; 1
            [MenuID] =&gt; 16
            [chkadd] =&gt; 15--0--a
            [chkedit] =&gt; 12--0--e
            [chkdel] =&gt; 16--0--d
        )

    [7] =&gt; Array
        (
            [user_group_master_id] =&gt; 1
            [MenuID] =&gt; 17
            [chkadd] =&gt; 16--0--a
            [chkedit] =&gt; 14--0--e
            [chkdel] =&gt; 17--0--d
        )

    [8] =&gt; Array
        (
            [user_group_master_id] =&gt; 1
            [MenuID] =&gt; 18
            [chkadd] =&gt; 17--0--a
            [chkedit] =&gt; 15--0--e
            [chkdel] =&gt; 18--0--d
        )

    [9] =&gt; Array
        (
            [user_group_master_id] =&gt; 1
            [MenuID] =&gt; 19
            [chkadd] =&gt; 18--0--a
            [chkedit] =&gt; 16--0--e
            [chkdel] =&gt; 19--0--d
        )

    [10] =&gt; Array
        (
            [user_group_master_id] =&gt; 1
            [MenuID] =&gt; 20
            [chkadd] =&gt; 19--0--a
            [chkedit] =&gt; 17--0--e
            [chkdel] =&gt; 20--0--d
        )

    [11] =&gt; Array
        (
            [user_group_master_id] =&gt; 1
            [MenuID] =&gt; 21
            [chkadd] =&gt; 20--0--a
            [chkedit] =&gt; 18--0--e
            [chkdel] =&gt; 21--0--d
        )

    [12] =&gt; Array
        (
            [user_group_master_id] =&gt; 1
            [MenuID] =&gt; 22
            [chkadd] =&gt; 21--0--a
            [chkedit] =&gt; 19--0--e
            [chkdel] =&gt; 22--0--d
        )

    [13] =&gt; Array
        (
            [user_group_master_id] =&gt; 1
            [MenuID] =&gt; 29
            [chkadd] =&gt; 22--0--a
            [chkedit] =&gt; 20--0--e
            [chkdel] =&gt; 29--0--d
        )

    [14] =&gt; Array
        (
            [user_group_master_id] =&gt; 1
            [MenuID] =&gt; 30
            [chkadd] =&gt; 29--0--a
            [chkedit] =&gt; 21--0--e
            [chkdel] =&gt; 30--0--d
        )

    [15] =&gt; Array
        (
            [user_group_master_id] =&gt; 1
            [MenuID] =&gt; 31
            [chkadd] =&gt; 30--0--a
            [chkedit] =&gt; 22--0--e
            [chkdel] =&gt; 31--0--d
        )

    [16] =&gt; Array
        (
            [user_group_master_id] =&gt; 1
            [MenuID] =&gt; 32
            [chkadd] =&gt; 31--0--a
            [chkedit] =&gt; 29--0--e
            [chkdel] =&gt; 32--0--d
        )

    [17] =&gt; Array
        (
            [user_group_master_id] =&gt; 1
            [MenuID] =&gt; 33
            [chkadd] =&gt; 32--0--a
            [chkedit] =&gt; 30--0--e
            [chkdel] =&gt; 33--0--d
        )

    [18] =&gt; Array
        (
            [user_group_master_id] =&gt; 1
            [MenuID] =&gt; 35
            [chkadd] =&gt; 33--0--a
            [chkedit] =&gt; 31--0--e
            [chkdel] =&gt; 35--0--d
        )

    [19] =&gt; Array
        (
            [user_group_master_id] =&gt; 1
            [MenuID] =&gt; 36
            [chkadd] =&gt; 35--0--a
            [chkedit] =&gt; 32--0--e
            [chkdel] =&gt; 36--0--d
        )

    [20] =&gt; Array
        (
            [user_group_master_id] =&gt; 1
            [MenuID] =&gt; 37
            [chkadd] =&gt; 36--0--a
            [chkedit] =&gt; 33--0--e
            [chkdel] =&gt; 37--0--d
        )

    [21] =&gt; Array
        (
            [user_group_master_id] =&gt; 1
            [MenuID] =&gt; 38
            [chkadd] =&gt; 37--0--a
            [chkedit] =&gt; 35--0--e
            [chkdel] =&gt; 38--0--d
        )

    [22] =&gt; Array
        (
            [user_group_master_id] =&gt; 1
            [MenuID] =&gt; 39
            [chkadd] =&gt; 38--0--a
            [chkedit] =&gt; 36--0--e
            [chkdel] =&gt; 39--0--d
        )

    [23] =&gt; Array
        (
            [user_group_master_id] =&gt; 1
            [MenuID] =&gt; 40
            [chkadd] =&gt; 39--0--a
            [chkedit] =&gt; 37--0--e
            [chkdel] =&gt; 40--0--d
        )

    [24] =&gt; Array
        (
            [user_group_master_id] =&gt; 1
            [MenuID] =&gt; 41
            [chkadd] =&gt; 40--0--a
            [chkedit] =&gt; 38--0--e
            [chkdel] =&gt; 41--0--d
        )

    [25] =&gt; Array
        (
            [user_group_master_id] =&gt; 1
            [MenuID] =&gt; 43
            [chkadd] =&gt; 41--0--a
            [chkedit] =&gt; 39--0--e
            [chkdel] =&gt; 43--0--d
        )

    [26] =&gt; Array
        (
            [user_group_master_id] =&gt; 1
            [MenuID] =&gt; 44
            [chkadd] =&gt; 43--0--a
            [chkedit] =&gt; 40--0--e
            [chkdel] =&gt; 44--0--d
        )

    [27] =&gt; Array
        (
            [user_group_master_id] =&gt; 1
            [MenuID] =&gt; 45
            [chkadd] =&gt; 44--0--a
            [chkedit] =&gt; 41--0--e
            [chkdel] =&gt; 45--0--d
        )

    [28] =&gt; Array
        (
            [user_group_master_id] =&gt; 1
            [MenuID] =&gt; 46
            [chkadd] =&gt; 45--0--a
            [chkedit] =&gt; 43--0--e
            [chkdel] =&gt; 46--0--d
        )

    [29] =&gt; Array
        (
            [user_group_master_id] =&gt; 1
            [MenuID] =&gt; 47
            [chkadd] =&gt; 46--0--a
            [chkedit] =&gt; 44--0--e
            [chkdel] =&gt; 47--0--d
        )

    [30] =&gt; Array
        (
            [user_group_master_id] =&gt; 1
            [MenuID] =&gt; 48
            [chkadd] =&gt; 47--0--a
            [chkedit] =&gt; 45--0--e
            [chkdel] =&gt; 48--0--d
        )

    [31] =&gt; Array
        (
            [user_group_master_id] =&gt; 1
            [MenuID] =&gt; 49
            [chkadd] =&gt; 48--0--a
            [chkedit] =&gt; 46--0--e
            [chkdel] =&gt; 49--0--d
        )

    [32] =&gt; Array
        (
            [user_group_master_id] =&gt; 1
            [MenuID] =&gt; 50
            [chkadd] =&gt; 49--0--a
            [chkedit] =&gt; 47--0--e
            [chkdel] =&gt; 50--0--d
        )

)

1 个答案:

答案 0 :(得分:1)

我认为您的问题是,在发布表单数据时,未检查的复选框不会显示在post数组中。知道如果没有选中某些复选框,您不能简单地尝试使用添加,编辑和删除数组的数字索引来确定是否已选中/取消选中。考虑以下测试:

<?php

// You have three menu IDs
$menu_id = array(
    '1','2','3'
);

// Only one checkbox was checked for "add"
$add = array(
    '1--0--a'
);

// Only two checkboxes were checked for "edit"
$edit = array(
    '1--0--e',
    '2--0--e'
);

// All three checkboxes were checked for "delete"
$delete = array(
    '1--0--d',
    '2--0--d',
    '3--0--d'
);

// Loop through the menu IDs, because they will always be posted
foreach( $menu_id as $id )
{
    $temp = array('MenuId' => $id);

    // Check if this menu ID has a checkbox checked for add
    foreach( $add as $a )
    {
        $parts = explode( '--', $a );
        if( $parts[0] == $id )
        {
            $temp['chkadd'] = $a;
            break;
        }
    }

    // Check if this menu ID has a checkbox checked for edit
    foreach( $edit as $e )
    {
        $parts = explode( '--', $e );
        if( $parts[0] == $id )
        {
            $temp['chkedit'] = $e;
            break;
        }
    }

    // Check if this menu ID has a checkbox checked for delete
    foreach( $delete as $d )
    {
        $parts = explode( '--', $d );
        if( $parts[0] == $id )
        {
            $temp['chkdel'] = $d;
            break;
        }
    }

    $final[] = $temp;
}

echo '<pre>';
print_r( $final );
echo '</pre>';

此测试应该可以帮助您了解复选框数据如何不再成为问题。现在你可以修复你的代码了。