2015-05-09 170 views
1

我有一个完整的工作代码,用于检索插入更新并将数据删除到数据库。codeigniter为foreach提供的无效参数()

所有的工作都很好,但是如果我删除了一些数据,它会被删除,但是当再次指向我的查看页面时,它将返回“为foreach()提供的无效参数”。

当我再次按下我的视图页面链接时,它是正常的,我删除的数据也不见了,这只是与删除功能发生。

代码:

控制器:

public function merk(){ 
     //$data["result"] = $this->modelAdminMerk->getMerk(); 
     $data["totalRows"] = $this->modelAdminMerk->getTotalRows(); 

     $config["base_url"] = base_url() . "admin/merk"; 
     $config["per_page"] = 15; 
     $config["total_rows"] = $data["totalRows"]; 
     $config["uri_segment"] = 3; 
     $config["num_links"] = 10; 

     $this->pagination->initialize($config); 

     if(! $this->uri->segment(3)){ 
      $page = 0; 
     }else{ 
      $page = $this->uri->segment(3); 
     } 

     $data["results"] = $this->modelAdminMerk->getMerkByPages($config["per_page"], $page); 
     $data["links"] = $this->pagination->create_links(); 

     $this->load->view("adminView/adminHeader"); 
     $this->load->view("adminView/adminMerk", $data); 
     $this->load->view("adminView/adminFooter"); 
    } 

    public function insertMerk(){ 
     $this->load->view("adminView/adminHeader"); 
     $this->load->view("adminView/adminInsertMerk"); 
     $this->load->view("adminView/adminFooter"); 
    } 

    public function validateInsertMerk(){ 
     $this->form_validation->set_rules("namaMerk", "Nama Merk", "trim|required|min_length[3]|max_length[30]"); 

     if($this->form_validation->run()){ 

      $newMerk = array("merk" => $this->input->post("namaMerk")); 
      $this->modelAdminMerk->insertMerk($newMerk); 
      $this->merk(); 
     }else{ 
      echo "GAGAL!!!"; 
     } 
    } 

    public function editMerk($id){ 
     $data["merk"] = $this->modelAdminMerk->getMerkById($id); 

     $this->load->view("adminView/adminHeader"); 
     $this->load->view("adminView/adminEditMerk", $data); 
     $this->load->view("adminView/adminFooter"); 
    } 

    function validateEditMerk(){ 
     $this->form_validation->set_rules("namaMerk", "Nama Merk", "trim|required|min_length[3]|max_length[30]"); 

     if($this->form_validation->run()){ 

      $newMerk = array("merk" => $this->input->post("namaMerk")); 
      $this->modelAdminMerk->editMerk($this->input->post("id"), $newMerk); 
      $this->merk(); 
     }else{ 
      echo "GAGAL"; 
     } 

    } 

    public function deleteMerk($id){ 
     $this->modelAdminMerk->deleteMerk($id); 
     $this->merk(); 
    } 

型号:

function getMerk(){ 
     $data = $this->db->query("SELECT * FROM merkbarang"); 
     return $data->result(); 
    } 

    function getMerkById($id){ 
     $this->db->where("id", $id); 
     $data = $this->db->get("merkbarang"); 
     return $data->row(); 
    } 

    function insertMerk($newMerk){ 
     $this->db->insert("merkbarang", $newMerk); 
     return true; 
    } 

    function editMerk($id, $newMerk){ 
     $this->db->where("id", $id); 
     $this->db->update("merkbarang", $newMerk); 
     return true; 
    } 

    function deleteMerk($id){ 
     $this->db->where("id", $id); 
     $this->db->delete("merkbarang"); 
     return true; 
    } 

    function getTotalRows(){ 
     $rows = $this->db->count_all("merkbarang"); 

     return $rows; 
    } 

    function getMerkByPages($limit, $start){ 
     $this->db->limit($limit, $start); 
     $query = $this->db->get("merkbarang"); 

     if($query->num_rows() > 0){ 
      foreach ($query->result() as $row){ 
       $data[] = $row; 
      } 
      return $data; 
     }else{ 
      return false; 
     } 
    } 

查看:

<div class="container"> 
    <div class="row"> 
     <div class="col-md-3"> 
      <div class="panel panel-danger"> 
       <div class="panel-body"> 
        <ul class="nav nav-pills nav-stacked"> 
         <li role="presentation" class="active"><a href="<?php echo base_url(); ?>admin/insertMerk">Insert Merk</a></li> 
        </ul> 
       </div> 
      </div> 
     </div> 

     <div class="col-md-9"> 
      <div class="panel panel-default"> 
       <div class="panel-heading"> 
        <strong>Merk</strong> 
       </div> 
       <div class="panel-body"> 
        <div class="row" id="tabelBarang"> 
         <div class="col-md-1 colBarang"> 
          <strong>No</strong> 
         </div> 
         <div class="col-md-7 colBarang"> 
          <strong>Merk</strong> 
         </div> 
         <div class="col-md-4 colBarang"> 
          <strong>Action</strong> 
         </div> 
        </div> 

        <?php 
         $angka = 0; 
         foreach ($results as $row){ 
          $id = $row->id; 
          $merk = $row->merk; 
        ?> 


        <div class="row" id="tabelBarang"> 
         <div class="col-md-1 colBarang"> 
          <?php echo ++$angka; ?> 
         </div> 
         <div class="col-md-7 colBarang"> 
          <?php echo $merk; ?> 
         </div> 
         <div class="col-md-2 colBarang"> 
          <button type="button" class="btn btn-info btn-xs" aria-label="Left Align"> 
           <a href="<?php echo base_url(); ?>admin/editMerk/<?php echo $id; ?>">Edit</a>       
          </button> 
         </div> 
         <div class="col-md-2 colBarang"> 
          <button type="button" class="btn btn-danger btn-xs" aria-label="Left Align"> 
           <a href="<?php echo base_url(); ?>admin/deleteMerk/<?php echo $id; ?>">Delete</a>       
          </button> 
         </div> 
        </div> 

        <?php 
         } 
        ?> 
        <?php echo $links; ?> 
       </div> 


      </div> 
     </div> 
    </div> 
</div> 
+1

请问,如果你改变'$这个 - 它的工作>默克();'到'重定向(“控制器/默克”)' ? –

+0

@JeremyJackson我打算建议同样的事情;基于我在视图中可以看到的,我认为控制器是admin,所以'redirect('admin/merk');' –

+1

omg,这是一个很好的答案,我不认为重定向和使用$ this->会不同..问题解决了,谢谢jeremy和ben –

回答

0

我看到它的方式,没有什么错在你的代码,这应该defi奈特雷工作,但我建议你尝试在delete功能CI的redirect()方法

public function deleteMerk($id){ 
     $this->modelAdminMerk->deleteMerk($id); 
     redirect('controllername/merk'); 
    } 
相关问题