【问题标题】:Show data from multiple checkbox in codeigniter在 codeigniter 中显示来自多个复选框的数据
【发布时间】:2017-05-02 06:25:59
【问题描述】:

希望大家能帮我解决我的申请问题

我的问题是,当我想显示多个复选框中的数据时,数据成功显示,但只显示我检查的第一个数据。 例如,我检查了数据 1、数据 2、数据 3,但我的页面上只显示了数据 1。

我的控制器:

function comparison()
    {
        if ($this->input->post('submit')) {
            foreach ($id_product = $this->input->post('id_product') as $rm) {
                $show_compare = $this->Compare->start_compare($rm);
            }
            $data['comparison'] = $show_compare;
            $data['title'] = "Comparison";
            $data['meta_keywords'] = ". . .";
            $data['meta_descriptions'] = ". . .";
            $this->load->view('theme/comparison',$data);
        }
    }

我的模特:

function start_compare($id_product)
    {
        $this->db->select('product.id_subcategory,product.type,product.product_name,specificcategory.specificcategory_name,specification_biostar.*');
        $this->db->join('specification_biostar', 'specification_biostar.id_product = product.id_product', 'left');
        $this->db->join('specificcategory', 'specificcategory.id_specificcategory = product.id_subcategory', 'left');
        $this->db->where('product.id_product', $id_product);
        $sql = $this->db->get('product')->result_array();
        return $sql;
    }

我的观点(选项多复选框):

<div class="box-body">
     <input type="checkbox" name="id_product[]" id="txt" onClick="EnableSubmit3(this)" value="<?php echo $row['id_product']; ?>"><label>Choose</label>
     </div>

我的观点(结果数据):

 <table class="table">
                <?php foreach ($comparison as $row){ ?>
                <tr>
                    <td colspan="2"><?php echo $row['id_product'] ?></td>
                </tr>
                <?php } ?>
            </table>

【问题讨论】:

  • 您将所有复选框值存储在数据库中??
  • 您将覆盖 $show_compare 变量每次迭代 foreach ($id_product = $this->input->post('id_product') as $rm) { $show_compare = $this->Compare->start_compare ($rm); } 所以就像这样使用 $show_compare[]
  • @rajat gupta,不。我从数据库中获取的多个复选框,当我检查其中一些复选框时只想显示我检查的内容
  • @JYoThI 你能给我一个例子我应该改变代码吗?
  • 用这个 $show_compare[] 数组替换 $show_compare 变量,它将所有值存储在 $show_compare 数组中。

标签: codeigniter checkbox show


【解决方案1】:

在您的代码中使用 $show_compare = array();在 foreach 循环之前并在 foreach 中使用 array_push。

$show_compare = array();
   foreach ($id_product = $this->input->post('id_product') as $rm) {
        array_push($show_compare,$this->Compare->start_compare($rm)); 
   }
   $data['comparison'] = $show_compare;

【讨论】:

  • array_push 是什么意思?
  • array_push() 函数将一个或多个元素插入到数组的末尾。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-24
  • 1970-01-01
相关资源
最近更新 更多