Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Codeigniter循环连接多个结果的正确方法?_Php_Mysql_Codeigniter - Fatal编程技术网

Php Codeigniter循环连接多个结果的正确方法?

Php Codeigniter循环连接多个结果的正确方法?,php,mysql,codeigniter,Php,Mysql,Codeigniter,我将举一个小例子来解释我的意思 CREATE TABLE blog ( b_id int primary key not null, b_title varchar(255) not null, b_body text not null, CONSTRAINT pk_b_id_01 PRIMARY KEY(b_id) ) CREATE TABLE images ( i_id int primary key not null, i_image varchar(255)

我将举一个小例子来解释我的意思

CREATE TABLE blog
(
 b_id int primary key not null,
  b_title varchar(255) not null,
  b_body text not null,
  CONSTRAINT pk_b_id_01 PRIMARY KEY(b_id)
 )

 CREATE TABLE images
 (
  i_id int primary key not null,
  i_image varchar(255) not null,
  i_b_id int not null,
  CONSTRAINT pk_i_id_01 PRIMARY KEY(i_id)
 )
假设在一个管理区域,我想显示一个表,其中包含博客的每个主题以及分配给它的所有图像,无论是1个图像还是5个图像

现在我是这样做的

$query = $this->db->get("blog");
foreach($query->result() as $qr)
{
    echo $qr->b_title . "<br />";
    $query2 = $this->db->get_where("images", array('i_b_id'=>$qr->b_id));
    foreach($query2->result() as $qr2)
    {
        echo $qr2->i_image . "<br />";
    }
    echo "<hr />\n";
}
$query=$this->db->get(“blog”);
foreach($query->result()作为$qr)
{
回声$qr->b_标题。“
”; $query2=$this->db->get_-where(“图像”,数组('i_-b_-id'=>$qr->b_-id)); foreach($query2->result()作为$qr2) { echo$qr2->i_图像。“
”; } 回声“
\n”; }
我的问题是,有没有一种方法可以通过一个查询来实现这一点,而不是可能的30个或多少个帖子

我试图通过连接来实现这一点,但它只显示了第一个图像

$this->db->join("images", "i_b_id = b_id");
$query = $this->db->get("blog");
foreach($query->result() as $qr)
{
    echo $qr->b_title . "<br />";
    $query2 = $this->db->get_where("images", array('i_b_id'=>$qr->b_id));

    //is there another for loop i can do here?
    echo $qr->i_image . "<br />";

    echo "<hr />\n";
}
$this->db->join(“图像”,“i_b_id=b_id”);
$query=$this->db->get(“blog”);
foreach($query->result()作为$qr)
{
回声$qr->b_标题。“
”; $query2=$this->db->get_-where(“图像”,数组('i_-b_-id'=>$qr->b_-id)); //这里还有其他的for循环吗? echo$qr->i_图像。“
”; 回声“
\n”; }
进行连接会导致每个图像的博客数据重复。为了避免重复显示相同的标题,只需使用一些简单的逻辑:

$current_blog_id = -1;
foreach($query->result() as $qr)
{
    // only output the title if it's a new blog
    if ($qr->b_id != $current_blog_id)
    {
        if ($current_blog_id != -1) echo "<hr />\n";

        echo $qr->b_title . "<br />";

        $current_blog_id = $qr->b_id;
    }

    echo $qr->i_image . "<br />";
}
$current\u blog\u id=-1;
foreach($query->result()作为$qr)
{
//仅当是新博客时才输出标题
如果($qr->b_id!=$current_blog_id)
{
如果($current\u blog\u id!=-1)回显“
\n”; 回声$qr->b_标题。“
”; $current_blog_id=$qr->b_id; } echo$qr->i_图像。“
”; }