在Codeigniter中转换为空字符串而不是null

时间:2018-12-14 05:47:30

标签: php codeigniter

我正在尝试将说明值插入db,并且将其作为空值插入,但是我希望它应该为空字符串,因为在前端从db渲染数据时,它仅显示为null         这是我的控制器         Import_Controller.php

        class Import_Controller extends CI_Controller {
    public function __construct() {
          parent::__construct(); 
          $this->load->model('Import_Model');
          $this->lang->load('general_lang');  
          $this->load->library('excel');

          if (!$this->ion_auth->logged_in())
          {
                // redirect them to the login page
            redirect('auth/login', 'refresh');
        }

    }

    public function index(){
        $data['content'] = 'admin/import/Import';
        $this->load->view('inc/template',$data);
    }


    public function import()
     {
      if(isset($_FILES["file"]["name"]))
      {
       $path = $_FILES["file"]["tmp_name"];
       $object = PHPExcel_IOFactory::load($path);
       foreach($object->getWorksheetIterator() as $worksheet)
       {
        $highestRow = $worksheet->getHighestRow();
        $highestColumn = $worksheet->getHighestColumn();

        for($row=2; $row<=$highestRow; $row++)
        {
            $name = $worksheet->getCellByColumnAndRow(0, $row)->getValue();
            $size = $worksheet->getCellByColumnAndRow(1, $row)->getValue();
            $manufacturer = $worksheet->getCellByColumnAndRow(2, $row)->getValue();
            $model_no = $worksheet->getCellByColumnAndRow(3, $row)->getValue();
            $model_name = $worksheet->getCellByColumnAndRow(4, $row)->getValue();
            $description = $worksheet->getCellByColumnAndRow(5, $row)->getValue();
            $width = $worksheet->getCellByColumnAndRow(6, $row)->getValue();
            $depth = $worksheet->getCellByColumnAndRow(7, $row)->getValue();
            $kit_pn = $worksheet->getCellByColumnAndRow(8, $row)->getValue();
            $color = $worksheet->getCellByColumnAndRow(9, $row)->getValue();
            $remarks = $worksheet->getCellByColumnAndRow(10, $row)->getValue();

            $arr['dm'] = array('name'  => $name,'size'   => $size);
            $arr['im'] = array('manufacturer' => $manufacturer,'model_no' => $model_no,'model_name' => $model_name,'description' => $description,'width' => $width,'depth' => $depth);
            $arr['ak'] = array('kit_pn' => $kit_pn,'color' => $color,'remarks' => $remarks);
            $new_array[] = $arr;    
        }
       }        

       $this->Import_Model->insert($new_array);

          echo 'Data Imported successfully';

      } 
     }
    }

我的模型是:Import_Model.php

        class Import_Model extends CI_Model
    {
        public function getValues($value){

            $data = $this->db->get_where('dispenser_model', $value['dm'])->row();


            if(empty($data)){

                $this->db->insert('dispenser_model',(isset($value['dm']) ? $value['dm'] : ''));
                $dm_insert_id = $this->db->insert_id();

            }  else{

                $dm_insert_id = $data->id;
            }

            $data1 = $this->db->get_where('im_manufacturer', $value['im'])->row();

            if(empty($data1)){
                $this->db->insert('im_manufacturer',array(
                    'manufacturer' =>$value['im']['manufacturer'],
                    'model_no' => $value['im']['model_no'],  
                    'model_name' => $value['im']['model_name'],
                    'description' => /*empty($value['im']['description']) ? $value['im']['description'] : "",*/

                    str_replace('Null', '' , $value['im']['description']),
                    'width' => $value['im']['width'],
                    'depth' => $value['im']['depth']
                ));

                //echo $this->db->last_query();

                $im_insert_id = $this->db->insert_id();

            }   else{

                $im_insert_id = $data1->id;
            }


            $data2 = $this->db->get_where('adaptor_kit',$value['ak'])->row();

            if(empty($data2)){
                $this->db->insert('adaptor_kit',$value['ak']);

                $ak_insert_id = $this->db->insert_id();

            }   else{
                $ak_insert_id = $data2->id;
            } 

            $data3 = $this->db->get_where('model', array(
                'dm_id' => $dm_insert_id,
                'im_id' =>$im_insert_id,
                'ak_id'=> $ak_insert_id))->row();

            if(empty($data3)){
                $this->db->insert('model',array(
                    'dm_id' => $dm_insert_id,
                    'im_id' =>$im_insert_id,
                    'ak_id'=> $ak_insert_id));

                $model_insert_id = $this->db->insert_id();

            }   else{
                $model_insert_id = $data->id;
            } 


        }

        public function insert($new_array)
        {
            $my_array = [];

            foreach ($new_array as $value) {

            $my_array[] = $this->getValues($value);

            //echo '<pre>' ; print_r($value);

            }

        }

    }

我的查看文件是:Import.php

    <div class="content">
  <div class="animated fadeIn">
    <div class="row">
      <div class="col-lg-12">           
        <h4>Import Data   
        <div class="pull-right">  
            <a class="btn btn-primary" href="<?php echo base_url('assets/spreadsheet/sample.xlsx')?>">Download template <i class="menu-icon fa fa-download"></i></a>
        </div>
        </h4>
      </div>
      <br><br><br>
      <div class="col-lg-12"> 
        <h4>Upload File </h4>   
        <br>   

        <form method="post" id="import_form" enctype="multipart/form-data">
            <input type="file" name="file" id="file" required accept=".xls, .xlsx" class="input-large" >
            <button type="submit" id="submit" name="import" class="btn btn-primary button-loading">Import</button>
        </form>
        <br />
        <div id="customer_data">
        </div>
    </div>




    </div>
  </div>
</div>

<script>
$(document).ready(function(){

 load_data();

 function load_data()
 {
    $('#import_form').on('submit', function(event){
  event.preventDefault();
  $.ajax({
   url:"<?php echo base_url(); ?>index.php/admin/Import_Controller/import",
   method:"POST",
   data:new FormData(this),
   contentType:false,
   cache:false,
   processData:false,
   success:function(data){
    $('#file').val('');
    load_data();
    alert(data);
   }
  })
 });

 }



});
</script>

我想将null插入为空字符串怎么办?

0 个答案:

没有答案