0

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 3

0

Use an extra variable.

$result = $data->result();
foreach ($result as &$row) {
 // enter code here
}
2
  • 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? Commented 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. Commented Nov 21, 2013 at 19:10
0

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);
7
  • 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. Commented 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. Commented 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 :D Commented 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." Commented 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). Commented Nov 21, 2013 at 19:31
0

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(); ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.