Search code examples
configurationcontrollercodeigniter-2delete-row

How to properly set your controller with CodeIgniter


I'm testing codeIgniter 2.2.0 for a personnal project. I've been reading for some tutorials but i get stuck with my CRUD : when I want to delete an element, the redirection works correctly but it doesnt suppress the element.

Here's an extract of my controller :

class Admin extends CI_Controller{
  function __construct(){
      parent::__construct();        
  }

  function index(){
    $data['heading'] = 'Panneau d\'administration';
    $data['title'] = 'Général';     
    $data['content'] = 'admin_panel';
    $this->load->view('admin/adminTemplate',$data);
}

function marques(){
    $data['heading'] = 'Panneau d\'administration | Marques';
    $data['title'] = 'Les marques'; 
    $data['content'] = 'admin_marques';
    $data['row'] = $this->admin_model->readMarque();
    $this->load->view('admin/adminTemplate',$data);
}



/******************************************
================CRUD=======================
*******************************************/

/******************************************
          CRUD Marques
  *******************************************/

function updateMarque(){
    if($this->uri->segment(3)){
        $data['row'] = $this->admin_model->getMarque($this->uri->segment(3));
        $data['heading'] = $data['row']->marque_lib;
        $this->admin_model->update($this->uri->segment(3));
        $this->load->view('admin_update', $data);            

    }
    else{
        redirect('admin');

    }
}

function deleteMarque(){
    //récupération de l'id dans ladresse
    if($this->uri->segment(4)){         
        $this->admin_model->deleteMarque($this->uri->segment(4));
        redirect('admin');              
    }

    else{

        redirect('index.php/admin');            

    }

}

function createMarque(){}

Here's an extract of my model :

 class Admin_model extends CI_Model{
function __construct(){
    parent::__construct();      
}


 /******************************************
          CRUD Marques
 *******************************************/

function create_marque(){

}

function readMarque(){
    $req = $this->db->get('marques');
    if ($req->num_rows>0) {
        foreach ($req->result() as $row) {
            $data[] = $row;
        }
        return $data;
    }       
}

function updateMarque($id, $data){
    $this->db->where('marque_id', (int)$id);
    $this->db->delete('marques', $data);
}

function getMarque($id){
    $this->db->where('marque_id', (int)$id);
    $marque = $this->db->get('marques');
    if($marque->num_rows>0){
        $row = $marque->row();
        return $row;
    }
}

function deleteMarque($id){
    $this->db->where('marque_id', $id);
    $this->db->delete('marques');
}

And my view is like :

<table class="table table-striped">
          <thead>
            <tr>
              <th>#</th>
              <th>Libellé</th>
              <th>Chemin de l'image</th>   
              <th>Actions</th>               
            </tr>
          </thead>

          <tbody>
            <?php 
                if ($row != null) : 
                    foreach ($row as $infoMarque): ?>
                        <tr>
                          <td><?php echo $infoMarque->marque_id ; ?></td>
                          <td><?php echo $infoMarque->marque_lib ; ?></td>
                          <td><?php echo $infoMarque->marque_img_path ; ?></td> 

                          <td>
                            <button type="button" class="btn btn-default btn-default">
                                <span class="glyphicon glyphicon-pencil"></span> 
                                <a href="<?php echo site_url('index.php/admin/marques/updateMarque/'.$infoMarque->marque_id) ; ?>">Editer</a>
                            </button>

                            <button type="button" class="btn btn-default btn-default">
                                <span class="glyphicon glyphicon-remove"></span> 
                                 <a href="<?php echo site_url('index.php/admin/marques/deleteMarque/'.$infoMarque->marque_id) ; ?>">Supprimer</a>
                            </button>
                        </td>  
                        </tr>
            <?php endforeach; endif;?>


          </tbody>
        </table>

I dont know why this doesnt work. I thought that maybe its because there's subpages and actions for thoses subpages not for the controller himself...but i cant figure it ou

Sorry for my bad english

Any ideas and advices would be great

Thanks



Solution

  • Sorry for the late answer. I've already figured out the problem. In my link to suppress an element, my route was

     site_url('index.php/admin/marques/updateMarque/'.$infoMarque->marque_id)  
    

    It should be :

     site_url('index.php/admin/updateMarque/'.$infoMarque->marque_id)  
    

    Becasue the action updateMarque() isn't a 'subfunction' of marques.

    Maybe everybody already know this but I've also found that it was better to use base_url() function : it allows to suppress the 'index.php' in the link