CodeIgniter Forums
using the $this->table->generate - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: using the $this->table->generate (/thread-55163.html)



using the $this->table->generate - El Forum - 10-12-2012

[eluser]alvaroeesti[/eluser]


Hi,


I am on to pagination and here is the stuff:


In the model, after a long query I return:


Code:
$query = $this->db->get();
  return $query->result();

Then, in the Controller, I have:


Code:
$data['datos'] = $this->resultados_search_M->devolverResultadosMapa($basement, $garage, $garden, $config['per_page'], $this->uri->segment(3) );
$this->load->view('resultados_search_V1', $data, $data_mapa );

And now, in the View, I wanted to generate the table, using that library, but...it complains that I cannot use a stnd obj class as an array . I realize yes, because usually we would loop through the array in the View, but I wanted to do it with this


Code:
<?php echo $this->table->generate($datos);?>

but is right up here where it gives the error. Is there anyway I can modify it either at the controller or at the model so that that call will work in the View?


thank u very much

regards

A


using the $this->table->generate - El Forum - 10-12-2012

[eluser]Aken[/eluser]
The table library isn't meant to have DB results passed to it. You need to send it an array in the format shown on the user guide page. You'll need to adjust your results before sending them - do that in your controller before loading the view.

Edit: Nevermind, I skimmed the library user guide page too fast. You can pass DB results, or the query itself.


using the $this->table->generate - El Forum - 10-12-2012

[eluser]CroNiX[/eluser]
@Aken There is an example in the user guide showing a table created from a database query result.
Quote:Here is an example of a table created from a database query result. The table class will automatically generate the headings based on the table names (or you can set your own headings using the set_heading() function described in the function reference below).
Code:
$this->load->library('table');
$query = $this->db->query("SELECT * FROM my_table");
echo $this->table->generate($query);



using the $this->table->generate - El Forum - 10-12-2012

[eluser]Aken[/eluser]
That's what I get for skimming.

It looks like you should pass the query object itself (the variable that you would call result() on, for example) for the easiest use. If you pass the result array, it won't function as you'd expect. You'll still need to prep an array if you want to go that route.


using the $this->table->generate - El Forum - 10-13-2012

[eluser]alvaroeesti[/eluser]
Thank you. I got it working by returning from the Model this

Code:
$query = $this->db->get();
return $query;
Instead of the array I had before.

Now, I was using that array for other things, for example to generate a map, doing this:
Code:
$marker = array();
foreach($data as $lugar)
{
$marker['regiones'] = $lugar->nombre_region;
$marker['position'] = $lugar->nombre_localidad;
$this->googlemaps->add_marker($marker);
}


Since I don't have the array now, I don't know how to do the same with the object that I was doing with the array (looping through to get the results). I still don't quite get the workings with objects as used to old procedural.
$data was what I was getting from the Model when I returned the array and not the query object.

This case illustrates something that is used all the time (but I use it without fully understanding why is this so)

For example. When I put as receiver of the array returned by the model, something like you can see in the map example, just $data, it is an array alright, and you loop through it.

However, when you are going to send something to the view, it is not enough to have just the $data, but you have to add the square brackets and put an index in it. You will use this index in the View to loop through it as if it were an array again.

Is this something from codeigniter, it this something from the MVC frameworks, is this plain PHP ?


regards


A




using the $this->table->generate - El Forum - 10-13-2012

[eluser]CroNiX[/eluser]
If you pass the query object, you just need to access the result set...
Code:
foreach($data->result() as $lugar)



using the $this->table->generate - El Forum - 10-13-2012

[eluser]alvaroeesti[/eluser]


Thank you, I appreciate all help because I am learning php on my own (to try to get a job someday) and sometimes I am stuck for days with a small issue like paginating, or repopulating select lists populated from DB.