我一直在尝试回显产品上的用户信息,但它不起作用。我曾在我的模型中尝试使用此连接函数:
public function getdata()
{
$this->db->select('*');
$this->db->from('users');
$this->db->join('products','products.user_id = users.user_id');
$query = $this->db->get();
if($query->num_rows()>0)
{
return $query->result_array();
}
}
它无法正常工作,因为它会回显数据库中的所有用户名,例如,当我尝试回显属于同一用户id的用户名时。
现在我试着这样做:
这是我的控制器功能(AlleCadeausController):
public function index()
{
/** Laad de models **/
$this->load->model('Product_model');
$selectProducts = $this->Product_model->selectProducts();
foreach ($selectProducts as $key => $product)
{
# first name
$user = $this->Product_model->get_user_name($product['user_id']);
#append both NEW VALUES to same array
$data[$key]['voornaam'] = $user[0]['voornaam'];
}
/** Make $data **/
$data['products'] = $this->Product_model->selectProducts();
/** Laad de allecadeaus view en geef $data mee **/
$this->load->view('allecadeaus', $data);
}
我的模型函数在我的Prduct_model.php文件:
public function selectProducts()
{
$query= $this->db->query("SELECT * FROM products");
$result = $query->result_array();
return $result;
}
function get_user_name($name)
{
$query= $this->db->query("SELECT voornaam FROM users WHERE user_id = $name ");
$result = $query->result_array();
return $result;
}
这就是我试图在视图中的产品Foreach循环中呼应用户信息的方式:
<?php foreach($products as $product) : ?>
<a href="<?php echo base_url() ?>/Product/details/<?php echo $product["product_id"]; ?>">
<div class="main-products">
<img id="cadeaufoto" src="<?php echo base_url(); ?>upload/<?php echo $product["product_foto_thumb"]; ?>">
<div class="product_naam"><?php echo $product["product_naam"]; ?></div>
<div class="ophaal_plaats"><?php echo $product["ophaal_plaats"]; ?></div>
</div>
</a>
<div class="aangeboden_door">
<p>
<tr>
<a href="<?php echo base_url() . 'User/userdetails/'.$product['user_id'];?>">
<td><?php echo $product['voornaam'];?></td>
</tr>
</p>
</div>
<?php endforeach; ?>
在这一行:
<代码>
表产品和用户的数据库信息:
Table 1 : products:
product_id
product_naam
product_beschrijving
user_id
ophaal_plaats
product_foto
product_foto_thumb
date_created
date_updated
category_id
table 2 : users:
user_id
email
voornaam
achternaam
beschrijving
profiel_foto
您可以使用活动记录,而不是创建多个功能:
您的控制器:
public function index()
{
$data = array();
$this->load->model('product_model');
$data['products'] = $this->product_model->selectProducts();
$this->load->view('allecadeaus', $data);
}
您的观点:
您的观点:
<?php foreach($products as $product) : ?>
<a href="<?php echo base_url() ?>/Product/details/<?php echo $product["product_id"]; ?>">
<div class="main-products">
<img id="cadeaufoto" src="<?php echo base_url(); ?>upload/<?php echo $product["product_foto_thumb"]; ?>">
<div class="product_naam"><?php echo $product["product_naam"]; ?></div>
<div class="ophaal_plaats"><?php echo $product["ophaal_plaats"]; ?></div>
</div>
</a>
<div class="aangeboden_door">
<p>
<tr>
<?php
//Here is the active record query which is getting the 'voorname' and other data
$userarray = $this->db->get_where('users', array('user_id'=>$product["user_id"]))->row_array();
// you can print_r($userarray); for see the array you get
?>
<a href="<?php echo base_url() . 'User/userdetails/'.$product['user_id'];?>">
<td><?php echo $userarray['voornaam'];?></td>
</tr>
</p>
</div>
<?php endforeach; ?>