Missing argument 1 for search_model::autocomplete(), - php

This is the controller search.php
public function login()
{
$email=$this->input->post('email');
$password=md5($this->input->post('password'));
$result=$this->search_model->login($email,$password);
if(!$result) {
$this->index();
}
else {
$this->session->set_userdata('user_id',$email);
$seid=$this->session->userdata('user_id');
$data['result'] =$this->search_model->autocomplete();
$this->load->view('pagination_view',array('result' => $result));
}
}
===========================Model=========================================
public function autocomplete($like) {
$this->db->select('name');
$this->db->from('tbl_reg');
$this->db->like('name', $like);
// $this->db->where('status', ACTIVE);
$q = $this->db->get();
if ($q->num_rows() > 0) {
return $q->result();
}
}
in controller search.php what argument is added to the autocomplete(). the code is
$data['result'] = $this->search_model->autocomplete();
I got an error like this Severity: Warning
Message: Missing argument 1 for search_model::autocomplete(),
and another error message like
Undefined variable: like
Whats the solution for this problem?

You need to pass argument in your model function
$this->search_model->autocomplete($YOUR_LIKE_VARIABLE);// pass your like variable here
Because in model file you use
public function autocomplete($like) {

you are supposed to pass the like variable as argument to your model function
In your controllers
$like = "matching word"; // replace this with your value;
$data['result'] =$this->search_model->autocomplete($like);

If you not passing $like variable then How your model will search.
$like='any text' // this would be your keyword for which you are doing auto complete process, Like any name or anything.
$data['result'] = $this->search_model->autocomplete($like);

Related

Facing an error after getting login: Fatal error: Call to a member function result() on a non-object in CodeIgniter

I am facing a problem while fetching data after getting logged in. Please have a look at my code. I am a fresher in CodeIgniter so please help me.
I get this error:
Fatal error: Call to a member function result() on a non-object in
CodeIgniter
This is my Controller:
public function login_function()
{
$this->load->database();
// Load the model
$this->load->model('login_model');
// Load the method of model
$records = $this->login_model->can_login();
// Return the data in view
if ($records === false)
{
$this->session->set_flashdata("message", "invalid user");
redirect('Main_controller', 'refresh');
} else {
$this->session->set_userdata('id', $records['id']);
$data['f'] = $records;
$this->load->view('user-home', $data);
}
}
This is my Model:
function can_login()
{
$email=$this->input->post('email');
$password=$this->input->post('password');
// Data is retrieve from this query
$this->db->select('*');
$this->db->where('email', $email);
$this->db->where('password', $password);
$query1 = $this->db->get('registration');
$result = $query1->result();
if (!empty($result)){
return $query1->row_array();
} else {
return false;
}
}
And this is my View:
<?php foreach ($f->result() as $row) { ?>
<h1><?php echo $row->first_name; ?></h1><br /><br />
<?php } ?>
Your error comes from your View. You call the result() method on $f which is the result of your can_login() function.
$f takes two values (according to your model):
false if no result;
an array (row_array()) if results are found.
The problem is that you call the result() method on a row_array() method (something like $query1->row_array()->result()), which is wrong.
Solution: You should return your $result variable in your model. Then, in your view, you can use this code to display your results:
foreach ($f as $row) // $f = $query->result() as you saved it in your model
{
echo $row->first_name;
}
there is no need to use code below in your model
if(!empty($result)){
return $query1->row_array();
} else{
return false;
}
just try return $query->result_array; or return $query->result;
it will return false if there is no result!

Getting error messages when trying to display images from a database while using codeigniter

I am trying do display information from my database using codeigniter, but I keep reeving an error message. The error messages is Severity: Warning Message: Invalid argument supplied for foreach().
This is my model:
class stuffModel extends CI_Model {
private $stuff_table = 'Stuff';
function __construct()
{
parent::__construct();
$this->load->database();
}
public function getItem()
{
$un = array();
$res = $this->db->get($this->stuff_table);
$results = $res->result();
return $results;
}
}
And this is my Controller:
public function displayStuff() {
$STUFF= $this->stuffModel->getItem();
$this->load->view('myView',array('stuff' => $STUFF));
}
And this is my View:
<?php
foreach ($stuff as $s) {
echo"<h1>".$s['Items']."</h1>";
}?>
And the error message is Severity: Notice Message: Undefined variable: stuff and Severity: Warning Message: Invalid argument supplied for foreach()
Nothing is jumping out at me and it looks like it should work. Here's a little trouble shooting idea.
Change the controller as follows
public function displayStuff()
{
$data['stuff'] = $this->stuffModel->getItem();
var_dump($data); //share this output here
$this->load->view('myView', $data);
}
You did not load the model into controller that's why you can't call the model from the controller .
first load the model into controller
$this-load->model('modelName');
than call
$this->modelName->methodName();
Changes in Controller
Make sure that the object of model class is declared in the constructor as given below
$this->load->model('stuffModel', 'stuffModel', TRUE);
then in the displayStuff function,
public function displayStuff() {
$data['stuff']= $this->stuffModel->getItem();
$this->load->view('myView',$data);
}
Change in the model
public function getItem()
{
$res = $this->db->get($this->stuff_table);
if ($res->num_rows() > 0) {
return $res->result_array();
} else {
return array();
}
}
Finally in the view
You can display the values stored in the array by iterating as follows
foreach($stuff as $stf){
echo $stf['field_name'];
}

Invalid argument supplied Codeigniter

My program is not working properly, i do not know what should i do :S
I got this error message:
Take a look at this:
Here is my code:
My controller file (Home):
<?php
class Home extends CI_Controller{
public function __construct(){
parent::__construct();
$this->load->model("Crudmodel");
}
public function index(){
# get all data in Study table
$selectStudys = $this->Crudmodel->selectStudys();
foreach ($selectStudys as $key => $study)
{
# get UserNames
$user = $this->Crudmodel->getName($study['user_id']);
#get Subject Names
$subject = $this->Crudmodel->getSubName($study['subject_id']);
#append both NEW VALUES to same array
if(!empty($user[0]['username'])){
$data[$key]['user_id'] = $user[0]['username'];
// your main problem can be this. may be it is not getting value from query this is why we have put validation on model function and error handler condition here
}else{
$data[$key]['user_id'] = ''; // or anything as your else condition you can use as error handler
}
if(!empty($subject[0]['name'])){
$data[$key]['subject_id'] = $subject[0]['name'];
// your main problem can be this. may be it is not getting value from query this is why we have put validation on model function and error handler condition here
}else{
$data[$key]["subject_id"] = "";
// or anything you can use as error handler
}
}
$data['records'] = $selectStudys;
$this->load->view('home', $data);
}
}
?>
Crudmodel:
class Crudmodel extends CI_Model{
public function __construct(){
parent::__construct();
$this->load->database();
}
function selectStudys()
{
$query= $this->db->query("SELECT * FROM cursadas");
if($query->num_rows()>0){
$result = $query->result_array();
}else{
$result = "";
// or anything you can use as error handler
return $result;
}
}
function getName($name)
{
$query= $this->db->query("SELECT username FROM usuarios WHERE id = $name ");
if($query->num_rows()>0){
$result = $query->result_array();
}else{
$result = "";
// or anything you can use as error handler
return $result;
}
}
Dont know what to do now :(
Hope you can help me :S
The problem is in the model. You only return something inside the else. Easy fix, move the return.
You should probably return an empty array if there are no rows. Then the foreach will still have something to work with - even if it is empty. foreach will choke if given something that cannot be used in a loop - a string for instance.
function selectStudys()
{
$query= $this->db->query("SELECT * FROM cursadas");
if($query->num_rows()>0){
$result = $query->result_array();
}else{
$result = array();
}
return $result;
}

Call to a member function num_rows() on boolean in PHP

I am getting the below error on my code,
Call to a member function num_rows() on boolean in C:\xampp\htdocs\codeigniter\application\controllers\go.php on line 15
Code:
<?php if (!defined('BASEPATH')) {
exit('No direct script access
allowed');
}
class Go extends MY_Controller
{
function __construct()
{
parent::__construct();
$this->load->helper('string');
}
public function index()
{
if (!$this->uri->segment(1)) {
redirect(base_url());
} else {
$url_code = $this->uri->segment(1);
$this->load->model('Urls_model');
$query = $this->Urls_model->fetch_url($url_code);
if ($query->num_rows() == 1) {
foreach ($query->result() as $row) {
$url_address = $row->url_address;
}
redirect(prep_url($url_address));
} else {
$page_data = array(
'success_fail' => NULL,
'encoded_url' => FALSE,
);
$this->load->view('common/header');
$this->load->view('nav/top_nav');
$this->load->view('create/create', $page_data);
$this->load->view('common/footer');
}
}
}
}
#ahmed is correct that db code belongs in the model but the problem you're having is most likely from a db error.
Besides fixing the problem, the workaround is this.
if ($query->num_rows() == 1) {
should be changed to
if ($query && $query->num_rows() == 1) {
I would put an else in there to dump the $this-db->error() to the error_log so you can see what the problem is.
You should call the 'num_rows()' inside the Model, not the Controller, for this function is used often after the get() method:
//Model function:
$query = $this->db->get();
$data = Array();
if($query->num_rows()){
//Work with data:
foreach($query->result_array() AS $row){
$data[] = $row;
}
}
return $data;
I suppose if you want to use the num_rows inside the controller, you should return the query AS a result like this:
$query = $this->db->get();
return $query;
$data = [];
$this->db->where('', 0);
$query = $this->db->get('');
if ($query->num_rows()>0)
{
foreach ($query->result_array() as $row)
{
$data[] = $row;
}
}
$query->free_result();
return $data;
First of all check that values come from database. if values come from table then put greater than 0 . if your value exist then query run forword. i thank this is help for you
This is caused by buggy Active Records in CI that adds an extra WHERE clause causing ambiguous column names and similar.
Try to get the actual query with $this->db->last_query(); and it will be instantly clear why $query is not an object.

Object of Class could not be converted to string CI Error

I am getting an error in the bellow code and I am unable to find out what is the reason for it. I am glad if you can help me with this.
Public function () {
$query = $this->db->select_sum("cost_after_discount");
$query = $this->db->get('tbl_payment');
$data = $query;
return $data;
}
Object of Class could not be converted to string CI Error
There are several errors in your code. First you have to provide a name for your function then you do not use any result method to return result. So try following
Public function xyz() {
$this->db->select_sum("cost_after_discount");
$query = $this->db->get('tbl_payment');
$data = $query->result_array();
return $data;
}
now you may call this xyz function to retrieve data
Try this
public function retrive()
{
$this->db->select_sum("cost_after_discount");
$query = $this->db->get('tbl_payment');
$data = $query->result_array();
var_dump($data);
}
This should do :)

Categories