CodeIgniter:向模型发出传递参数

时间:2015-12-09 11:52:33

标签: php codeigniter

我试图传递一个模型,一个从控制器传入的参数。因此,如果我输入网址www.foobar.com/report/ecomma/ford('报告'是控制器名称,' ecomma'功能' ford'参数),请在网址中添加#39; m期望控制器传递模型argumnet 'ford'然后模型应该使用传递给它的参数LIKE "%$brand%"";执行sql查询。

我有以下控制器(称为报告):

    public function ecomma($brand){
        $this->load->model('report_model');
        $data ['query'] = $this->report_model->generate_brand_data_report($brand);
        $this->load->view('brand_report_view',$data);
    }
}

与控制器关联的模型位于:

public function generate_brand_data_report($brand = ''){
$sql = "SELECT 
    DISTINCT pd_model_code AS `Model_SKU`,
             pd_model_description AS `Model_Title` 
    FROM iris_product_data
    WHERE pd_vendor LIKE "%$brand%"";

$query = $this->db->query($sql);
return $query->result();

}

然而,这个论点没有通过,任何人都可以在这里看到这个问题吗?

2 个答案:

答案 0 :(得分:2)

在Active Record中,您可以将查询编写为

CREATE FUNCTION [dbo].[Split] (@StringToSplit NVARCHAR(MAX), @SpliterChar CHAR(1))
RETURNS
    @returnList TABLE ([item] [NVARCHAR] (500))
AS
BEGIN
 DECLARE @nItem NVARCHAR(500);
 DECLARE @pos INT; 
 WHILE CHARINDEX(@SpliterChar, @StringToSplit) > 0
 BEGIN
  SELECT @pos  = CHARINDEX(@SpliterChar, @StringToSplit); 
  SELECT @nItem = SUBSTRING(@StringToSplit, 1, @pos-1);
  if (@nItem <> '') INSERT INTO @returnList SELECT @nItem;
  SELECT @StringToSplit = SUBSTRING(@StringToSplit, @pos+1, LEN(@StringToSplit)-@pos);
 END
 if (@StringToSplit<>'') INSERT INTO @returnList SELECT @StringToSplit;
 RETURN
END

答案 1 :(得分:1)

你不能在双引号内使用双引号你可以像这样使用单引号或双引号:

$sql = "SELECT 
DISTINCT pd_model_code AS `Model_SKU`,
         pd_model_description AS `Model_Title` 
FROM iris_product_data
WHERE pd_vendor LIKE \"%$brand%\"";

 $sql = "SELECT 
DISTINCT pd_model_code AS `Model_SKU`,
         pd_model_description AS `Model_Title` 
FROM iris_product_data
WHERE pd_vendor LIKE '%$brand%'";