Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
显示';无数据';或者当mysql表、php、html中没有数据时为空_Php_Html_Mysql_Codeigniter - Fatal编程技术网

显示';无数据';或者当mysql表、php、html中没有数据时为空

显示';无数据';或者当mysql表、php、html中没有数据时为空,php,html,mysql,codeigniter,Php,Html,Mysql,Codeigniter,我的问题与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['us

我的问题与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)

当没有行可显示时,如何显示无数据或空白


感谢高级,请稍微修改您的模型功能

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;
}
希望这有助于…

一个更干净的解决方案(对@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的经验,如果行为空,则可能是
->row()
函数返回空对象。如果是这种情况,那么我所做的修改将不起作用,因为它仍然是一个计算结果为true的值


祝你好运

希望这能给你一些想法:

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();
    }

在您看来,只需使用:

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

你能给我们看看你的模型吗?@Craig,添加了查看最新新闻()的模型,
if(isset($latestnews->news\u content))
?我认为
如果($latestnews)
也可以,如果没有找到记录,IIRC CI将返回null,但已经有一段时间没有使用它了
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();
    }
if (is_object($latestnews)) {
echo $latestnews->newscontent;
}