首页 文章

codeigniter模型查询没有正确发送json数据

提问于
浏览
0

我有一个codeigniter模型函数,它查询数据库并将结果发送回控制器,编码为json .

整个功能如下所示:

function get_skufamily_cube($q){

        $sql=("select min([Pieces]) as ProductCode from
(SELECT  
       [ProductCode]
      ,[Description]
      ,[Length]
    ,[Pieces]
      ,[Thickness]
      ,[Width]
    ,([width]*1000) w2
    ,([thickness]*1000) t2
    ,REPLACE((convert(varchar,convert(decimal(8,1),length))),'.','') AS l2
    ,concat(([width]*1000),([thickness]*1000),REPLACE((convert(varchar,convert(decimal(8,1),length))),'.','')) AS pc
    ,REPLACE([ProductCode],concat(([width]*1000),([thickness]*1000),REPLACE((convert(varchar,convert(decimal(8,1),length))),'.','')),'') as grade
    ,CONCAT(([width]*1000),([thickness]*1000),REPLACE([ProductCode],concat(([width]*1000),([thickness]*1000),REPLACE((convert(varchar,convert(decimal(8,1),length))),'.','')),'')) as options

  FROM [hammerhead].[dbo].[ProductList]) as p
         where Options like '%$q%' ");
    $query=$this->db->query($sql);


    if($query->num_rows > 0){
      foreach ($query->result_array() as $row){
        $row_set[] = htmlentities(stripslashes($row['ProductCode']));
        echo  $row['ProductCode']; // to see database result and troubleshoot
      }
      $this->output->set_content_type('application/json')->set_output(json_encode($row_set));
    }

  }

$ q正在成功传递给查询, echo $row['ProductCode']; 中查询的输出与我需要的结果一致 . 在这种情况下,它是108.数据库查询在单个字段中返回单个结果 .

出于某种原因,这并没有正确地传递回控制器 .

控制器是:

$this->load->model('Sales_model');
    if (isset($_POST['data'])){
        $q = strtolower($_POST['data']);
        $data = $this->Sales_model->get_skufamily_cube($q);
        $this->output->set_content_type('application/json')->set_output(json_encode($data));
    }
}

在我的开发人员工具中,我可以看到服务器响应 108NULL . 108 是我的回声和 NULL 是json的回应 . 如果我删除回声,这只是NULL .
enter image description here

Laslty,我需要用值填充表格行中的输入 . 我的视图jquery语法是这样的:

$.post('get_skufamily_cubes', {data:selectedObj.value},function(result) 
 { 
 $(this).find('input[id^="cubesperbundle"]').val(result); 
 });

目前没有任何人填充 . html输入名称和id是: cubesperbundle 但是附加了行号因此 'input[id^="cubesperbundle"]'

任何援助将不胜感激 .

感谢致敬,

1 回答

  • 1

    我看到的错误是你没有向控制器返回任何东西 .

    模型

    function get_skufamily_cube($q)
    {
        $Query="select
                  min(Pieces) as ProductCode
                from (SELECT
                        ProductCode,
                        Description,
                        Length,
                        Pieces,
                        Thickness,
                        Width,
                        (width*1000)    w2,
                        (thickness*1000)    t2,
                        REPLACE((convert(varchar,convert(decimal(8,1),length))),'.','') AS l2,
                        concat((width*1000),(thickness*1000),REPLACE((convert(varchar,convert(decimal(8,1),length))),'.','')) AS pc,
                        REPLACE(ProductCode,concat((width*1000),(thickness*1000),REPLACE((convert(varchar,convert(decimal(8,1),length))),'.','')),'') as grade,
                        CONCAT((width*1000),(thickness*1000),REPLACE(ProductCode,concat((width*1000),(thickness*1000),REPLACE((convert(varchar,convert(decimal(8,1),length))),'.','')),'')) as options
                      FROM hammerhead.dbo.ProductList) as p
                where Options like '%$q%'";
        return  $this->db->query($Query)->row();
    }
    

    调节器

    function getJson(){
        $this->load->model('Sales_model');
        if (isset($_POST['data'])){
            $q = strtolower($_POST['data']);
            $viewData   =   $this->Sales_model->get_skufamily_cube($q);
            $data_json = json_encode($viewData);
            echo $data_json;
        }
    }
    

    EDITS:
    更改模型功能中的返回指令 .

    $result = $this->db->query($Query)->row();
    return $result->ProductCode;
    

相关问题