2013-04-23 87 views
0

我有一个codeigniter模型函数,它查询数据库并将结果发送回控制器,编码为json。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)); 
    } 
} 

在我的开发人员工具,我可以看到服务器响应 108NULL108是我的回声,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

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

型号

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; 
+0

@Smudger查看答案并阅读其中的注释。 – 2013-04-23 07:11:58

+0

谢谢Raheel,感谢你总是帮助我。服务器对此的回应是:'[{“ProductCode”:108}]'我只需要108.任何想法?再次感谢。 – Smudger 2013-04-23 07:15:25

+0

嗨Raheel,我已经添加到我的问题,我似乎无法填充我的输入与此结果。你可以看看我的看法jQuery。再次感谢。 – Smudger 2013-04-23 07:21:22