【问题标题】:Show 'No Data' or blank when there are no data in mysql table, php, htmlmysql table, php, html中没有数据时显示'No Data'或空白
【发布时间】:2015-06-06 04:16:09
【问题描述】:

我的问题与mysql、php、html有关。 我有我的 MVC,

下面是我的控制器,

public function index(){
    $this->load->model('user/model_user');
    $this->load->model('admin/model_admin');
    $view_all['news_array'] = $this->model_user->view_news();
    $view_all['users_array'] = $this->model_admin->view_users();
    $view_all['latestnews'] = $this->model_user->view_latestnews();
    $view_all['newscomments'] = $this->model_user->view_newscomments($view_all['latestnews']->news_id);
    $view_all['newstags'] = $this->model_user->view_newstags($view_all['latestnews']->news_id);
    $this->load->view('user/view_home',$view_all);
}

以上所有数组数据都是通过我的模型获取的 下面是型号

function view_latestnews()
{
    $this->db->join('sc_users', 'sc_users.user_id = sc_news.news_postedby');
    $this->db->where('news_postedon = (SELECT max(news_postedon) FROM  sc_news)', NULL, FALSE);
    return $this->db->get('sc_news')->row();
}

下面是我的观点之一(html),

<?php echo $latestnews->news_content;?>

这在数据库中有数据时可以正常工作,意味着模型从数据库中获取一些行,

但是当没有数据时,我的视图页面会显示如下错误消息,

A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: user/view_home.php

Line Number: 115

当没有可显示的行时,我们如何不显示数据或空白?

非常感谢,

【问题讨论】:

  • 你能告诉我们你的模型吗?
  • @Craig,为 view_latestnews() 添加了模型,
  • if (isset($latestnews-&gt;news_content)) ?我认为if ($latestnews) 也可以,如果没有找到记录,IIRC CI 返回 null,但有一段时间没有使用它

标签: php html mysql codeigniter


【解决方案1】:

稍微修改你的模型函数

function view_latestnews()
{
  $this->db->join('sc_users', 'sc_users.user_id = sc_news.news_postedby');
  $this->db->where('news_postedon = (SELECT max(news_postedon) FROM  sc_news)', NULL, FALSE);
  $newsrow = $this->db->get('sc_news')->row();
  $nonews = new stdClass();
  $nonews->news_content = 'No news found';
  return ($newsrow) ? $newsrow : $nonews;
}

希望这会有所帮助...

【讨论】:

    【解决方案2】:

    在您看来,只需使用:

    if (is_object($latestnews)) {
    echo $latestnews->newscontent;
    }
    

    【讨论】:

      【解决方案3】:

      更清洁的解决方案(@Gopakumar Gopalan 的回答略有修改)

      将检查$newsrow 是否存在,如果存在则返回$newsrow,否则返回false,如下所示:

      function view_latestnews() {
          ...
          return $newsrow ? $newsrow : false;
      }
      

      如果新闻行为空,这将返回 false,从而为您提供一个可以使用的值。

      在显示最新消息的代码中:

      <?php
      if (!$newsrow) { //no news found
          echo '<div class="no-news">No news found!</div>'
      } else {
          //Do stuff here with news object.
      }
      ?>
      

      只需检查虚假值即可解决您的问题。 附:

      我没有使用 codeIgniter 的经验,如果行为空,可能是 -&gt;row() 函数返回一个空对象。如果是这种情况,那么我所做的修改将不起作用,因为它仍然是一个评估为 true 的值。

      祝你好运!

      【讨论】:

        【解决方案4】:

        希望这会给你一些想法:

        public function check_user($user_id=NULL)
            {
                $query = null; //emptying in case 
                $query = $this->db->get_where('api_users', array(//making selection
                    'id' => $user_id,
                ));
        
                $count = $query->num_rows(); //counting result from query
                 //when $count has rows then return your result otherwise 0 or any thing 
                if ($count === 0) {
                   return 0;
                }
                   return $query->row_array();
            }
        

        【讨论】:

          猜你喜欢
          • 2019-12-15
          • 1970-01-01
          • 2022-01-13
          • 2023-03-24
          • 2021-02-04
          • 2019-03-28
          • 1970-01-01
          • 2022-07-27
          • 1970-01-01
          相关资源
          最近更新 更多