如何使用getAll()

时间:2017-10-30 11:15:18

标签: codeigniter

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

class Unit extends CI_Controller {
    
    /*Load supplier default*/
    public function index($id='')
    {

       $this->load->model('Unit_model');
        ($id!='') ? $data["unit_details"]=$this->Unit_model->get_unit($id) :'';
    
    $this->load->view('includes/header');
    $this->load->view('includes/left_menu');
    $this->load->view('unit/unit_manage', ($id!='') ? $data : '');
    $this->load->view('includes/footer');

    /*Submit supplier on add/edit */
}
public function post_unit()
  {
     $this->load->model('Unit_model');
      $this->db->trans_begin();
      // transaction begin statement
      try{ //Use try catch statement here
         $this ->db-> trans_complete();
    if ($this->input->is_ajax_request()){
  
          $case = $this->input->post('case');
          //for both case use insert_customer function 
        
        if($case=='edit')
          $insert_unit=$this->Unit_model->update_unit();
        else
          $insert_unit=$this->Unit_model->insert_unit();
        
          $data['insert_unit']=$insert_unit;
        
          //place commit statement
        $this ->db-> trans_commit();
    }

else
          {
          // through new exception statement
      
          }
      }
      catch(Exception $e){ //statement here
       // Place roll back statement
        $this->db-> trans_rollback();

      }
      $this->sendData($data);
  }

  } ?>

这是我的控制器部分。

class Unit_model extends CI_Model
{
    protected $strTableName = 'suc_unit';

    function __construct()
    {
        parent::__construct();
        $CI = &get_instance();
        $this->db->from($this->strTableName);
    }

    /*Load all details of table*/
    function get_all()
    {
        $query = $this->db->get($this->strTableName);
        return $query->result();
    }
}

以上是我的模型...我希望使用模型中声明的getAll()在我的页面中显示数据。插入,更新和视图应该在同一页面中进行

<table id="tblListOfUnits" class="table table-bordered table-striped">
    <tr>
        <th style="width: 10px">Sl No</th>
        <th>Unit Name</th>
        <th>Unit Symbol</th>
        <th style="width: 40px">No.Of Decimal</th>
    </tr>
    <?php
    if(isset($units)){
        $count = 1;
        foreach ($units as $key => $unit) {
            ?>
            <tr id="tr_<?php echo $unit->pk_int_unit_id; ?>">
                <td id="tdUntSlNo_<?php echo $unit->pk_int_unit_id; ?>"><a href="javascript:unitEdit(<?php echo $unit->pk_int_unit_id; ?>)" ><?php echo $count; ?>.</a></td>
                <td id="tdUntNme_<?php echo $unit->pk_int_unit_id; ?>"><a href="javascript:unitEdit(<?php echo $unit->pk_int_unit_id; ?>)" ><?php echo $unit->vchr_unit_name; ?></a></td>
                <td id="tdUntSymbl_<?php echo $unit->pk_int_unit_id; ?>"><a href="javascript:unitEdit(<?php echo $unit->pk_int_unit_id; ?>)" ><?php echo $unit->vchr_unit_symbol; ?></a></td>
                <td id="tdUntDecml_<?php echo $unit->pk_int_unit_id; ?>"><a href="javascript:unitEdit(<?php echo $unit->pk_int_unit_id; ?>)" ><?php echo $unit->int_no_decimal; ?></a></td>
            </tr>
            <?php
            $count ++;
        }
    }?>

</table>

这是我的视图页面我希望使用我在模型中编写的getAll()语句在该表中显示数据。该页面包含add&amp;一起查看页面。请帮我完成我的工作

0 个答案:

没有答案