Welcome Guest, Not a member yet? Register   Sign In
Pass a variable of a controller to another controller
#1

[eluser]masentinel900[/eluser]
I'm management a variable in a controller "X" which get info from a db, I need get this variable in another controller for may work it of different way..

So.. In the controller "X" I show the data get from a DB, when onclick on the name of file in the view call another controller, "Z".. which will download the file. But I need pass to controller "Z" the variable get form DB.

Is just for works of another way and to have options different in this nwe controller.
#2

[eluser]xeroblast[/eluser]
use session flashdata.
#3

[eluser]PhilTem[/eluser]
Either use session (w/ or w/o flashdata) or use the URI segments to pass the argument. It depends on how long your variable will be and how sensitive the data is so you may not want to expose it to the user
#4

[eluser]LuckyFella73[/eluser]
I don't know exactly what you want to do - but maybe you can
put all the logic into a model (especially because you are talking
about database interaction ). You could provide the needed
methods handling the database result the way you need it. The model
can be loaded by all controllers needing that functionality.
#5

[eluser]solid9[/eluser]
I think defining a config variable is also possible.
#6

[eluser]masentinel900[/eluser]
Thanks everybody by your responses.
I'm doing this:
Controller 1
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
session_start(); //we need to call PHP's session object to access it through CI
class Maincontent extends CI_Controller
{
public function __construct()
{
  parent::__construct();  
  $this->load->model('content','', TRUE);
  $this->load->helper('form');
}
function index()
{  
  $query = $this->content->contenido();
  echo "
  [removed][removed]
  <table cellpadding='0' cellspacing='0' border='0' class='display' id='tabla_lista_paises'>
    <thead>
                    <tr>
                        <th>Id</th>
                        <th>Nombre</th>
      <th>Propietario</th>
      <th>Ultima modificacion</th>
                    </tr>
                </thead>
                <tfoot>
                    <tr>
                        <th></th>
                        <th></th>
                        <th></th>
                        <th></th>
                    </tr>
                </tfoot>
                  <tbody>
  ";

  foreach($query->result() as $row)
  {
   echo "
   <tr>
    <td id=''>&lt;input name='opcion-res' type='radio' align='center'/&gt;&lt;/td>
    <td id=''><a href='download'>$row->rutaarchivo</a></td>
    <td id=''>$row->modificadopor</td>
    <td id=''>$row->fechademodificacion</td>
    &lt;input type='hidden' value='hola'/&gt;
   </tr>  
   ";  
  $data = array(
  'rutaarchivo' => $row->rutaarchivo,
  );
  }  
  $this->session->set_userdata($data);
}
}
?&gt;

In this controller I generate a dynamic table with info from the database. The table have four rows, in the second row appear the name of the file.. When the user onclick in the name of the file ("second row"), active the download controller with "href".

This is the controller "download"

Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
session_start(); //we need to call PHP's session object to access it through CI
class Download extends CI_Controller
{
public function __construct()
{
  parent::__construct();  
  $this->load->model('content','', TRUE);
}
function index()
{
  /*
  $query = $this->content->contenido();
  foreach($query->result() as $row)
  {
   $ruta = $row->rutaarchivo;
  }  
  */
  $ruta = $this->input->post('hola');
  $this->load->helper('download');
  $data = file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/Online-App/filesFolder/" . $ruta);
  $name = $ruta;
  force_download($name, $data);
}

function insert()
{
  $nombre = array(
  'nombreacta' => $this->input->post('nombreacta'),
  'descripcion' => $this->input->post('descripcion'),
  'rutaarchivo' => $this->input->post('nombreacta'),
  );
  $this->content->insert($nombre);
}
}
?&gt;

Right now I just i'm using the index method, Which about the route of the file download the corresponding file stored in my server folders.

The problem is that I need load the variable rutaarchivo which is a row of my DB, this variable should to be in due controller, In the frst bucause there I'm bringing from the DB, and the second controller because there I going to use it for may to do the download..

When I store the variable "$data" and I set for the userdata session Show me the next log error..



A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at C:\Program Files (x86)\PostgreSQL\EnterpriseDB-ApachePHP\apache\www\Online-App\CodeIgniter\application\controllers\maincontent.php:46)

Filename: libraries/Session.php

Line Number: 672


And also I tried of pass it through form helper, but I see that is not possible because I donĀ“t have the button of submit for pass the value..
#7

[eluser]PhilTem[/eluser]
What do my eyes see? You are echoing in a controller. That's an absolute No-No

Plus it also is the source of your problem/error. Try to integrate views into your controller and code to make your app (a) fully MVC compliant, (b) less prone to (whitespace echoing) errors, and © more readable.
#8

[eluser]masentinel900[/eluser]
Yes I know that I should conserve the MVC, But in this situation I need put that like this.. already I can pass the variable but still appear this error...


A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at C:\Program Files (x86)\PostgreSQL\EnterpriseDB-ApachePHP\apache\www\Online-App\CodeIgniter\application\controllers\maincontent.php:48)

Filename: libraries/Session.php

Line Number: 672


Also I need that the variable that pass to next controller be the same that is selected.. But just pass the last info from the DB..

Please someone that may give any idea..
#9

[eluser]LuckyFella73[/eluser]
You get the error because you echo out / send data to be rendered
before you try to alter session data. You can only alter/ set
session data before the first output. You wouldn't get the
problem when following the mvc principle Wink
#10

[eluser]CroNiX[/eluser]
CI uses buffered output when using the loader class and other things.

So when you directly echo from a controller, that output gets sent out before the rest of the page which includes the headers. Obviously, headers need to be sent before any other output.

You can always use the output class in your controller to append the content that you were echoing to the buffer.

Code:
$this->output->append_output('<div>some extra thing</div>');

However, generally there should be NO HTML in a controller. That belongs in a view. If you follow how CI is meant to be used you will have fewer problems.

Controllers should really only gather the information needed from models/libraries/etc to generate the view.




Theme © iAndrew 2016 - Forum software by © MyBB