【发布时间】:2014-09-13 16:53:30
【问题描述】:
我正在加入两个表,并且我想在两者的 id 相等时显示数据。当 id 不相等时,什么都不显示,但它会显示所有内容。
这是我的控制器
<?php
/**
*
*/
class profile extends CI_Controller
{
function __construct()
{
parent::__construct();
}
function index()
{
$this->load->model("profile_model");
$data['profile_records']=$this->profile_model->getAllRecords();
}
}
?>
这是我的模特
<?php
/**
*
*/
class profile_model extends CI_Model
{
function __construct()
{
parent::__construct();
}
function getAllRecords()
{
$this->db->select('*');
$this->db->from('ads AS A');// I use aliasing make things joins easier
$this->db->join('membership AS C', 'C.id = A.ad_id', 'INNER');
$this->db->where('A.ad_id = C.ad_fk');
$q = $this->db->get("ads");
if($q->num_rows() > 0)
{
return $q->result();
}
return array();
}
}
?>
这是我的看法
<?php
foreach ($profile_records as $profile_rows)
{
?>
<span>
echo "<span class='title2'>". $profile_rows->ad_timestamp ."</span>"."<br/>";?>
<?php echo "<span class='title2'>". $profile_rows->ad_title ."</span>"."<br/>";?>
<?php echo "<span class='title'>". $profile_rows->ad_description ."</span>"."<br/>";?>
<?php echo "<span class='title2'>". $profile_rows->cat_type ."</span>"."<br/>";?>
<?php echo "<span class='title2'>". $profile_rows->ad_name ."</span>"."<br/>";?>
<?php echo "<span class='description'>". $profile_rows->ad_phone ."</span>"."<br/>";?>
<?php echo "<span class='name'>". $profile_rows->ad_address ."</span>"."<br/>";?>
<?php echo "<span class='phone'>". $profile_rows->ad_flag."</span>"."<br/>";?>
<?php //echo "<span class='website'>". $row->address ."</span>"."<br/>";?>
</span>
<hr width="600px" />
<?php }?>
如何使用codeigniter在数据库中不存在数据时显示空数据
【问题讨论】:
-
听起来像一个 MySQL
join问题,虽然我不明白实际问题,您能否包括一些您希望加入的表中的示例数据和所需的结果? -
请向我们展示您得到的结果,以便更好地帮助您,因为您在评论中附加的任何内容都不起作用。
-
@Muhammad,请将上面的图片复制到您的问题中 - 这些链接需要 GMail 身份验证,其他人无法看到它们。
标签: php mysql database codeigniter activerecord