How can I output the result of select_sum from controller to the view without using foreach. I don't want to use foreach because my function returns only 1 number.
The logic is I have a column in DB with numeric values called 'time', I want to sum all of them together, and output the result next to /10000 in my view.
I believe I need to use row() but I am not sure how.
Controller:
$this->load->library(array('form_validation', 'session'));
$this->db->select_sum('time');
$data['num'] = $this->db->get('posts');
$this->db->order_by('id', 'DESC');
$data['query'] = $this->db->get('posts');
$this->load->view('templates/header');
$this->load->view('templates/navigation');
$this->load->view('home_page', $data);
$this->load->view('templates/footer');
View:
<?php foreach ($num->result() as $row): ?>
<?php if (empty($row->time)): echo "0"?>
<?php else: echo $row->time ?>
<?php endif ?>/10000</p>
<?php endforeach;?>
You can combine row() with the variable.
$sql = $this->db->select_sum('time');
$sql = $this->db->get('posts');
$data['num'] = $sql->row();
And you can call in your view
$num->time;
hope this solve your problem.
Related
i got code like this on codeigniter :
<?php
$query = $this->db->query("SELECT count(*) FROM data_pribadi WHERE STATUS = 'diterima'");
return $query->result();
?>
how can i call a result?
Try this in your model.
$this->db->select('*');
$this->db->from('data_pribadi');
$this->db->where('STATUS','diterima');
$query = $this->db->get();
return $result=$query->result();
And in your Controller, you can seek the results to your view like the below code
$this->load->model('Mymodel');
$this->load->view('YourviewName',array($data=>$this->Mymodel->Myfunction()));
And these also for your view (example)
<?php
foreach ($data as $u):
echo"<option value ='$u->column_name1'>".$u->column_name. "</option>";
endforeach;
?>
Your code should be placed on model.
Let me guide you.
MODEL:
// file name: Mymodel.php
function test() {
$query = "SELECT count(*) AS total_acc FROM data_pribadi WHERE STATUS = 'diterima'";
return $this->db->query( $query )->row()->total_acc;
}
How about the result?
CONTROLLER
$this->load->model( 'mymodel' ); //available in CI3
$total_acc = $this->mymodel->test();
echo $total_acc;
Done, now you get it. Because your request only produces one row, I think it's suitable for you.
Btw, salam kenal :)
Yesterday when I var_dump($this->m_test->result_getGrades());it gave me an array[1356] now it returned null. Can anyone help me figure out why it's NULL? I'm still new in PHP and Codeigniter and its pretty stressful figuring out why I can't retrieve any data from my database.
I assume the reason why I have this error because ($this->m_test->result_getGrades()) is NULL
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: views/v_display.php
Line Number: 7
Also, What are the factors why I can't retrieve any data from my database? For future references
This is my code:
Controller c_test.php
function getGrades() {
$data['query'] = $this->m_test->result_getGrades();
$this->load->view('v_display', $data);
}
Model m_test.php
function result_getGrades()
{
$this->db->select('grades.blockcode,subjectblocking.subjectcode,subjects.description,grades.final');
$this->db->from('grades');
$this->db->join('subjectblocking','grades.blockcode=subjectblocking.blockcode');
$this->db->join('subjects','subjectblocking.subjectcode=subject.subjectcode');
$this->db->where('2013-F019');
$query=$this->db->get();
}
Views v_display.php
<?php foreach ($query as $row): ?>
<?php echo $row->studentid;?><br>
<?php echo $row->subjectcode;?><br>
<?php echo $row->description;?><br>
<?php echo $row->final;?><br>
<?php endforeach; ?>
Thank you once again! :)
You should try this:
First, you will need to check your where clause.
see example below:
$this->db->where('name', 'test');
that will produce WHERE name = 'test' in the MySQL query
then you have to put a return statement after your get() method:
$query = $this->db->get();
return $query->result_array();
so your model should be something like this:
function result_getGrades()
{
$this->db->select('grades.blockcode,subjectblocking.subjectcode,subjects.description,grades.final');
$this->db->from('grades');
$this->db->join('subjectblocking','grades.blockcode=subjectblocking.blockcode');
$this->db->join('subjects','subjectblocking.subjectcode=subject.subjectcode');
$this->db->where('name of field', '2013-F019');
$query=$this->db->get();
return $query->result_array();
}
There are many faults in your result_getGrades() function :
function result_getGrades()
{
$this->db->select('grades.blockcode,subjectblocking.subjectcode,subjects.description,grades.final');
$this->db->from('grades');
$this->db->join('subjectblocking','grades.blockcode=subjectblocking.blockcode');
$this->db->join('subjects','subjectblocking.subjectcode = subjects.subjectcode');
$this->db->where('your_field', '2013-F019');
$query = $this->db->get()->result();
return $query;
}
There was :
add the return $query;
field name in your where clauses
subject to subjects in your join
var dumping the query itself usually doesn't return much for me usually but it depends on when you dump it.
<?php foreach ($query as $row): ?>
<?php echo $row->studentid;?><br>
<?php echo $row->subjectcode;?><br>
<?php echo $row->description;?><br>
<?php echo $row->final;?><br>
<?php endforeach; ?>
For this you don't need to keep opening and closing the php tags unless you want alot of html around it:
<?php foreach ($query as $row){
echo $row->studentid.'<br>';
echo $row->subjectcode.'<br>';
echo $row->description.'<br>';
echo $row->final.'<br>';
}?>
Notice the . ? It joins the $row->studentid to the html meaning it's alot cleaner code.
Now onto the question, you're assigning a variable as a variable in your foreach...Which is pointless. Thankfully i know the fix. You need to turn the query into a result from your database:
<?php foreach ($query->result() as $row){
echo $row->studentid.'<br>';
echo $row->subjectcode.'<br>';
echo $row->description.'<br>';
echo $row->final.'<br>';
}?>
$query->result() as $row will enable you to echo out the information you get back from the database.
Also at the end of the model, you need to add return $query; otherwise your model is getting the data and not doing anything with it.
Having a bit of trouble getting my For Each function to display all the values from a DB into a view. Where am I going wrong? Not a back-end pro, nor did I write it. Database table is extremely simple, one column named id. I want to display each of the rows in my for each.
I have a custom MY_Controller.php file and in the _construct I have this, which fills in variables globally across the site.
$this->country_zones = $this->Studios_model->get_studios();
Model:
function get_studios()
{
//this will alphabetize them
$this->db->order_by('id', 'ASC');
$query = $this->db->get('gc_studio_zones');
foreach ($query->result() as $row)
{
$countries = $row->id;
}
return $countries;
}
In my view:
<?php foreach($this->$country_zones as $country):?>
<li><?php echo $country['countries']->id;?></li>
<?php endforeach;?>
Error:
Cannot access empty property...etc
I think you have a typo in your view regarding your loop variable, it should be this->country_zones:
<?php foreach($this->country_zones as $country):?>
<li><?php echo $country->id;?></a></li>
<?php endforeach;?>
Besides, your Model isn't returning an array, but only the last id. You need to push the values to your result array:
function get_studios()
{
//this will alphabetize them
$this->db->order_by('id', 'ASC');
$query = $this->db->get('gc_studio_zones');
$countries = array();
foreach ($query->result() as $row) {
$countries[] = $row;
}
return $countries;
}
I am trying to display a table using CodeIgniter. I made a function to select all data from one table and display it using a foreach loop when the button is clicked. I am getting this error:
Fatal error: Call to undefined method CI_DB_mysql_driver::result() in C:\Xampp\htdocs\Auction\application\models\bidding_model.php on line 47
This is my controller page:
public function viewauction()
{
$this->load->model('bidding_model');
$data['query'] = $this->bidding_model->viewauction();
$this->load->view('auction_view', $data);
}
This is the model:
function viewauction()
{
$query = $this->db->select('products');
return $query->result();
}
This is the view:
<tbody>
<?php foreach($query as $row): ?>
<tr>
<td><?php echo $row->product_id; ?></td>
<td><?php echo $row->auction_id; ?></td>
<td><?php echo $row->start_time; ?></td>
<td><?php echo $row->end_time; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
Just change your model method code to
function viewauction()
{
$query = $this->db->select('*')->from('products')->get();
return $query->result();
}
Hope this helps. Thanks!!
you have to use get()
select() query builder is used for selecting the columns of the table and not the table
example
$query = $this->db->select(array('product_id','auction_id'))
->get('products');
return $query->result();
if you want to select all you can use the get only,
read more at
http://ellislab.com/codeigniter/user-guide/database/active_record.html#select
Your problem is here:
$query = $this->db->select('products');
return $query->result() ;
$query->result() is returning false probably because the products table does not exist. you have to use get instead of select.
Try:
$query = $this->db->get('products');
return $query->result() ;
That could get your started
public function select($table, $field, $value)
{
$this->db->select(*);
$this->db->from('$table');
$this->db->where($field, $value);
$query = $this->db->get();
return $query;
}
I hope the above code will help you.
There is actually a simpler way available.
You should get most from the framework features it is providing,
Use, CodeIgniter's Table Library,
$this->load->library('table'); // Loading the Table Library
$query = $this->db->get('table_name'); // the MySQL table name to generate HTML table
echo $this->table->generate($query); // Render of your HTML table
You can also modify the behavior of HTML generator if you want some custom things like class in table head or body or anything, which you will almost need.
$this->table->set_template($template); // passing an array
Use this line after loading the table library. Use keys from the documentation link below.
Reference: CodeIgniter 3 Table Library - Official Docs
function viewauction()
{
$this->db->select('*');
$this->db->from('tablename');
$query = $this->db->get();
return $query->result();
}
Above code will help you.
I'm making a query in PHP using CodeIgniter to get the data that I wanted from a database. To be able to get all values for a specific column, I stored it into an array and passed it into a view. But I can do it only for one column.
Here's my code:
$query = $this->db->query('SELECT name, description FROM module');
$result = $query->result_array();
foreach ($result as $key => $rowdata) {
$resultdata['values'][$key] = $rowdata['name'];
}
$this->load->view('myview', $resultdata);
With this scenario I can get all name from the module table. But my problem is, I also wanted to get all the description in the module table, but I don't know how can I implement it and be able to pass it into the view. Hope someone could help me with this. Thanks!
Your not using the MVC pattern!
First you should write your query in a model!
Then load it in your controller like this
$this->load->model('ModelName');
Then call the funcion to retreive the data
$data = $this->modelname->functionToRetreiveData();
Then loop through data with foreach
$dataToView = array();
foreach($data as $key=>$row)
{
$dataToView['something'][$key]['value'] = $row->name_of_column;
}
And pass the array to the view $this->load->view('someview',$dataToView);
Then in the view
foreach($value as $val):
<p><?php echo $val['name']?><p>
endforeach
hi in you way you will do loop twice in controller and then in view to print it check this out
//in controller
$query = $this->db->query('SELECT name,description FROM module');
$resultdata['results'] = $query->result_array();
$this->load->view('myview',$resultdata);
myview.php
foreach($results as $result)
{
echo $result['name'],' ',$result['description'];
}
$query = $this->db->query('SELECT name,description FROM module');
$result = $query->result_array();
foreach($result as $rowdata){
$resultdata['values'][] = $rowdata;
}
$this->load->view('myview',$resultdata);
Try in view:
print_r($values);
You will probably have:
$result[0]['name'], $result[0]['description'] ...
$this->load->database();
$this->db->select('employee');
$query = $this->db->get();
return $query;
foreach ($query as $row) {
print $row->'Name';
.
.
.
print $row->'Address';
}