如何在视图中打印else语句

时间:2014-02-07 06:48:56

标签: codeigniter

我想在视图中打印“找不到记录”


public function getProductsListByCategory4($limit, $start) 
{                       
    $this->db->limit($limit, $start);
    $this->db->select('image_path,tagline,category_id,product_id');
    $this->db->where('category_id','4');
    $query = $this->db->get("tb1_bl_products order by tagline");

    if ($query->num_rows() > 0) 
    {
        foreach ($query->result() as $row) 
        {
            $data[] = $row;
        }
        return $data;
    }
    else
    {
        echo 'No records found';
    }
}

4 个答案:

答案 0 :(得分:1)

我希望您在模型中使用此代码。

改进模型中的代码,如下所示:

public function getProductsListByCategory4($limit, $start) 
{                       
    $this->db->limit($limit, $start);
    $this->db->select('image_path,tagline,category_id,product_id');
    $this->db->where('category_id','4');
    $query = $this->db->get("tb1_bl_products order by tagline");

    if ($query->num_rows() > 0) 
    {
        return $query->result_array(); // single line which removes for loop
    }
    else
    {
        return array();
    }
}

在控制器中:

$data["records"] = $this->MODEL_NAME->getProductsListByCategory4($limit, $start);
$this->load->view("VIEW_FILE",$data);

在您看来,

if(count($records)>0)
{
   foreach($records as $r)
   {
       echo $r["FIELD_NAME"];
   }
}
else
{
  echo "<p>No Records found</p>";
}

答案 1 :(得分:0)

public function getProductsListByCategory4($limit, $start) 
{                           
    $this->db->limit($limit, $start);
    $this->db->select('image_path,tagline,category_id,product_id');
    $this->db->where('category_id','4');
    $query = $this->db->get("tb1_bl_products order by tagline");

    if ($query->num_rows() > 0) 
    {
        foreach ($query->result() as $row) 
        {
            $data[] = $row;
        }
        return $data;
    }
    else
    {

        $data = '';
        return $data;
    }
}

然后检查您的VIEW,如果$data为空,则显示“未找到记录”

答案 2 :(得分:0)

以这种方式使用计数: -

public function getProductsListByCategory4($limit, $start) 
{     
    $data=array();                  
    $this->db->limit($limit, $start);
    $this->db->select('image_path,tagline,category_id,product_id');
    $this->db->where('category_id','4');
    $query = $this->db->get("tb1_bl_products order by tagline");

    if ($query->num_rows() > 0) 
    {
        foreach ($query->result() as $row) 
        {
            $data[] = $row;
        }
    }
    return $data;
 }

在视图中: -

if(count($data)){
   // do your stuff here
}
else{
 echo 'No records found';
}

答案 3 :(得分:0)

这里的每个其他答案都使用count(),这一点都不错,但我觉得这很浪费。

我发现这是最有效的方法

<强>模型

...
    if ($query->num_rows() > 0) { //using count here!

        return $query->result_array(); // or result() if you want array of objects

    } else {

        return FALSE;

    }
...

控制器 //完全与kumar_v回答

if ($data['product_list'] = $this->model->model_method($param, $param2)) {
    //if model returns FALSE this code is jumped over
    $this->load->view('view_file_no_data');
} else {
    //all good we have some data to show load
    $this->load->view('view_file', $data);
}

<强> view_file

// no need for checking if there are some data or not
foreach($product_list as $key => $product) {
    echo $product['image_path'];
}

<强> view_file_no_data

echo "no results";
  

我为这个例子分割了视图,通常你会创建一个视图   制作if/else