Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

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?

share|improve this question

Use an extra variable.

$result = $data->result();
foreach ($result as &$row) {
 // enter code here
}
share|improve this answer
    
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 Nov 21 '13 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 Nov 21 '13 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);
share|improve this answer
    
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 comes 0000-00-00, so I need somehow to ignore or change value of it. – Paulius Butkus Nov 21 '13 at 19:09
    
I edited the answer :D – Raffaele Izzia Nov 21 '13 at 19:16
    
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 Nov 21 '13 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 :D – Raffaele Izzia Nov 21 '13 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 Nov 21 '13 at 19:26

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(); ?>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.