Query not working in CodeIgniter - codeigniter

function menuName ()
{
$this->viewData['page_title'] = "ContentManagement Systemt!";
$this->db->where('visible', 1);
$this->db->order_by("position", "ASC");
$query = $this->db->get('subjects');
$subjects = $query->result();
foreach ($subjects as $subject)
{
echo $subject->menu_name ."<br />";
$this->db->where('subject_id', $subject->id );
$query = $this->db->get('pages');
$pages = $query->result();
foreach ($pages as $page)
{
echo $page->menu_name ."<br />";
}
}
}
Why is my query not working? Please tell me.

You should always use foreach loops like the following:
$subjects = $query->result();
foreach ($subjects as $subject)
Your approach has a bad performance.
And maybe this already solves your problem - I couldn't test it right now . Your query variables are named equally and you're using $query in both of your foreach loops - this could maybe lead to some strange behaviour.

Why dont you echo the query like so:
echo $this->db->last_query()
To see what is going on with the results
Maybe the problem is there.

Related

I couldnt get the joined table value in my query

Workflow type table
[Workflow caterory table][2]
public function view_wf_category()
{
$this->db->select('workflow_category.wf_cat','workflow_type.type_name');
$this->db->from('workflow_category');
$this->db->join('workflow_type', 'workflow_type.workflow_type_id = workflow_category.wf_type');
$query = $this->db->get();
foreach($query->result_array() AS $row)
{
echo $row["wf_cat"]."<br/>";
echo $row["type_name"]."<br/>";
}
}
here im not able to print the $row["type_name"] and also showing a notice as undefined index for $row["type_name"].
Try like this...
public function view_wf_category()
{
$this->db->select('workflow_category.wf_cat,workflow_type.type_name')
->from('workflow_category')
->join('workflow_type', 'workflow_type.workflow_type_id = workflow_category.wf_type');
$query = $this->db->get();
foreach($query->result_array() as $row)
{
echo $row["wf_cat"]."<br/>";
echo $row["type_name"]."<br/>";
}
}
Error due to this line..
$this->db->select('workflow_category.wf_cat','workflow_type.type_name');
So it should be just
$this->db->select('workflow_category.wf_cat,workflow_type.type_name');
In above query i have been used query grouping.

Iterating through result set in Laravel controller

I am trying to simply iterate though a result set in laravel on the controller side. This is what I tried but I get the following error:
Cannot use object of type stdClass as array
Controller snippet:
$result = DB::select($query);
foreach($result as $r){
echo $r['email'];
}
I appreciate any help with this,
Thanks in advance!
You need to use it as an object:
$result = DB::select($query);
foreach($result as $r){
echo $r->email;
}
Or if for some reason you want to use it as array, you need to convert it first:
$result = DB::select($query)->toArray();
foreach($result as $r){
echo $r['email'];
}
After pulling data from database you can convert it to array but it is optional, it's depends on your necessity then you can simply use the foreach to loop through to each distinct object and get access their property
$data = DB:: table('categories')
->where('parent_id', $request->value)
->get()->toArray();
$output = '<option value="">Select '.ucfirst($request->dependent).'</option>';
foreach($data as $row){
$output.= '<option value="'.$row->id.'">'.$row->title.'</option>';
}
return response($output, 200);
The Laravel Collection class supports the standard methods for iterables: map, sort, filter etc. see: vendor/laravel/framework/src/Illuminate/Collections/Collection.php
if you're querying on a model:
$results = MyAwesomeModel::where('something' '=' 'other')->get();
//this is valid
$results->map(function($result) {
echo $result->field;
});
//this is also valid
for($i = 0; $i < sizeof($results); $i++){
echo $results[$i]->field;
}
I would suggest avoiding ->toArray() on hydrated results (instances of models) because you loose all the model properties.

Codeigniter model pass value controller to view

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!!

codeigniter how to show data in functions

My model:
function get_data($id)
{
$this->db->select('id, Company, JobTitle');
$this->db->from('client_list');
$this->db->where('id', $id);
$query = $this->db->get();
return $query->result();
}
I want to get the the data from get_data(), is this the right way?
public function show_data( $id )
{
$data = $this->get_data($id);
echo '<tr>';
echo '<td>'.$data['Company'].'</td>';
echo '<td>'.$data['JobTitle'].'</td>';
echo '<td>'.$data['id'].'</td>';
echo '<td></td>';
echo '</tr>';
}
Use the row_array() function to get the data in the array format.
Reference url
http://ellislab.com/codeigniter/user-guide/database/results.html
you can use foreach loop to print
foreach ($data->result() as $row)
{
echo '<tr>';
echo '<td>'.$row['Company'].'</td>';
echo '<td>'.$row['JobTitle'].'</td>';
echo '<td>'.$row['id'].'</td>';
echo '<td></td>';
echo '</tr>';
}
thank you
just to improve answer, I use "general_model" for all my controllers, there are some exceptions where I need special queries, I just create desired model so "general_model" stays same and I can use it in any project.
e.g.
general_model.php
function _getWhere($table = 'table', $select = 'id, name', $where = array()) {
$this->db->select($select);
$q = $this->db->get_where('`'.$table.'`', $where);
return ($q->num_rows() > 0) ? $q->result() : FALSE;
}
.
.
.
//bunch of another functions
in controller I just call
$this->data['books'] = $this->general_model->_getWhere('book', '*', array('active' => '1'));
$this->render('book_list_view'); // $this->load->view('book_list_view', $this->data);
sidenote: I am extending CI_Controller therefore I use $this->data['books'] instead $data['books'] to pass data into view
in view
//check if there are any data
if ($books === FALSE) {
//some error that there are no books yet
} else {
//load data to table or something
foreach ($books as $book) {
$book->id; // book id
}
}

Codeigniter returning non-object

I have this happen all the time and I'm never quite sure why it's happening. Here is the query in my model:
$query = $this->db->query('SELECT * FROM members WHERE email = "' . $this->session->userdata('email') . '"');
return $query;
I try to output this in my view with:
foreach($query->result() as $row) {...echo $row->name, etc.}
But I get an error:
Fatal error: Call to a member function result() on a non-object ...
I ran the profiler and my query is valid and there is data in the database to be pulled. So what am I do wrong?
Try this.
In your model:
function get_members()
{
$query = $this->db->query('SELECT * FROM members WHERE email = "' . $this->session->userdata('email') . '"');
return $query->result();
}
In your controller:
You should then be assigning the data from the database in your controller to your view like so.
$data['query'] = $this->yourmodel->get_members(); // Data['query'] will be turned into $query in your view.
$this->load->view('mytemplate', $data);
In your view:
foreach($query as $row) {...echo $row->name, etc.}
If you are passing the $query variable to the view, codeigniter will convert it into a keyed array if it is an object.
http://codeigniter.com/user_guide/general/views.html
Note: If you use an object, the class variables will be turned into array elements.
Edit:
In your controller:
$query = // do whatever to get the $query.
$members = array();
foreach($query->result() as $row) {
$members[] = $row;
}
//send $members to view
In view:
foreach($members as $member) {
echo $member['name']; // etc
}

Resources