如何从倍数复选框中检索数组值?

时间:2019-07-13 18:02:33

标签: php mysql codeigniter codeigniter-3

如何从倍数复选框中检索数组值?当我取回值数组并回显值时遇到问题。我收到此严重性:警告 消息:in_array()期望参数2为数组,给定字符串 文件名:page / update.php。
请再帮我一次。谢谢。 您会找到三个文件:View,Model和Controller。请检查我在哪里错了...

UPDATE.PHP:

<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
<div class="row">
<div class="col-sm-12">
<?php echo form_open('page_controller/update_page_post'); ?>

<div class="form-group">
            <div class="row">
                <div class="col-sm-3 col-xs-12">
                    <label><?php echo trans('subcategory'); ?></label>
                </div>
           </div><div class="col-xm-12">
                         <div class="table-responsive">
                             <table class="table table-bordered table-striped" role="grid">
                            <tbody>
                                <tr>
                                <?php   $valuesub = ($page->subcat_recip_id); ?>
                            <?php   $array_of_values = explode(",", $valuesub); 
                                //if ($item['parent_id'] != "0" && $item['subcat_recip_id'] == "0") :
                    foreach ($array_of_values as $item) { 

                        if(in_array($valuesub,$item)): {  ?>

                       <td>
                           <input type="checkbox" name="subcat_recip_id[]" class="square-purple" value="<?php echo html_escape($item["title"]); ?>" CHECKED> &nbsp; <?php echo html_escape($item["title"]);
                                    } ?>
                                <?php   else: { ?>
<input type="checkbox" name="subcat_recip_id[]" class="square-purple" value="<?php echo html_escape($item["title"]); ?>"> &nbsp; <?php echo html_escape($item["title"]);
                        } 

                                endif;  }?>
                                </td> 
        <?php echo html_escape($valuesub); ?></tr>
                                 </tbody>
                             </table>
                 </div>   
                </div>
        </div>

PAGE_CONTROLLER.PHP

 public function update_page_post()
{
    //validate inputs
    $this->form_validation->set_rules('title', trans("title"), 'required|xss_clean|max_length[500]');

    if ($this->form_validation->run() === false) {
        $this->session->set_flashdata('errors', validation_errors());
        $this->session->set_flashdata('form_data', $this->page_model->input_values());
        redirect($this->agent->referrer());
    } else {
        //get id
        $id = $this->input->post('id', true);
        $redirect_url = $this->input->post('redirect_url', true);

        if (!$this->page_model->check_page_name()) {
            $this->session->set_flashdata('form_data', $this->page_model->input_values());
            $this->session->set_flashdata('error', trans("msg_page_slug_error"));
            redirect($this->agent->referrer());
            exit();
        }

        if ($this->page_model->update($id)) {
            $this->session->set_flashdata('success', trans("msg_updated"));

            if (!empty($redirect_url)) {
                redirect($redirect_url);
            } else {
                redirect(admin_url() . 'pages');
            }
        } else {
            $this->session->set_flashdata('form_data', $this->page_model->input_values());
            $this->session->set_flashdata('error', trans("msg_error"));
            redirect($this->agent->referrer());
        }
    }
}

PAGE_MODEL.PHP

  <?php defined('BASEPATH') OR exit('No direct script access allowed');

   class Page_model extends CI_Model
   {
//input values
public function input_values()
    {
    $checkbox = implode(',', $this->checkBox());
    $data = array(
        'lang_id' => $this->input->post('lang_id', true),
        'title' => $this->input->post('title', true),
        'slug' => $this->input->post('slug', true),
        'page_description' => $this->input->post('page_description', true),
        'page_keywords' => $this->input->post('page_keywords', true),
        'page_content' => $this->input->post('page_content', false),
        'page_order' => $this->input->post('page_order', true),
        'parent_id' => $this->input->post('parent_id', true),
        'page_active' => $this->input->post('page_active', true),
        'title_active' => $this->input->post('title_active', true),
        'breadcrumb_active' => $this->input->post('breadcrumb_active', true),
        'right_column_active' => $this->input->post('right_column_active', true),
        'need_auth' => $this->input->post('need_auth', true),
        'howmany_people' => $this->input->post('howmany_people', true),
        'difficulty' => $this->input->post('difficulty', true),
        'howmany_time' => $this->input->post('howmany_time', true),
        'location' => $this->input->post('location', true),
         'subcat_recip_id' => $checkbox
    );
    return $data;
   }
     public function checkBox(){
 $count = count($_POST['subcat_recip_id']);
 for($n=0; $n<$count; $n++){
  $checkbox[$n] = $_POST['subcat_recip_id'][$n];
  }
 return $checkbox;
}
//update page
public function update($id)
{
    //set values
    $data = $this->page_model->input_values();

    if (empty($data["slug"])) {
        //slug for title
        $data["slug"] = str_slug($data["title"]);

        if (empty($data["slug"])) {
            $data["slug"] = "page-" . uniqid();
        }
    }

    $page = $this->get_page_by_id($id);
    if (!empty($page)) {
        $this->db->where('id', $id);
        return $this->db->update('pages', $data);
    }
    return false;
}

2 个答案:

答案 0 :(得分:1)

在您查看的情况下,

<tr>
<?php   $valuesub = ($page->subcat_recip_id); ?>
<?php   $array_of_values = explode(",", $valuesub); 
foreach ($array_of_values as $item) { 
if(in_array($subcat_recip_id,$item)): {  ?>
<td>
<input type="checkbox" name="subcat_recip_id[]" class="square-purple" value="<?php echo html_escape($item["title"]); ?>" CHECKED> &nbsp; <?php echo html_escape($item["title"]);
} ?>
<?php   else: { ?>
<input type="checkbox" name="subcat_recip_id[]" class="square-purple" value="<?php echo html_escape($item["title"]); ?>"> &nbsp; <?php echo html_escape($item["title"]);
} 
endif;  }?>
</td> 
<?php echo html_escape($valuesub); ?>
</tr>

更改为:

<tr>
<?php $valuesub = ($page->subcat_recip_id);
$array_of_values = explode(",", $valuesub); ?>
<td>
<?php foreach ($array_of_values as $item) :?>
<input type="checkbox" name="subcat_recip_id[]" class="square-purple" value="<?php echo html_escape($item["title"]); ?>" <?=(in_array($subcat_recip_id,$item))?"CHECKED":""?>> &nbsp; <?=html_escape($item["title"]);?>'
<?php endforeach; ?>
</td> 
<?=html_escape($valuesub)?>
</tr>

在模型中:

public function update($id){
 //set values
$data = $this->page_model->input_values();
if (empty($data["slug"])) {
//slug for title
$data["slug"] = str_slug($data["title"]);
if (empty($data["slug"])) {
$data["slug"] = "page-" . uniqid();
}
}
$page = $this->get_page_by_id($id);
 if (!empty($page)) {
$this->db->where('id', $id);
return $this->db->update('pages', $data);
}
return false;
}

更改为:

public function update($id){
$data = $this->input_values();
if (empty($data["slug"])) {
 $data["slug"] = str_slug($data["title"]);
if (empty($data["slug"])) {
 $data["slug"] = "page-" . uniqid();
}
 }
$page = $this->get_page_by_id($id);
if (!empty($page)) {
$this->db->where('id', $id);
return $this->db->update('pages', $data);
}
return false;
}

如果出现错误,请更新, tq

答案 1 :(得分:0)

嗯,它仍然是一个bug,但是对最后的代码可以进行一些更改。很大的问题是循环,它将值重新加载$ ​​valuesub多少次。就是如果数组由三个值组成,则它将读取相同值的三倍。请查看两个屏幕截图:https://www.screencast.com/t/MfiEDhdFuX 我无法摆脱这个循环!

代码

    <td>
        <?php foreach ($menu_links as $item) : 
          $valuesub = ($page->subcat_recip_id); 
          $array_of_values = explode("," , $valuesub); 
       if ($item['parent_id'] != "0" && $item['subcat_recip_id'] == "0") :
          foreach($array_of_values as $dt): ?>
        <?php //if(in_array($dt,$item)): ?>
       <input type="checkbox" name="subcat_recip_id" class="square-purple" value="<?php echo html_escape($item["title"]); ?>" <?=(in_array($dt,$item))?"CHECKED":""?>> <?=html_escape($item["title"]);?>&nbsp;                                                
        <?php endforeach; endif;?>
        <?php endforeach; ?> 
    </td><?php echo html_escape($valuesub); ?>