Error:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: calificacione
Message: Invalid argument supplied for foreach()
what is wrong?
In Controller the variable is created: $data['calificacione']
And in the view is called on foreach
My View:
<table class="table table-condensed table-striped">
<thead>
<tr>
<th>Calificacione</th>
</tr>
</thead>
<tbody>
<?php
foreach ($calificacione as $key => $value) {
echo "<pre>";print_r($value);echo "</pre>";
}
?>
<tr>
<td><?php ?></td>
</tr>
<?php } ?>
</tbody>
</table>
My Controller
class Provedore extends CI_Controller{
function __construct()
{
parent::__construct();
$this->load->model(['Provedore_model','Provedor_calificacione_model','Provedor_calificacione_model','Personas_contato_model','Direcione_model']);
}
/*
* Listing of all provedores
*/
function index()
{
$data['provedores'] = $this->Provedore_model->get_all_provedores();
$data['calificacione'] = $this->Provedor_calificacione_model->get_all_provedor_calificaciones();
$data['_view'] = 'provedore/index';
$this->load->fullView('layouts/main',$data);
}
Please check below mentioned possibilities.
Are you getting data from model or any error.
var_dump($data['calificacione']);
die;
$data['_view'] = 'provedore/index';
$this->load->fullView('layouts/main',$data);
Please share your output so we can help you.
in your model Provedore_model get_all_provedores may has some arguments like
function get_all_provedores ($argument)
{
// your database query
}
and you did'nt fulfilling that argument
//try this
function index()
{
$provedores = $this->Provedore_model->get_all_provedores();
$data['provedores'] = $provedores;
$calificacione = $this->Provedor_calificacione_model->get_all_provedor_calificaciones();
$data['calificacione'] = $calificacione;
$this->load->view('layouts/main',$data)
$this->load->view('provedore/index',$data)
}
Related
I am in a codeigniter project, examining a file in the views folder. The php is as follows:
As a complete beginner, trying to make sense of this, I am trying to change the value of 'username' below to the 'firstname' that is a different field in the database. Changing the field below, from username, to firstname, causes an error.
I notice there is a loop using the variable: $results as $result)
What I want to know -as a lay person - is how to trace back where these variables are being 'called'from and which files to look into in order to find out how to use the required field, in this case, firstname, which is also from the database.
I have looked into the controllers, but can find nothing resembling this and don't know where to start, as the developer hasn't necessarily labelled everything logically.
My question is: As someone coming into a codebase that they don't know, how do you go about tracing back the logic to find out where you need to look to change a variable and make a change.
Any help on this matter would be greatly appreciated.
<?php
$rank = 0;
foreach($results as $result){$rank++;
?>
<tr style="border: 1px solid #ebebeb;padding-bottom: 0px;background-color: #fff;">
<td ><?php echo $rank; ?></td>
<td ><?php echo $result->username; ?></td>
<td ><?php echo $result->marks; ?></td>
<td ><?php echo $result->percentage; ?></td>
<td ><?php echo $result->duration; ?></td>
<td ><?php echo $result->date_time; ?></td>
<td>
<?php /*<i class="glyph-icon tooltip-button demo-icon icon-edit" title="Edit" style="color:blue;" data-original-title=".icon-edit"></i>
*/?>
<a onclick="return confirm('Are you Sure to Delete this Result???');" href="<?php echo site_url('result/delete/'.$result->id); ?>"><i class="glyph-icon tooltip-button demo-icon icon-close" title="Delete" data-original-title=".icon-close" style="color:red;"></i></a>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
</div>
</div>
<?php } ?>
The starting code on the page I mention is below: Again, notice $results. What is it? Where do I identify its origin and seek to find the fields that populate it?
<?php if(empty($results)) {?>
<div id="page-title">
<h2>No Results Found.</h2>
</br>
</div>
<?php } else { ?>
<div id="page-title">
<h2>All Results</h2>
<h3>Quiz : <?php echo $results[0]->quiz_name; ?></h3>
Delete All
</br>
</div>
What I have done so far:
I have found the controller that seems to be relating to it - pasted below:
load->model('results');
if(!$this->session->userdata('ID'))
{
$this->session->set_flashdata('noAccess', 'Sorry');
redirect(site_url());
}
}
public function index($id='')
{
$data['results'] = $this->results->get_resultByQuiz(-1);
$this->load->view('template/header.php');
$this->load->view('result/index.php',$data);
$this->load->view('template/footer.php');
}
public function view($id='')
{
$data['results'] = $this->results->get_resultByQuiz($id);
$this->load->view('template/header.php');
$this->load->view('result/index.php',$data);
$this->load->view('template/footer.php');
}
public function delAll($id = '')
{
$updated = $this->results->delAll($id);
if($updated)
{
$this->session->set_flashdata('resultDeleteSuccess','result');
}
else
{
$this->session->set_flashdata('resultDeleteFail','result');
}
redirect('quiz');
}
public function delete($id = '')
{
$updated = $this->results->delete_results($id);
if($updated)
{
$this->session->set_flashdata('deleteSuccess','result');
}
else
{
$this->session->set_flashdata('deleteSuccess','result');
}
redirect('result');
}
}
I have also found this related model.
<?php if(!defined('BASEPATH')) exit ("No direct script access allowed");
class Results extends CI_Model {
public function get_all_results()
{
$query = $this->db->get('quiz_takers');
return $query->result();
}
public function get_resultByQuiz($ID)
{
$query = $this->db->query("select (select quiz_name from quizes where id = qt.quiz_id) as quiz_name, qt.* from quiz_takers qt where qt.quiz_id = '$ID' order by qt.percentage desc ");
return $query->result();
$this->db->where('quiz_id',$ID);
$query = $this->db->get('quiz_takers');
return $query->result();
}
public function delAll($ID = '')
{
$this->db->where('quiz_id',$ID);
return $this->db->delete('quiz_takers',$data);
}
public function delete_results($ID = '')
{
$this->db->where('id',$ID);
return $this->db->delete('quiz_takers',$data);
}
}
I am still none the wiser in determining where to alter what is held in that $results variable, so that I can change the field from username to firstname.
I suspect it may have something to do with this:
return $query->result();
but where do I search for queries? In models/controllers - or somewhere else?
In codeigniter,
view is where you write the code to be seen on front-end.
model is where you write your DB queries.
controller is where you connect your view and model(front-end
with DB) ie it works as an intermediate. Also, the routes are
generated as your controller-name and then your function-name plus any additional parameter you provided to that function.
So, if your route(URL) is
your-website/controller_name/function_name/additional-parameter
you need to look at the function {function_name} in your controller {Controller_name}.
Sometimes you might see that the controller or the function is not present in the location as it should(from the URL) then you need to check if any route is provided for that particular URL which will be available at application->config->routes.php
Say if your URL is
www.site/xyz
$route['xyz'] = 'controller_name/function_name'; // route set in routes.php
You need to look for function {function_name} in your controller {Controller_name}.
You need not worry about the model or view as they're called and loaded in the controller itself.
Now, about $results,
it is the key provided to the array variable in the controller which acts as variable in the view. See example -
Controller
$data['xyz'] = 'some-value'; // or $whatever['xyz'];
$data['abc'] = 'any-value'; // ...
$this->load->view('some_folder/some_view', $data); // pass $data array or $whatever
View (located at view->some_folder->some_view.php)
echo $xyz; // output: some-value -- key {xyz} of $data becomes a variable
echo $abc; // output: any-value -- key {abc} of $data becomes a variable
For more details, you might wanna look here, here and here.
See if it helps you.
my model
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
class User_model extends CI_Model{
public function get_user($offset,$limit,$q=''){
$sql = "SELECT * FROM users WHERE 1=1 ";
if($q!=''){
$sql .=" AND name LIKE '%{$q}%' ";
}
$result['count'] = $this->db->query($sql)->num_rows();
$sql .=" LIMIT {$offset},{$limit} ";
$result['data'] = $this->db->query($sql)->result();
return $result ;
}
}
<?php
class User extends CI_Controller{
public function __construct(){
parent::__construct();
//load the model
// $this->load->model('user_model');
}
//load user view
public function index(){
$this->load->view('user_view');
}
//get data for datagrid
public function get_user(){
/*Default request pager params dari jeasyUI*/
$offset = isset($_POST['page']) ? intval($_POST['page']) : 1;
$limit = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
$search = isset($_POST['search']) ? $_POST['search'] : '';
$offset = ($offset-1)*$limit;
$data = $this->user_model->get_user($offset,$limit,$search);
$i = 0;
$rows = array();
foreach ($data ['data'] as $r) {
//array keys ini = attribute 'field' di view nya
$rows[$i]['first_name'] = $r->first_name;
$rows[$i]['last_name'] = $r->last_name;
$rows[$i]['phone'] = $r->phone;
$rows[$i]['email'] = $r->email;
$i++;
}
//keys total & rows wajib bagi jEasyUI
$result = array('total'=>$data['count'],'rows'=>$rows);
echo json_encode($result); //return nya json
}
}
<?php $url_data=base_url().'user/get_user';?>
<?php echo $url_data;?>
<table id="dg" title="Product" class="easyui-datagrid"
style ="width:auto;height:400px"
url ='<?php echo $url_data;?>' toolbar="#toolbar"
pagination="true"
rownumbers="false"
fitColumns="true" singleSelect="true"
checkBox ="true" striped="true"
remoteSort="false"
nowrap ="false">
<thead>
<tr>
<th field="ck" checkbox="true"></th>
<th field="first_name" width="200" sortable="true">
<b>First Name</b>
</th>
<th field="last_name" width="700" sortable="true">
<b>Last Name</b>
</th>
<th field="phone" width="100" sortable="true">
<b>Phone</b>
</th>
<th field="email" width="100" sortable="true">
<b>email</b>
</th>
</tr>
</thead>
</table>
I'm not that much into OOP so I may be wrong.
I think you forgot to encode the result of your SQL query in JSON before returning it :
echo json_encode($result);
Hope this will help.
My model is:
function get_all_pages(){
$query= $this->db->query("SELECT Title FROM news");
return $query->result_array();
}
My view is:
<table cellpadding="0" cellspacing="0">
<tr>
<th>Title</th>
</tr>
<?php
$count=1;
if(!empty($content)) {
foreach ($content as $content1)
{
?>
<tr>
<td align="left"><?php echo $content1['Title']; ?></td>
</tr>
<?php
}
} else{?>
<tr>
<td align="center" colspan="4"><?php echo "No Record Added Yet!!!";?></td>
</tr>
<?php }?>
</table>
My controller is:
public function manage_news()
{
$this->load->model('users_model');
$result_nb = $this->users_model->get_all_pages();
$data['content'] = $result_nb;
$data['page_title']="Manage Pages";
$this->load->view("manage_news",$data);
}
I am using codeigniter. my exact error is:
Call to undefined method users_model::get_all_pages()
how can i fix this error? i want the title row data to appear in a table (from the db)
Q: use Correct naming structure for CI? Which version do u use?
Filename
models/user_model.php
Examples (for demo purpose)
<?php
class User_model extends CI_Model {
// example one
function get_all_pages() {
$query = $this->db->query("SELECT Title FROM news")->result_array();
return ( is_array($query) && sizeof($query) > 0 ? $query : FALSE);
}
// example second
function get_all_pages_second() {
$data = FALSE;
$query = $this->db->query("SELECT Title FROM news");
if ($query->num_rows() > 0){
$data = $query->result_array();
}
return $data;
}
// example third
function get_all_pages_third() {
$data = FALSE;
$query = $this->db->select('Title')->get('news');
if ($query->num_rows() > 0){
$data = $query->result_array();
}
return $data;
}
}
I'm new to codeigniter and followed the guides but seem to be missing something. I have a simple database with customer records in it. My first goal in codeigniter is to simple list all my customers.
here is my controller:
public function index()
{
$this->load->model('HomeModel'); //loads the HomeModel model
$data = $this->HomeModel->function1(); //loads the function (function1) from the model
$this->load->view('index', $data);
}
Here is my model:
public function function1()
{
$query = $this->db->get('work_orders');
return $query->result();
}
here is my view:
<table class="table table-bordered table-striped table-highlight" id="invoice-details">
<thead>
<tr>
<th>Work Order ID</th>
<th>Status</th>
<th>Description</th>
</tr>
</thead>
<?php
foreach ($data->result() as $row);?>
<tr class="even gradeC">
<td><a href="/WorkOrders/viewWo/<?php echo $row['id']; ?>">
<?php echo $row['id']; ?></a></td>
<td><?php echo $row['status']; ?></td>
<td><?php echo Logic\System\Lib\Helper::trunc(htmlentities($row['description']), 8); ?></td>
</tr>
<?php endforeach; ?>
</table>
Change on model :
public function function1()
{
$query = $this->db->get('work_orders');
//return $query->result();
return $query->result_array();
}
Change on controller :
public function index()
{
$this->load->model('HomeModel');
//$data = $this->HomeModel->function1();
$data['result'] = $this->HomeModel->function1();
$this->load->view('index', $data);
}
Change on view :
//foreach ($data->result() as $row);
if(is_array($result)&&!empty($result))
foreach ($result as $row);
Data is passed from the controller to the view by way of an array or an object in the second parameter of the view loading function. Here is an example using an array:
$data = array(
'title' => 'My Title',
'heading' => 'My Heading',
'message' => 'My Message'
);
$this->load->view('blogview', $data);
Views : Codeigniter User Guide
And Since you are using array on your view like $row['id'], you got to use result_array on model to return result set array:
This function returns the query result as a pure array, or an empty array when no result is produced. Typically you'll use this in a foreach loop, like this:
$query = $this->db->query("YOUR QUERY");
foreach ($query->result_array() as $row)
{
echo $row['title'];
echo $row['name'];
echo $row['body'];
}
Generating Query Results : Codeigniter
You can use $this->db->join();
$this->db->select('*');
$this->db->from('blogs');
$this->db->join('comments', 'comments.id = blogs.id');
$query = $this->db->get();
Produces:
SELECT * FROM blogs
JOIN comments ON comments.id = blogs.id
Go through active record class guide .
Active Record Class
In ur controller,u must pass the values as subarray of data array$data['result'].After which u can simply call it in view as $result .
Controller
$data['result'] = $this->HomeModel->function1();
And In view,
foreach ($result as $row){
<?php echo $row->id; ?>
..
}
I am getting the message:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: query
Filename: views/main.php
Line Number: 12
This is the code for my controller:
function get_all(){
$this->load->model('book_model');
$this->load->library('table');
$this->load->helper('html');
$data['query'] = $this->books_model->books_getall();
$this->load->view('main', $data);
}
And this is my view:
<h1> List of books in library</h1>
<table border="1"/>
<tr>
<th>ID</th>
<th>Title</th>
<th>Author</th>
<th>Information</th>
<th>Publisher</th>
<th colspan="2">Action</th>
</tr>
<?php
if(is_array($query)){
foreach ($query as $row){
echo "<tr>";
echo "<td>".$row->id."</td>";
echo "<td>".$row->title."</td>";
echo "<td>".$row->author."</td>";
echo "<td>".$row->information."</td>";
echo "<td>".$row->publisher."</td>";
echo "<td>".anchor('book/input'.$row->id,'Edit')."</td>";
echo "<td>".anchor('book/delete'.$row->id,'Delete')."</td>";
echo "</tr>";
}
}
?>
</table>
Thanks in advance for any help
Edit: This is my model code:
function get_all(){
$this->load->database();
$query = $this->db->get('books');
return $query->result();
}
From what I can see, line 12 appears to be this line:
if(is_array($query)){
And the error message is saying that $query is undefined. When you pass $data['query'] through to the view, that becomes $query.
I note your model is also returning $query->result(). This is not correct - result() should be used inside a loop to loop through the results, and you can use num_rows() to test for more than zero results, as in this example:
Model
function example()
{
$query = $this->db->get('books');
return $query;
}
Controller
$this->load->model('book_model');
$this->load->library('table');
$this->load->helper('html');
$data['query'] = $this->books_model->books_getall();
$this->load->view('main', $data);
View
if($query->num_rows() > 0)
{
foreach($query->result() as $row)
{
// Do stuff here on each $row
}
}
I suggest you take a look at this part of CodeIgniter's documentation, which is actually very good.