EllisLab text mark
Advanced Search
     
using the $this->table->generate
Posted: 12 October 2012 01:24 PM   [ Ignore ]
Joined: 2012-05-29
124 posts

Hi,


I am on to pagination and here is the stuff:


In the model, after a long query I return:

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

Then, in the Controller, I have:

$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

<?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

 
Posted: 12 October 2012 06:19 PM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2007-11-28
2435 posts

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.

 
Posted: 12 October 2012 08:04 PM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2009-02-19
3800 posts

@Aken There is an example in the user guide showing a table created from a database query result.

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).

$this->load->library('table');
$query $this->db->query("SELECT * FROM my_table");
echo 
$this->table->generate($query); 
 Signature 
 
Posted: 12 October 2012 10:35 PM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2007-11-28
2435 posts

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.

 
Posted: 13 October 2012 03:10 AM   [ Ignore ]   [ # 4 ]   [ Rating: 0 ]
Joined: 2012-05-29
124 posts

Thank you. I got it working by returning from the Model this

$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:

$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

 
Posted: 13 October 2012 04:06 PM   [ Ignore ]   [ # 5 ]   [ Rating: 0 ]
Avatar
Joined: 2009-02-19
3800 posts

If you pass the query object, you just need to access the result set…

foreach($data->result() as $lugar
 Signature 
 
Posted: 13 October 2012 04:14 PM   [ Ignore ]   [ # 6 ]   [ Rating: 0 ]
Joined: 2012-05-29
124 posts

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.