【问题标题】:How to show null data when data does not exist in database using codeigniter使用codeigniter在数据库中不存在数据时如何显示空数据
【发布时间】: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


【解决方案1】:

据我了解,如果值为空,您想打印 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 }
} ?>

【讨论】:

  • 请仔细查看我的代码,我将加入两个表。您的代码无法正常工作
  • 我知道你的加入表,我在模型中建议的代码是正确的。错误在哪里
  • 我的条件是当两个 id 相等时显示数据,或者当 id 不相等或不存在时不显示任何内容,但是您的代码打印我的数据库中可用的所有内容
  • 因为您在模型中删除了此代码,$this->db->select('*'); $this->db->from('ads AS A');// 我使用别名使连接更容易 $this->db->join('membership AS C', 'C.id = A.ad_id', '内'); $this->db->where('A.ad_id = C.ad_fk');在该代码之后插入我建议的代码
  • 尝试在你的 sql 中使用该语句来确定需要显示的结果
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-21
  • 2017-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-12
相关资源
最近更新 更多