How I can change value of objects array, which I got from $this->db->query('My Query');
(result is Object). I tried use foreach ($data->result() as $row)
- it is working fine, I can reach every variable, but I can't modify it by using "&" (foreach ($data->result() as &$row)
) because of Error (php don't show it). Any ideas?
3 Answers
Use an extra variable.
$result = $data->result();
foreach ($result as &$row) {
// enter code here
}
-
Okey, it works fine, but how then I can generate table from array ($result, which I modified)? How it is posible to edi object and then greate table from it?Paulius Butkus– Paulius Butkus11/21/2013 16:51:33Commented Nov 21, 2013 at 16:51
-
I see, this is a different question. You should issue 'update' statements to the database in order to do this. Raffaele's way is what you need.Stefanov.sm– Stefanov.sm11/21/2013 19:10:03Commented Nov 21, 2013 at 19:10
If I understand what you mean, you are assuming that modifing the object you are editing also values in the database.
If that's the case, you should run a query to update data in the database
$result = $data->result();
foreach ($result as &$row) {
$row->prop = 'new value';
$this->db->set($row)->where('id',$row->id)->update('table_name');
}
==================== EDIT ====================
If you want to ignore date like 0000-00-00
you should set it to an empty string and then print out the table.
$result = $data->result();
foreach ($result as &$row)
{
if( $row->date == '0000-00-00')
$row->date = '';
}
to print out a table you can use the HTML table class of Codeigniter passing the $result object.
http://ellislab.com/codeigniter%20/user-guide/libraries/table.html
$this->load->library('table');
echo $this->table->generate($result);
-
Not really, I need to modify values which I got from database and then show them on table. Main problem is with Dates, because, if value was
null
, then Dates value in table comes0000-00-00
, so I need somehow to ignore or change value of it.Paulius Butkus– Paulius Butkus11/21/2013 19:09:16Commented Nov 21, 2013 at 19:09 -
I know this way of solving problem, but CodeIgniter's method
$this->table->generate($data);
wants Object type variable, not array I think.Paulius Butkus– Paulius Butkus11/21/2013 19:25:00Commented Nov 21, 2013 at 19:25 -
You can pass both. Array or object from database result. BTW, here we are working with objects so that's what you are asking :DRaffaele Izzia– Raffaele Izzia11/21/2013 19:26:23Commented Nov 21, 2013 at 19:26
-
From the documentation: "$this->table->generate() Returns a string containing the generated table. Accepts an optional parameter which can be an array or a database result object."Raffaele Izzia– Raffaele Izzia11/21/2013 19:26:55Commented Nov 21, 2013 at 19:26
-
I think no, because there is some problem, when I add Array on
$this->table->generate($result);
, whole table disappears (means, that there is error).Paulius Butkus– Paulius Butkus11/21/2013 19:31:07Commented Nov 21, 2013 at 19:31
For some reasons, methods, that were offered don't work. I found solution, how to solve my problem:
<?php $result = $data->result_array();
foreach($result as &$row)
{
if($row['date'] == '0000-00-00')
{
$row['date'] = ' ';
}
$this->table->add_row($row['date'], $row['(other parameter)']...);
}
$this->table->set_template($template);
echo $this->table->generate(); ?>