This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I am working on a small application that displays data from a database table. I am getting 2 errors and dont know why are there, and I cant figure it out as I am still a noob. I guess its something stupid, please help.
ERRORS
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: querys
Severity: Warning
Message: Invalid argument supplied for foreach()
these errors are located in my view page.
VIEW
<?php foreach ($queries as $row): ?>
<table>
<th>Name</th>
<th>Surname</th>
<th>phone</th>
<th>email</th>
<td><?php echo $row->name; ?></td>
<td><?php echo $row->surname; ?></td>
<td><?php echo $row->phone; ?></td>
<td><?php echo $row->email; ?></td>
</table>
<?php endforeach; ?>
CONTROLLER
function display($offset = 0)
{
$limit = 20;
$this->load->model('info_model');
$results = $this->info_model->search($limit, $offset);
$data['queries'] = $results['rows'];
$data['num_results'] = $results['num_rows'];
$this->load->view('info_view',$data);
}
MODEL
function search ($limit, $offset){
//results query
$q = $this->db->select('ID, name, surname, phone, email');
$this->db->from('tblinfo');
$this->db->limit($limit, $offset);
$ret['rows'] = $q->get()->result();
//count query
$q = $this->db->select('COUNT(*) as count', FALSE )
->from('tblinfo');
$tmp = $q->get()->result();
$ret['num_rows'] = $tmp[0]->count;
return $ret;
}
EDIT
i fixed the foreach error by inserting
<?php if(is_array($queries)): ?>
<?php endif; ?>
the only error i am getting is the
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: queries
$data['query'] = $results['rows'];
should be
$data['querys'] = $results['rows']; // although correct spelling is "queries"
Then you can access $querys inside your view.
The problem seems to be with your models search() method, which is probably not returning any results. I would re-write it as follows to utilize codeigniters active record class:
function search ($limit, $offset){
//results query
$this->db->select('ID, name, surname, phone, email');
$q = $this->db->get('tblinfo', $limit, $offset);
$ret['rows'] = $q->result();
//count query
$ret['num_rows'] = $this->db->count_all('tblinfo');
return $ret;
}
You can use Codeigniters active record class to get a count of all records in a specific table using just $this->db->count_all('table_name');
Personally I think a function should perform only one task. Right now you are trying to do two tasks (return records and return a record count) in one function.
I would rewrite my model like this:
function search($limit, $offset)
{
$this->db->select('ID, name, surname, phone, email');
$this->db->from('tblinfo');
$this->db->limit($limit, $offset);
return $this->db->get();
}
And your controller could look like this:
function display($offset = 0)
{
$this->load->model('info_model');
$results = $this->info_model->search(20, $offset);
$data['queries'] = $results->result();
$data['num_results'] = $this->db->count_all('tblinfo'); // Alternatively you could make a separate model function that does this
$this->load->view('info_view', $data);
}
Maybe change your search code a little
$q = $this->db->select('ID, name, surname, phone, email')
->from('tblinfo')
->limit($limit, $offset);
Change your model code to this
function search ($limit, $offset){
//results query
$this->db->select('ID, name, surname, phone, email');
$this->db->from('tblinfo');
$this->db->limit($limit, $offset);
$q = $this->db->get();
$ret['rows'] = $q->result();
//count query
$q1 = $this->db->select('COUNT(*) as count', FALSE )
->from('tblinfo');
$tmp = $q1->get()->result();
$ret['num_rows'] = $tmp[0]->count;
return $ret;
}
Related
I'm trying to get and display the data from joined tables but I can't get anything in return. I reference this and it worked on my other function, but when I tried it again on a different function, I can't get any results.
Here's the Model:
public function viewReview($id)
{
$this->db->select('clients.firstname, clients.lastname, packages.title, rate_review.review, rate_review.date_created');
$this->db->where('rate_review.id', $id);
$this->db->join('clients', 'clients.id = rate_review.user_id');
$this->db->join('packages', 'rate_review.package_id = packages.id');
$this->db->from('rate_review');
$query = $this->db->get();
return $query->row_array();
}
Controller:
public function view_review($id)
{
$data['title'] = 'Rate & Reviews';
$data['review'] = $this->Admin_model->viewReview($id);
$this->load->view('../admin/template/admin_header');
$this->load->view('../admin/template/admin_topnav');
$this->load->view('../admin/template/admin_sidebar');
$this->load->view('../admin/rate_review/view', $data);
$this->load->view('../admin/template/admin_footer');
}
View:
<div class="card-body">
<p>User:
<?php echo $review['firstname'].' '. $review['lastname']; ?>
</p>
<p>Package:
<?php echo $review['title']; ?>
</p>
<div class="m-2">
<pre class="border-2"><?php echo $review['review']; ?></pre>
<br>
<span class="mt-2"><?php echo $review['date_created']; ?></span>
</div>
</div>
When I var_dump($query) in model, this is what I get
the query seems to be fine except that we do not know if the data really exists in those joined tables. Are you sure data if there? the best way to debug is to use profiler so that you preview the real query built $this->output->enable_profiler(TRUE), after that take that query and run it on the mysql directly.
place this anywhere in your controller $this->output->enable_profiler(TRUE).
Please be sure the path is correct and try this query in your model
public function viewReview($data) {
$sql = "SELECT c.firstname as firstname, c.lastname as lastname, p.title as title, r.review as review, r.date_created as date_created FROM rate_review r INNER JOIN clients c ON (c.id = r.user_id)
INNER JOIN packages p ON (p.id = r. package_id) WHERE r.id = ?";
$query = $this->db->query($sql, $data);
return (array)$query->row();
}
Also please change this line in your controller
$data['review'] = $this->Admin_model->viewReview(array($id));
Then error log what you get please
error_log($data['review']);
Table products: id, product, lining, ...
Table tbl_lining: id, article, description
Model
public function getliningsale($id)
{
$this->db->select('*');
$this->db->from('products');
$this->db->join('tbl_lining', 'products.lining=tbl_lining.id', 'left');
$this->db->where('products.id', $id); // Also mention table name here
$query = $this->db->get();
if($query->num_rows() > 0)
return $data->result();
}
Controller
function liningsale($id)
{
$data['liningsale'] = $this->sales_model->getliningsale($id);
$this->load->view('add', $data);
}
View
echo '<td><input class="span1 tran2" name="lining\'+ count +\'" type="text"';
echo 'value="';
foreach ($liningsale as $valor) {
echo $valor->article;
echo '-';
echo $valor->description;
echo '">';
}
echo '</td>';
This doesn't display any record.
I've tried several ways without success.
Can anyone help?
In your model, you need to return the $query result.
At the minute you are trying to return the result set of a variable named $data which I presume doesn't exist!!
Try changing return $data->result(); to return $query->result();
Hope that helps!!
i am trying to access a function in model, in my view in codeigniter and its not working.
can somebody please tell me where is the problem.
MODEL:
function gettemplates()
{
$sql = "select * from srs_templates";
$query = $this->db->query($sql);
$result = $query->result_array();
echo "<pre>"; print_r($result);exit;
return $result;
}
VIEW:
<select id="special" >
<?php echo $this->dashboard_ext_model->gettemplates(); ?>
</select>
Change this:
echo "<pre>"; print_r($result);exit;
To this:
echo "<pre>"; print_r($result); echo "</pre>";
Basically remove the exit. Exit aborts the script.
You should never have a good reason to call the model from the view. Model data should be passed to the controller, to then pass to the view.
Since this is MVC (Model View Controller) you shouldn't call a model from a view.
You should be calling the Model inside the controller then passing to the view as an argument.
Model:
function gettemplates()
{
$sql = "select * from srs_templates";
$query = $this->db->query($sql);
$result = $query->result_array();
echo "<pre>"; print_r($result);exit;
return $result;
}
Controller:
function gettemplates(){
//$this->dashboard_ext_model = $this->load->model('dashboard_ext_model');
//uncomment this if you didn't load the model
$data['templates'] = $this->dashboard_ext_model->gettemplates();
$this->view->load('page_name', $data);
}
View:
<select id="special" >
<?php echo $templates ?>
</select>
MVC may look stupid at first but it really helps in large size projects.
MVC DESIGN: http://i.stack.imgur.com/Beh3a.png
I have the following happening in my model:
public function load_user_menu($username)
{
$this->db->select('*');
$this->db->from('menu');
$this->db->where('username', $username);
$query = $this->db->get();
return $query->result();
}
and the following in my controller:
public function index()
{
/*If user previously logged in and not logged out, username remains in session.
load username and load profile data.
*/
//check if user logged in or not
if(($this->session->userdata('username')!=""))
{
//load data from model
$profile = array();
$username = $this->session->userdata('username');
$result = $this->profileModel->user_profile($username);
foreach($result as &$value)
{
$profile['userdetails'] = $value;
}
$this->load->view('profile', $profile);
}else{
//redirect to login function
$this->login();
}
}
but I am getting errors on profile view. I am sure I am accessing them wrong because I am doing this on view:
<? echo $userdetails['profilepic']; ?>
and nothing is showing but this error:
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: controllers/profile.php
Line Number: 60
which am certain because of the wrong accessing. How can I access the details based on the above?
assumming you have profilepic inside userdetails and seeing the error you are trying to get he property of nonobject that means
<? echo $userdetails['profilepic']; ?>
should be
<? echo $userdetails->profilepic; ?>
since you are taking you result as object and not array
return $query->result();
or you change this to
return $query->result_array();
$query->result(); produces an object; $query->result_array(); gives you an array
either is fine, depending on your needs
-There is no need of foreach loop in your controller as
$result = $this->profileModel->user_profile($username);
foreach($result as &$value)
{
$profile['userdetails'] = $value;
}
-Try this instead.
$profile['userdetails'] = $this->profileModel->user_profile($username);
-To Access in View,use this as
echo $userdetails->profilepic;
just try this on your view page
<? echo $profilepic; ?>
first be gentle im a begginer.
I have a problem with codeogniter pagination and im totally clules.
I watched a lot of tuts on the net and never got any aswer.
My problem is, im building a real-estate site, it has 2 types of estate, for sale and for rent.
My query looks like this
function for_sale()
{
$query = $this->db->query(" SELECT city, rand_id, price, image FROM estate WHERE type = 'for_sale' ");
if ($query->num_rows() > 0)
{
foreach ($query->result() as $f)
{
$for_sale[] = $f;
}
return $for_sale;
}
}
my controller
function index()
{
$this->load->view("header");
$this->load->model('estate_model');
$for_sale['result'] = $this->estate_model->for_sale();
$this->load->library('pagination');
$config['base_url'] = 'http://localhost/kpi/for_sale/index';
$config['total_rows'] = 22;
$config['per_page'] = 9;
$config['num_links'] = 19;
$this->pagination->initialize($config);
// $data['records'] = $this->db->get('ingatlan', $config['per_page'], $this->uri->segment(3));
$this->load->view("for_sale_view", $for_sale);
$this->load->view("footer");
}
and my view
foreach($results as $r) {
echo '<div class="grid_4">';
echo '<p class="title">Estate for sale</p>';
echo '<div class="thumbnail">
<div class="info">
<p class="bar">'.$r->city.'</p>
<p></p>
</div>
'.$r->image.'</div>';
echo '<div class="more">View details</div>';
echo '</div>';
}
So my problem is i cant figure this out how to use it with my query, i watched lots of tuts, that i need to load in the table library, and pass a data array like this
$data['records'] = $this->db->get('estate', $config['per_page'], $this->uri->segment(3));
anf give the per page this result `$this->db->get('estate')->num_rows(); but i would like this with my query, and not with a table.
So can someone hive me a hint?
Thank you
... i need to load in the table library, and pass a data array ...
how you display your data is totally up to you. the html-table class can help you with that, but it is not necessary to use it. your view should work just fine except for the fact that you want to iterate over $results which will be empty because you put your data into $for_sale['result'] (results != result).
in order for the pagination library to work with your query, you have to pass your query 2 parameters:
how many rows/records should be loaded from your db (= $config['per_page'])
how many rows/records should be skipped at the beginning (also known as 'offset', this value will be provided by the pagination library = $this->uri->segment(3))
So instead of:
$for_sale['result'] = $this->estate_model->for_sale();
Model:
function for_sale()
{
$query = $this->db->query(" SELECT city, rand_id, price, image FROM estate WHERE type = 'for_sale' ");
...
}
you should try:
$for_sale['result'] = $this->estate_model->for_sale($config['per_page'],$this->uri->segment(3));
Model:
function for_sale($per_page, $offset) {
$query = $this->db->query(" SELECT city, rand_id, price, image FROM estate WHERE type = 'for_sale' LIMIT $offset, $per_page");
...
return $query->result();
}
One last thing: the pagination library has to be initialized BEFORE you query the database.
And another last thing: $config['total_rows'] = 22; you will probably want to replace that with something like $this->estate_model->get_for_sale_total_rows()
Hope this helps.