我正在加入两个表,我希望在两者的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
在数据库中存在数据时显示空数据答案 0 :(得分:0)
据我所知,如果值为空,您想要打印null?所以这是代码。
在您的模型中尝试将此代码改为
$q = $this->db->get("ads");
if($q->num_rows() > 0)
{
return $q->result();
}
return false;
并在您的视图中
if(!$profile_records)
{
//the value is null
echo "The value is null";
}
else
{
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 }
} ?>