如何与CodeIgniter一起显示两个表


How to show two tables together with CodeIgniter?

如何与CodeIgniter一起显示两个表?

我试了很多次,但我做不到。我试着用谷歌和YouTube搜索,但我不能。我该怎么做呢?

我有两个表:

表1:

id  image  title
1   1.jpg  pic1
2   2.jpg  pic2
3   3.jpg  pic3
8   8.jpg  pic8
表2:

id2  image2  title2
4   p1.jpg  img1
5   p2.jpg  img2
6   p3.jpg  img3

和我想要的结果:

- 1.jpg  pic1
- p1.jpg  img1
- 2.jpg  pic2
- p2.jpg  img2
- 3.jpg  pic3
- p3.jpg  img3
-8.jpg  pic8
$sql = "SELECT * FROM table1 INNER JOIN table2 ON(table2.image2 = CONCAT('p',table1.image))";
$query = $this->db->query($sql);
foreach ($query->result() as $row)
{
    echo $row->image . " ". $row->title . "<br>";
    echo $row->image2 . " ". $row->title2 . "<br>";
}

无论table1和table2中的表ID如何

function index()
{
    /**
     * Regardless of any IDs
     */
    # Table1
    $query1  = $this->db->select('image,title')->from('table1')->get();
    $obj1   = $query1->result();
    $offset=-1;
    foreach($obj1 as $row1):
        $offset++;
        $this->combine_with_table2($row1,$offset);
    endforeach;
}
function combine_with_table2($row1,$offset)
{
    $query2  = $this->db->select('image2,title2')->from('table2')->limit(1,$offset)->get();
    $obj2   = $query2->result();
    foreach($obj2 as $row2):
        $output_result = $row1->image.' '.$row1->title.'<br/>'
                        .$row2->image2.' '.$row2->title2.'<br/>';
        echo '<pre>';
        print_r($output_result);
        echo '</pre>';
    endforeach;
}

连接两个表:

$this->query("SELECT
                 `product_master`.`id`,
                 `product_master`.`prod_style`,
                 `product_master`.`prod_name`,
                 `product_attributes`.`id`,
                 `product_attributes`.`prod_style`,
                 `product_attributes`.` prod_attr`,
                 `product_attributes`.`prod_value`
             FROM
                 `product_attributes`,
                 `product_master`");