My question is related to mysql, php, html. I have my MVC,
Below is my controller,
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);
}
All above array data are taken through my models below is model
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();
}
below is one of line from my view(html),
<?php echo $latestnews->news_content;?>
This works expected when there are data in database, means model get some rows from DB,
But when there are no data then my view pages shows error message like below,
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: user/view_home.php
Line Number: 115
How can we show no data or blank when there are no rows available to show?
Thanks advanced,
Slightly modify your model function
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;
}
Hope this helps...
A cleaner solution (with slight mod on @Gopakumar Gopalan's answer)
Would be to check if the $newsrow
exists, if so return $newsrow
otherwise return false
like so:
function view_latestnews() {
...
return $newsrow ? $newsrow : false;
}
This will return false
if the newsrow is empty thus giving you a value to work with.
In your code showing the latest news:
<?php
if (!$newsrow) { //no news found
echo '<div class="no-news">No news found!</div>'
} else {
//Do stuff here with news object.
}
?>
Simply checking for a falsy value solves your issue. P.S.
I have no experience using codeIgniter, it could be that the ->row()
function returns an empty object if the row is empty. If this is the case then the modification I made will not work as it will still be a value that evaluates to true.
Good luck!
hope this will give you some idea :
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();
}
In your view, simply use:
if (is_object($latestnews)) {
echo $latestnews->newscontent;
}