Sending a function to library in Codeigniter - php

This works in my controller.
$this->load->driver('cache');
//working
// $data['advisory']=$advisory = $this->cache->memcached->get('advisory');
// if(! $advisory)
// {
// $data['advisory']=$advisory = $this->Mhomework->getbyadvisory($this->teacherid);
// $this->cache->memcached->save('advisory' , $advisory);
// }
I made a library to make for this as following so that I can use it for others.
function cache($key, $data)
{
$this->CI->load->driver('cache');
$cache = $this->CI->cache->memcached->get($key);
if (!$cache) {
// There's been a miss, so run our data function and store it
$cache = $data($CI);
//$cache = $data;
$this->CI->cache->memcached->save($key, $cache);
}
return $cache;
}
And in controller I changed to the following which gives an error.
$data['advisory'] = $this->hwtracker->cache('advisory.'.$this->teacherid, function(&$CI){
return $CI->Mhomework->getbyadvisory($this->teacherid);
});
// error Call to a member function getbyadvisory() on a non-object in ... controller
// Message: Trying to get property of non-object
My question is how I can send function to my library in CodeIgniter? Or Can I?
Update:    
function getbyadvisory($id){
  $Q="SELECT *, studentid, COUNT(studentid),be_user_profiles.first_name,   
       be_user_profiles.last_name
FROM be_user_profiles
LEFT JOIN hw_homework
ON be_user_profiles.user_id= hw_homework.studentid
WHERE be_user_profiles.advisor = $id
GROUP BY be_user_profiles.user_id
ORDER BY COUNT(studentid) DESC";
$query = $this->db->query($Q);
if ($query->num_rows() > 0)
{
foreach ($query->result_array() as $row)
{
$data[] = $row;
}
}
else
{
$data = FALSE;
}
$query->free_result();
return $data;
}

Please change your library to be like this
function cache($key, $data)
{
$this->CI->load->driver('cache');
//bind your key here
$key_details = $data.$key;
$cache = $this->CI->cache->memcached->get($key_details);
if (!$cache) {
// There's been a miss, so run our data function and store it
$cache = $this->$data($key);
//$cache = $data;
$this->CI->cache->memcached->save($key, $cache);
}
return $cache;
}
//new advisory function in library
function advisory($teacherid){
return $this->CI->Mhomework->getbyadvisory($teacherid);
}
And in controller call like this
$data['advisory'] = $this->hwtracker->cache($this->teacherid,'advisory');

Related

PHP object is not being created

I have received the following error:"Fatal error: Call to a member function session_type() on a non-object".
Supposedly I'm not creating an object in the variable "$tabsessiontype2". I don't see the reason why the object isn't created. Thanks for you help.
Here is my TeacherController.php
if(isset($_POST['search']) && $_POST['sessiontype_name']){
$tabsessiontype2=Db::getInstance()->select_session_type2($_POST['sessiontype_name']);
if($tabsessiontype2->session_type()=='X'){
$view='teacher.php';
}
elseif($tabsessiontype2->session_type()=='XO'){
$view='teacherxo.php';
}
elseif($tabsessiontype2->session_type()=='note'){
$view='teachernote.php';
}
}
This is the function I call:
public function select_session_type2($name_session_type) {
$query = "SELECT * FROM session_types WHERE session_types.name_session_type='$name_session_type'";
$result = $this->_db->query($query);
$tableau = array();
if ($result->rowcount()!=0) {
while ($row = $result->fetch()) {
$tableau[] = new Sessiontype($row->code_session_type,$row->name_session_type,$row->quarter_session_type,$row->code_lesson,$row->session_type);
}
}
return $tableau;
}
Here is the SessionType class:
class SessionType{
private $_code_session_type;
private $_name_session_type;
private $_quarter_session_type;
private $_code_lesson;
private $_session_type;
public function __construct($code_session_type,$name_session_type, $quarter_session_type, $code_lesson,$session_type){
$this->_code_session_type = $code_session_type;
$this->_name_session_type = $name_session_type;
$this->_quarter_session_type = $quarter_session_type;
$this->_code_lesson = $code_lesson;
$this->_session_type = $session_type;
}
public function code_session_type(){
return $this->_code_session_type;
}
public function name_session_type(){
return $this->_name_session_type;
}
public function quarter_session_type(){
return $this->_lastname;
}
public function code_lesson(){
return $this->_email_student;
}
public function session_type(){
return $this->_session_type;
}
}
It looks like the function select_session_type2 is returning an array:
$tableau = array();
if ($result->rowcount()!=0) {
while ($row = $result->fetch()) {
$tableau[] = new Sessiontype($row->code_session_type,$row->name_session_type,$row->quarter_session_type,$row->code_lesson,$row->session_type);
}
}
return $tableau;
and you are expecting an object:
$tabsessiontype2=Db::getInstance()->select_session_type2($_POST['sessiontype_name']);
if($tabsessiontype2->session_type()=='X'){
$view='teacher.php';
}

return multiple values inside function method in codeigniter

This is my query counting rows in tblapplication
public function countallrecord() {
$query = $this->db->get('tblapplication');
return $query->num_rows();
}
and this is the function to get all the data
public function getdata() {
$query = $this->db->get('tblapplication');
return $query->result();
}
Is there any way I can make this code on one function
I'm trying to pass it here:
public function Countandviewallrecord() {
// returns both rows and count
}
Just return it as an array. Include the results and count in their respective indices:
public function get_records()
{
$result = $this->db->get('tblapplication');
$data['results'] = $result->result();
$data['count'] = $result->num_rows;
return $data;
}
When accessing the model method in your controller, the usual:
$data = $this->model_name->get_records();
echo $data['count']; // whatever number this is
if($data['count'] > 0) {
foreach($data['results'] as $row) {
echo $row->column_name; // etc blah blah ..
}
}
this Countandviewallrecord will display data as well count total records
public function Countandviewallrecord(){
$TotalRecords= $this->countallrecord();
$totalData= $this->getdata();
}

Load specific data from array

So i have this class for loading data from my MySQL database :
class Db {
protected static $connection;
public function connect() {
if(!isset(static::$connection)) {
$config = parse_ini_file('config.ini');
static::$connection = new mysqli('localhost',$config['username'],$config['password'],$config['dbname']);
}
if(static::$connection === false) {
return false;
}
return static::$connection;
}
public function query($query) {
return $this->connect()->query($query);
}
public function select($query) {
$rows = array();
$result = $this->query($query);
if($result === false) {
return false;
}
while ($row = $result->fetch_assoc()) {
$rows[] = $row;
}
return $rows;
}
public function error() {
return $this->connect()->error;
}
public function quote($value) {
return "'" . $this->connect()->real_escape_string($value) . "'";
}
}
I use that class like this :
$db = new Db();
$rows = $db -> select("SELECT name, shortlink FROM `test` WHERE id=3");
It gives me an array with the data.
The problem is that i want to pull out specific data, for example the shortlink field.
How do i do that? I have tried echo $rows['shortlink'], but that gives me the following error :
Undefined index: shortlink
So how do I print specific data?
Your returned $rows columns is an array of associative arrays, to pull out shortlink data returned from the query you have to do something like this:
foreach($rows as $row) {
echo $row['shortlink'];
}

multiple delete with checkbox in ci

I know it has already been asked, but I'm not able to solve this. Below is my control module
public function ahome()
{
$this->load->model('model');
$data['user'] = $this->model->selall('user');
$this->load->view('ahome', $data);
if ($this->input->post('del'))
{
$did = $this->input->post('chk');
// print_r($did);
for ($i = 0; $i < count($did); $i++)
{
$multi = $did[$i];
print_r($multi);
$this->model->delall("user", $multi);
// redirect("control/ahome");
}
}
}
and this is my model module
public function delall($tb,$wh)
{
$this->db->query("delete from $tb where uid in('$wh')");
}
now problem is that it's only delete single row not multiple rows
In your query string, try removing the quotes around the $wh variable.
public function delall($tb,$wh)
{
$this->db->query("delete from $tb where uid in ($wh)");
}
That should get it to work; however, you should be using CodeIgniter's query bindings. Something like this:
public function delall($tb,$wh)
{
$this->db->query("delete from $tb where uid in (?)", array($wh));
}
Refer to this answer for more details.
Try this,
controller,
public function ahome()
{
$this->load->model('model');
$data['user'] = $this->model->selall('user');
$this->load->view('ahome', $data);
if ($this->input->post('del'))
{
$did = $this->input->post('chk');
// print_r($did);
foreach ($did as $d_id)
{
$this->model->delall("user", $d_id);
// redirect("control/ahome");
}
}
}
Model:
public function delall($tb,$wh)
{
$this->db->where('id',$wh);
$this->db->delete($tb);
return true;
}

Codeigniter: Call to a member function result_array() on a non-object

I'm using Codeigniter to build a webapp and I received this error:
Fatal error: Call to a member function result_array() on a
non-object in /var/www/application/controllers/people.php on line 29
This is the people.php class:
public function __construct()
{
parent::__construct();
}
function People() {
}
function view($id) {
echo "Hello world " . $id;
}
function all() {
$this->load->model('people_model', '', TRUE);
$allContacts = $this->people_model->get_all_contacts();
$results = array();
foreach ($allContacts->result_array() as $row) {
$eachrow = array("personID"=>$row['personID'],"fName"=>$row['fName'],"lName"=>$row['lName'],"phoneNumber"=>"",
"emailAddress"=>$row['emailAddress'],"address"=>$row['address'],"city"=>$row['city'],"state"=>$row['state'],"zip"=>$row['zip']);
$results = push_array($results, $eachrow);
}
foreach ($results as $row) {
$phoneData = $this->people_model->get_phone_by_id($row['personID']);
$phoneNumbers = "";
foreach ($phoneData->result_array() as $row2) {
$phoneNumbers = $row2['type'] . " " . $row2['phoneNumber'] . " " . $row2['extension'];
}
$results['phoneNumber'] = $phoneNumbers;
}
$data['title']="Page Title Goes Here";
$data['username']="Your Username";
$this->load->view('header', $data);
$this->load->view('people_results', $results);
$this->load->view('footer');
}
}
Here is the people_model class:
function __construct()
{
// Call the Model constructor
parent::__construct();
}
function get_all_contacts() {
$query = $this->db->query('SELECT * FROM person');
return $query->result();
}
function get_phone_by_id($id) {
$query = $this->db->query('SELECT * FROM phoneNumber WHERE personID = '.$id);
return $query->result();
}
}
The only thing I might question in database.php is if the hostname needs a port number, but I don't think that helped when I tried it.
I just tried adding (based on similar question in side bar)
$this->mydb = $this->load->database('realDB', TRUE);
to the people_model and changing the db to mydb and received this error:
You have specified an invalid database connection group.
and this is my line in database.php:
$db['default']['database'] = 'realDB';
Thanks for all your help.
since get_all_contacts is already using the result() function in your model, you can't also use the result_array() function in your controller. result_array() would be a method of the $query object. The quick and dirty way to get this working(which might break other stuff if its also using the get_all_contacts method) would be to change your get all contacts function to the following:
function get_all_contacts() {
$query = $this->db->query('SELECT * FROM person');
return $query;
}
however, if you want to be smarter about it and not risk breaking other stuff, you can pass a param from the controller, and only return query if its set like so:
REVISED CONTROLLER LINE**
$allContacts = $this->people_model->get_all_contacts(true);
REVISED MODEL CODE
function get_all_contacts($special = false) {
$query = $this->db->query('SELECT * FROM person');
if($special)
{
return $query;
}
return $query->result();
}

Categories