gpt4 book ai didi

php - 一个表的 sql 结果与另一个表(包括子字段)的字段相匹配

转载 作者:行者123 更新时间:2023-11-29 11:40:32 25 4
gpt4 key购买 nike

使用 codeigniter 查询我无法获得适当的结果我需要通过这种方式获取表格的结果

$catid=$_POST['category'];
$new_id = select catid from categories_table where catid='$catid' and parent='$catid';

所以它还会添加包括其他以 $cadid 作为 parent 的猫的结果

Select * from articles_table where catid = '$new_id';

我正在尝试像这样的codeigniter

  $p=$this->input->post('category');
$this->db->select('catid');
$this->db->where('parent',$p);
$this->db->where('catid',$p);
$query = $this->db->get('categories_table');

if($query->num_rows()>0)
{
foreach($query->result() as $row)
$new_id[]=$row->catid;
}
$this->db->where('catid',$new_id);
$this->db->where('status',1);
$this->db->order_by('time_added', 'desc');
$res = $this->db->get('articles_table');
$query_res= $res->result();

它给出错误消息:尝试获取非对象的属性

cats table
catid -- parent -- name
1 -- 0 -- first cat
2 -- 1 -- cat child of first

Article table
id -- catid - content
1 -- 2 -- first article
2 -- 1 -- second article

如果我查询 cat id = 1 的位置,它也应该返回来自 catid 2 的结果,因为 2 是 1 的子级

最佳答案

我会尝试使用这样的模型:

$p = $this->input->post('category');
$query = $this->db
->select('catid')
->where('parent',$p)
->or_where('catid',$p)
->get('categories_table');

$data = array();
if($query->num_rows())
{
foreach($query->result() as $row)
{
$res = $this->db
->where('catid',$row->catid)
->where('status',1)
->order_by('time_added', 'desc')
->get('articles_table');
foreach ($res->result() as $article_data)
{
$data[] = $article_data;
}
}
}
return $data;

代码首先检查类别表中 $p 等于 catidparent 的类别。然后检查与类别具有相同 catid 的文章,并将所有文章添加到数组 $data 中。

关于php - 一个表的 sql 结果与另一个表(包括子字段)的字段相匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35853854/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com