EllisLab text mark
Advanced Search
     
Get the generated value of a loop
Posted: 12 November 2012 12:29 AM
Joined: 2012-10-12
58 posts

I’m selecting some values from a DB for put them in a dynamic table. Next of generate the table with four(4) rows get from the db, I need get the value generate of each one row for may to do the appropiate download..

This is the controller.

foreach($query->result() as $row)
  
{   
   $datos 
= array(
   
'idacta' => $row->idacta,
   
'rutaarchivo' => $row->rutaarchivo,
   
'descripcion' => $row->descripcion,
   
'fechademodificacion' => $row->fechademodificacion,
   );
   for(
$i>1$i<=$datos['idacta']$i++)
   
{   
   $content 
.= "  
   <tr>
    <td id='
$i'><input name='opcion-res' type='radio' align='center'/></td>
    <td id='
$i'><a href='download'>$datos[rutaarchivo]</a></td>
    <td id='
$i'>$datos[descripcion]</td>
    <td id='
$i'>$datos[fechademodificacion]</td>
    <input id='
$i' type='hidden' name='hola' value='$datos[rutaarchivo]'/>
   </tr>
   "
;
   
}   
   $this
->session->set_userdata('logged_content'$datos);
   
$this->output->set_output($content);
  

This is the side of my controller where I’m add the variables with the DB rows, I need get the variable of hidden input which content the route of the file for be downloaded. The second <th> have a link wich call the download controller and there I get the value of the route trought the userdata parameters..

If I have 20 files diferents and I try to download any always goes download me the last because is getting the route but outside of the foreach.

The question is : How I do for may get the dynamic variable inside of the loop?

I’m developing a space for downloads. Any suggestion or Idea please let me know..

 
Posted: 12 November 2012 01:35 AM   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2011-08-08
533 posts

First thing first.
Your controller broke the MVC rule.
Unless you do $query for testing purpose only.

To get the dynamic variable from the loop is you store it into an array.

 Signature 

Stick with it, practice it and have fun with it.

 
Posted: 12 November 2012 11:30 AM   [ # 2 ]   [ Rating: 0 ]
Joined: 2012-10-12
58 posts

Ok..  Many thanks by your response.
How I do for get the variable trough of the array, If do you can give me a example will be very good..

 
Posted: 12 November 2012 12:44 PM   [ # 3 ]   [ Rating: 0 ]
Joined: 2012-11-01
81 posts

I think your problem is here:

for($i>1$i<=$datos['idacta']$i++) 

You’re essentially saying: For i greater than 1, and i less than or equal to the value in datos[‘idacta’], and increment by adding i+1.

To use the for() function, you need to know the count() of array items in $datos. But as solid9 said, you’re violating the MVC framework. For example, in your query, there’s no need to store the result as an object then restore it as an array. $query->result_array() will already do this. I think you need to read the user guide sections focusing on views and getting data from a database query.

 
Posted: 15 November 2012 08:14 AM   [ # 4 ]   [ Rating: 0 ]
Joined: 2012-10-12
58 posts

Hello, Many thanks by your responses.

Yes exactly were a problem but just of redound. But only that.

But still I have this problem. I don’t know how to get each one link of download for may to do the downloads in the link that appropiate.

I don’t know if maybe not understands what is the problem.

I need link the name of each one with the route and that when I see the list for example 20 names, When I click on the name automatically pass their route to the controller download. Already I have all right, But always pass the same route variable, generally is the last.

My DB have id, name, description, route, and data. I show all less the route variable that I use it for may to do the download.