php - Fatal error: Call to undefined method DB::get() -
i accessing db class in index.php.
checkout files:
db.php
<?php namespace blog\db; class db { /** * return db * @var object */ private $db; /** * results limit. * @var integer */ public $limit = 5; public function __construct($config){ $this->connect($config); } private function connect($config){ try{ if ( !class_exists('mongo')){ echo ("the mongodb pecl extension has not been installed or enabled"); return false; } $connection= new \mongoclient($config['connection_string'],array('username'=>$config['username'],'password'=>$config['password'])); return $this->db = $connection->selectdb($config['dbname']); }catch(exception $e) { return false; } } /** * 1 article id * @return array */ public function getbyid($id,$collection){ // convert strings of right length mongoid if (strlen($id) == 24){ $id = new \mongoid($id); } $table = $this->db->selectcollection($collection); $cursor = $table->find(array('_id' => $id)); $article = $cursor->getnext(); if (!$article ){ return false ; } return $article; } /** * data in collection , paginator * * @return multi array */ public function get($page,$collection){ $currentpage = $page; $articlesperpage = $this->limit; //number of article skip beginning $skip = ($currentpage - 1) * $articlesperpage; $table = $this->db->selectcollection($collection); $cursor = $table->find(); //total number of articles in database $totalarticles = $cursor->count(); //total number of pages display $totalpages = (int) ceil($totalarticles / $articlesperpage); $cursor->sort(array('saved_at' => -1))->skip($skip)->limit($articlesperpage); //$cursor = iterator_to_array($cursor); $data=array($currentpage,$totalpages,$cursor); return $data; } /** * create article * @return boolean */ public function create($collection,$article){ $table = $this->db->selectcollection($collection); return $result = $table->insert($article); } /** * delete article via id * @return boolean */ public function delete($id,$collection){ // convert strings of right length mongoid if (strlen($id) == 24){ $id = new \mongoid($id); } $table = $this->db->selectcollection($collection); $result = $table->remove(array('_id'=>$id)); if (!$id){ return false; } return $result; } /** * update article * @return boolean */ public function update($id,$collection,$article){ // convert strings of right length mongoid if (strlen($id) == 24){ $id = new \mongoid($id); } $table = $this->db->selectcollection($collection); $result = $table->update( array('_id' => new \mongoid($id)), array('$set' => $article) ); if (!$id){ return false; } return $result; } /** * create , update comment * @return boolean */ public function commentid($id,$collection,$comment){ $postcollection = $this->db->selectcollection($collection); $post = $postcollection->findone(array('_id' => new \mongoid($id))); if (isset($post['comments'])) { $comments = $post['comments']; }else{ $comments = array(); } array_push($comments, $comment); return $postcollection->update( array('_id' => new \mongoid($id)), array('$set' => array('comments' => $comments)) ); } public function receive($page,$collection){ $currentpage = $page; $articlesperpage = $this->limit; //number of article skip beginning $skip = ($currentpage - 1) * $articlesperpage; $table = $this->db->selectcollection($collection); $cursor = $table->find(); //total number of articles in database $totalarticles = $cursor->count(); //total number of pages display $totalpages = (int) ceil($totalarticles / $articlesperpage); $cursor->sort(array('saved_at' => -1))->skip($skip)->limit($articlesperpage); //$cursor = iterator_to_array($cursor); $data=array($currentpage,$totalpages,$cursor); return $data; } }
index.php:
<?php require_once 'auth.php'; include '../app1.php'; include '../config.php'; include '../db.php'; require_once '../vendor/markdown/markdown.inc.php'; use michelf\markdownextra, michelf\markdown; $url_action = (empty($_request['action'])) ? 'login' : $_request['action']; if (isset($url_action)) { $action = new auth; if (is_callable(array($action,$url_action))) { call_user_func(array($action,$url_action)); } else { echo 'function not exist, request terminated'; } } if (is_array($_session) &&$_session['username'] ==userauth) { $data = array(); $status = (empty($_get['status'])) ? 'dashboard':$_get['status']; switch ($status) { case 'create': if ($_server['request_method'] === 'post' ) { $article = array(); $article['title'] = $_post['title']; $article['html'] = markdown::defaulttransform($_post['content']); $article['content'] =$_post['content']; $article['saved_at'] = new mongodate(); if ( empty($article['title']) || empty($article['content']) ) { $data['status'] = 'please fill out both inputs.'; }else { // create new row in table $db->create('posts',$article); $data['status'] = 'row has been inserted.'; } } $layout->view('admin/create', $data); break; case 'edit': $id = $_request['id']; $data['status'] =null; if ($_server['request_method'] === 'post' ) { $article = array(); $article['title'] = $_post['title']; $article['html'] = markdown::defaulttransform($_post['content']); $article['content'] =$_post['content']; $article['saved_at'] = new mongodate(); if ( empty($article['title']) || empty($article['content']) ) { $data['status'] = 'please fill out both inputs.'; }else { // create new row in table $db->update($id,'posts',$article); $data['status'] = 'row has been update.'; } } $layout->view('admin/edit',array( 'article' => $db->getbyid($id,'posts'), 'status' => $data['status'] )); break; case 'delete': $id = $_get['id']; $status = $db->delete($id,'posts'); if ($status ==true ) { header("location:index"); } break; default: $currentpage = (isset($_get['page'])) ? (int) $_get['page'] : 1; //current page number $data = $db->get($currentpage,'posts'); $layout->view('admin/dashboard',array( 'currentpage' => $data[0], 'totalpages' => $data[1], 'cursor' => $data[2], )); break; } }
index.php inside folder i.e ../admin/index.php , db.php lies in root folder of web directory.
fatal error: call undefined method db::get() in c:\xampp\htdocs\admin\index.php on line 82.
error line: $data = $db->get($currentpage,'posts');
i can't see in index.php
create $db
object calling db constructor.
you need @ least @ beginning of code 1 line this:
$db = new db($config);
Comments
Post a Comment