从一个数据中获取循环数据

get looping data from one data

我正在使用 codeigniter。

我在模型中有两个函数。

在第一个函数中,我从一个 table 中获取所有特定行。这是我想要获取所有产品详细信息的地方。

function getProduct() 
    {
        $shoes = 'SELECT pd.name,
                         pd.id, 
                         pd.price

                  FROM product_detail pd
                  LEFT JOIN product_sub_category psc
                  ON pd.sub_category_id = psc.id
                  WHERE psc.name = "Shoes"';

        $categoried = $this->db->query($shoes);
        return $categoried->result();
    }

在第二个函数中,我根据第一个函数中的一行获取所有特定行。这是我想要获取每个产品的所有图像的地方。

function getImage()
    {
        $shoes = 'SELECT pim.file_name                      
                  FROM product_img pim
                  LEFT JOIN product_detail pd
                  ON pim.product_detail_id = pd.id
                  WHERE pim.product_detail_id = "'.$row->id.'"';

        $categoried = $this->db->query($shoes);
        return $categoried->result();

    }

但它给了我一些错误。

有人可以帮助我吗?感谢您的帮助。



[更新]

这是我的控制器

在控制器中,我识别records获取产品,识别recorded获取图像。

function index() 
    {
        $this->load->model('site_model'); 

        $data = array();
        $query = $this->site_model->getProduct();
        $data['records'] = $query;

        foreach ($query as $row) 
            {
                $id = $row->id; // get ID
                $name = $row->name; // get ID
                $product_image = $this->site_model->getImage($id); // invoke the second method feeding that ID
                $data['recorded'] = $product_image;
            }
        $this->load->view('mothershop', $data);

    } 

这是我的模特

function getProduct() 
    {
        $shoes = 'SELECT pd.name,
                         pd.id, 
                         pd.price

                  FROM product_detail pd
                  LEFT JOIN product_sub_category psc
                  ON pd.sub_category_id = psc.id
                  WHERE psc.name = "Shoes"';

        $categoried = $this->db->query($shoes);
        return $categoried->result();
    }


function getImage($id)
    {
        $shoes = 'SELECT pim.file_name                      
                  FROM product_img pim
                  LEFT JOIN product_detail pd
                  ON pim.product_detail_id = pd.id
                  WHERE pim.product_detail_id = "'.$id.'"';

        $categoried = $this->db->query($shoes);
        return $categoried->result();

    }

这是我对mothershop.php

的看法

在本节中,我调用 recordsrecorded

foreach ($records as $row) 
    {
        echo "<div>".$row->name."</br>";
            foreach ($recorded as $rowed)
                {
                    echo "<img src='".base_url()."upload/thumbs/".$rowed->file_name."'/>";
                }
        echo "</div>"; 
    } 

但是,我不知道为什么,它只会为每个产品获得相同的图像。

但是如果我在控制器中打印 $product_image,它会为每个产品提供不同的图像。

这里有什么问题?

提前感谢@Ghost

由于 $row->id 仅取决于第一种获取方法,目前,它超出了范围。为了从第一个方法中使用该 ID,只需在第二个方法上添加一个参数并将其用于第二个查询。更多类似内容:

function getImage($id)

那你就可以用在第二种方法上了

因为您没有显示任何关于如何使用您的方法的代码。考虑这个例子:

第一种方法用法(Controller):

$this->load->model('Super_model');
$products = $this->Super_model->getProduct();
foreach($products as &$row) {
    $id = $row->id; // get ID
    $product_image = $this->Super_model->getImage($id); // invoke the second method feeding that ID
    if(!empty($product_image)) { // if it exists
        $row->file_name = $product_image->file_name; // add another property which is file_name on that first fetched data
    }
}

模型方法:

function getImage($id)
{
    $shoes = "SELECT pim.file_name                      
              FROM product_img pim
              LEFT JOIN product_detail pd
              ON pim.product_detail_id = pd.id
              WHERE pim.product_detail_id = '$id' ";

    $categoried = $this->db->query($shoes);
    return ($categoried->num_rows() > 0) ? $categoried->row() : null;   
}

这是它的基本思想,我们不知道发生了什么,也不知道你如何将其他所有东西粘在控制器上,如何调整它以适应你的代码库。