• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Adding html Around a Table Column

#1
[eluser]thatguy142[/eluser]
Hello,

I have a function that I'm using to generate a table based on a query run against the database. I want to add a link in each row in the "title" column for a user to click on to edit more details related to that row, but the returned table still doesn't get updated.

Code:
function create_table($table) {
      
            $this->load->library('Jquery_pagination');
            $this->load->library('table');
        
            /*
            ...

            pagination configuration and initialization...

            ...
            */
            
            $this->db->select('id, due_date, title, company, short_description');
            $query = $this->db->get('jobs', $config['per_page'], $this->uri->segment(3));
            
            
            foreach ($query->result_array() as $row) {
                $row['title'] = anchor('jobs/edit/'.$row['id'],$row['title']);
            }  
      
      return $query->result_array();
      
    }

Is there something obvious that I'm missing here? Thanks.

#2
[eluser]Aken[/eluser]
$row becomes a new variable that is only available during that iteration. In order to change the original array item, you must define it as a reference.
Code:
foreach ($query->result_array() as &$row) { ...

http://php.net/manual/en/language.references.php

#3
[eluser]thatguy142[/eluser]
Thanks for the suggestion. I tried that earlier today but I was getting this error when I did:

"Fatal error: Cannot create references to elements of a temporary array expression"

So I wasn't sure if that was the right way to go. I can tell that the changes are being made to a local copy of the array, I just can't figure out how to get the right array changed.

#4
[eluser]thatguy142[/eluser]
Maybe I'm going about this the wrong way. Is there a way to manipulate a particular table cell using the table library itself? I searched through this page and didn't see anything that looked like it could help me.

#5
[eluser]thatguy142[/eluser]
The next thing I've tried is assigning the query result to a variable before the foreach like so:

Code:
$this->db->select('id, due_date, title, company, short_description');
            $query = $this->db->get('jobs', $config['per_page'], $this->uri->segment(3));
            
            $result = $query->result();
            
            foreach ($result as &$row) {
                $row['title'] = anchor('jobs/edit/'.$row['id'],$row['title']);
            }

      return $result;

The new error I'm getting is "Fatal error: Cannot use object of type stdClass as array" on this line:

Code:
$row['title'] = anchor('jobs/edit/'.$row['id'],$row['title']);

. I think I'm on the right track and just need to figure out how to access the data properly.

#6
[eluser]thatguy142[/eluser]
Ok. The thing I've realized is that $query->result() returns the result rows as objects.

I've thus changed the last code bit to this:

Code:
$query = $this->db->get('jobs', $config['per_page'], $this->uri->segment(3));
            
      foreach ($query->result() as &$row) {
          $row->title = anchor('jobs/edit/'.$row->id,$row->title);
      }
            
return $query->result();

This puts me back at the error:

“Fatal error: Cannot create references to elements of a temporary array expression”

And if I remove the & in front of row, I get a familiar error in the table library itself:

"Fatal error: Cannot use object of type stdClass as array in C:\wamp\www\ci\system\libraries\Table.php on line 178"

#7
[eluser]thatguy142[/eluser]
Sorry to clog up the forum with so many self-responses. I've solved the problem. I was mixing up the syntax between $query->result() and $query-result_array(). Here's the final solution:

Code:
$query = $this->db->get('jobs', $config['per_page'], $this->uri->segment(3));
            
      $result = $query->result_array();
            
      foreach ($result as &$row) {
          $row['title'] = anchor('jobs/edit/'.$row['id'],$row['title']);
      }    
      
return $result;

I hope this helps someone in the future!


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.