Not working- Passing data from Controller to View in a PHP MVC -
i'm watching tutorial on php, , have problems. everytime call variable in view throws error
notice: trying property of non-object in /opt/lampp/htdocs/talkingspace/topics.php on line 18
and
notice: undefined variable: totaltopics in /opt/lampp/htdocs/talkingspace/templates/topics.php on line 36
earlier instead of ,for example, $topic->userid, put $topic['userid'], not working anymore.
here template/topics.php:
<?php include('includes/header.php'); ?> <ul id="topics"> <?php if($topics) : ?> <?php foreach ($topics $topic) : ?> <li class="topic"> <div class="row"> <div class="col-md-2"> <img class="avatar pull-left" src="images/avatars/<?php echo $topic['avatar']; ?> " alt="img"> </div> <div class="col-md-10"> <div class="topic-content pull-right"> <h3><a href="topic.php"><?php echo $topic['title']; ?></a></h3> <div class="topic-info"> <a href="topics.php?category=<?php echo urlformat($topic['category_id']); ?>"><?php echo $topic['name']; ?></a> >> <a href="profile.php?user=<?php echo urlformat($topic['user_id']);?>"><?php echo $topic['username']; ?></a> <?php echo formatdate($topic['create_date']);?> <span class="badge pull-right"><?php echo replycount($topic['id']); ?></span> </div> </div> </div> </div> </li> <?php endforeach; ?> </ul> <?php else : ?> <p>no topics display</p> <?php endif; ?> <h3>forum statistics</h3> <ul> <li>total number of users: <strong>4</strong></li> <li>total number of topics: <strong><?php echo $totaltopics; ?></strong></li> <li>total number of categories: <strong><?php echo $totalcategories; ?></strong></li> </ul> <?php include('includes/footer.php'); ?>
here template class:
<?php /** * template class * creates template/view object */ class template { //path template protected $template; //variables passed in protected $vars = array(); /* * class constructor */ public function __construct($template){ $this->template = $template; } /* * template variables */ public function __get($key) { return $this->vars[$key]; } public function __set($key, $value) { $this->vars[$key] = $value; } public function __tostring() { extract($this->vars); chdir(dirname($this->template)); ob_start(); include basename($this->template); return ob_get_clean(); } } ?>
here topics.php:
<?php require('core/init.php'); ?> <?php $topic = new topic; //get category url $category = isset($_get['category']) ? $_get['category'] : null; //get template & assign url $template = new template('templates/topics.php'); //assign template variables if(isset($category)){ $template->topics = $topic->getbycategory($category); $template->title = 'posts in "'.$topic->getcategory($category)->name.'"'; } if(!isset($category)){ $template->topics = $topic->getalltopics(); } //assign variables $template->totaltopics = $topic->gettotaltopics(); $template->totalcategories = $topic->gettotalcategories(); //display template echo $template; ?>
and topic class here
<?php class topic{ //init db variable private $db; //constructor public function __construct() { $this->db = new database; } //get topics public function getalltopics() { $this->db->query("select topics.*, users.username, users.avatar, categories.name topics inner join users on topics.user_id = users.id inner join categories on topics.category_id = categories.id order create_date desc "); $results = $this->db->resultset(); return $results; } public function getbycategory($category_id){ $this->db->query("select topics.*, categories.*, users.username, users.avatar, categories.name topics inner join categories on topics.category_id = categories.id inner join users on topics.user_id = users.id topics.category_id = :category_id "); $this->db->bind(':category_id', $category_id); //assign result set $results = $this->db->resultset(); return $results; } public function getcategory($category_id){ $this->db->query("select * categories id = :category_id"); $this->db->bind(':category_id', $category_id); //assign row $row = $this->db->single(); return $row; } public function gettotaltopics(){ $this->db->query("select * topics"); $rows = $this->db->resultset(); return $this->db->rowcount(); } public function gettotalcategories(){ $this->db->query('select * categories'); $rows = $this->db->resultset(); return $this->db->rowcount(); } public function gettotalreplies($topic_id){ $this->db->query('select * replies topic_id = '.$topic_id); $rows = $this->db->resultset(); return $this->db->rowcount(); } } ?>
what i'm doing wrong?
Comments
Post a Comment