这里我有一个属于不同父类别的相同类别名称的列表。当我根据id获取数据时,将得到结果,因为id对于所有人都不同,但是当我按类别名称获取数据时,将只得到第一个结果,即使我在不同父类别下有两个相同的类别名称..这是我的类别表

    id      category_name   parent_id
    8          men             0
    9          kids            0
    10       T-shirts          8
    11        Shirts           8
    12        Jeans            8
    13        Pants            8
    14        Shorts           8
    15        Tees             9
    16        Shirts           9
    17        Jeans            9
    18        Pants            9

这里有衬衫作为不同父项下的类别名称。当我选择16件衬衫的id时,由于相同的类别名称,得到id 11衬衫的值。
这是我使用的控制器
public function men_clothing_image($category=null)
{

    $category_id =$this->roxmodel->get_category_id($category);
    $data['active_mn']='men_clothing';

    $data['men']=$this->roxmodel->get_category_by_parent($p_id=8);
    $data['kids']=$this->roxmodel->get_category_by_parent($p_id=9);

    $config['base_url'] = base_url().'men-'.$category;
    $config['per_page'] = 2;
    $config['uri_rsegment'] = 4;
    $config['use_page_numbers'] = TRUE;
    $config['total_rows'] = $this->roxmodel->count_category_images($category_id);
    $data['galllery']=$this->roxmodel->get_gallery_men_images($category_id,$config['per_page'],$this->uri->rsegment(4));

    $this->load->library('pagination',$config);
    $data['page_links'] = $this->pagination->create_links();


    $this->load->view('men_clothing_image',$data);

}

这是我经过的模特
 public function get_category_id($category_name)
 {
  $this->db->select('category.id');
  $this->db->where('category.category_name',$category_name);
  $result = $this->db->get('category')->row();
  if($result)
  {
   return $result->id;
  }
}

我已经完成了连接id和类别名称的自连接,但是输出为空
public function get_category_id($category_name)
{
$this->db->select('c.id');
$this->db->join('category c1','c.id=c1.category_name');
$this->db->where('c1.category_name',$category_name);
$result = $this->db->get('category c')->row();
  if($result)
  {
    return $result->id;
  }
}

计数图像函数如下。。
public function count_category_images($p_id)
{
  $this->db->select('gallery.*','category.category_name');
  $this->db->join('category', 'category.id = gallery.category_id');
  $this->db->where('category.id',$p_id);
  $this->db->order_by('gallery.id','desc');
  return $this->db->count_all_results('gallery');
}

最佳答案

你必须使用result()result_array()。因为结果中有几行。
row()-此函数返回单个结果行。
result()-此函数将查询结果作为对象数组返回,如果失败则返回空数组。
所以,试试看

public function get_category_id($category_name)
{
  $this->db->select('category.id');
  $this->db->join('category c1','c.id=c1.category_name');
  $this->db->where('category.category_name',$category_name);
  $result = $this->db->get('category')->result();

  return $result;
}

你也可以用
if ($query->num_rows() > 0)

检查是否有行。
如果使用、搜索和使用部分类别名称,请使用LIKEinterms ofWHERE

10-07 12:26
查看更多