Welcome Guest, Not a member yet? Register   Sign In
Redirect to session value how to
#1

[eluser]ReyPM[/eluser]
Hi, I need some help with this problem because is not clear to me. First suppose that I have this URL:
Code:
localhost/adminbiblios.php/cauthor/update/44304
. This is my controller:
Code:
class Cauthor extends CI_Controller {

    private $author_id;

    function __construct() {
// Call the Model constructor
parent::__construct();

if (!$this->ion_auth->logged_in()) {
     redirect('auth/login');
}

$empty_session = array(
     'ID_AUTOR_PPI' => '',
     'ID_AUTOR' => '',
     'NIVEL_ING' => '',
     'COHORTE' => '',
     'PSEUDO_COH' => '',
     'COMISION' => '',
     'PUB_PPI' => '',
     'NIVEL_EGR' => '',
     'ANO_EGR' => '',
     'ANO_REING' => '',
     'ANO_LIC' => '',
     'MENCIO_LIC' => '',
     'AREA_LIC' => '',
     'UNIVER_LIC' => '',
     'PAIS_LIC' => '',
     'MAX_GRADO' => '',
     'ANO_MA_PSG' => '',
     'DOCTORADO' => '',
     'ANO_DOC' => '',
     'UNIVER_DOC' => '',
     'PAIS_DOC' => '',
     'COD_ESPEC' => '',
     'ESPECIALIZ' => '',
     'NUM_MAE' => '',
     'ANO_MAE' => '',
     'UNIVER_MAE' => '',
     'PAIS_MAE' => ''
);

$this->session->unset_userdata($empty_session);

$this->session->set_userdata(array('author_id' => is_numeric($this->uri->segment(3)) ? $this->uri->segment(3) : $this->uri->segment(4)));
$this->author_id = $this->session->userdata('author_id');
    }
}
Now see this other code:
Code:
public function ppi() {
$this->data_view['author_id'] = $this->author_id;
$this->data_view['_ppi_data'] = $this->Mauthor->getPPI($this->author_id);

$this->session->set_userdata($this->data_view['_ppi_data']);

$this->data_view['author'] = $this->Mauthor->getOne($this->author_id);
$this->data_view['ppi_data'] = $ppi_data = $this->Mauthor->getFieldsName('autores_ppi');

$this->load->library('form_validation');
$this->form_validation->set_rules('NIVEL_ING', 'Nivel Ingenieril', 'trim|required|numeric|min_length[1]|max_length[3]');

$data = array();
$int_values = array('ID_AUTOR', 'NIVEL_ING', 'COHORTE', 'PSEUDO_COH', 'PUB_PPI', 'ANO_EGR', 'ANO_REING', 'ANO_LIC', 'ANO_MA_PSG', 'DOCTORADO', 'ANO_DOC', 'NUM_MAE', 'ANO_MAE');
if ($this->form_validation->run()) {
     foreach ($_POST as $value => $key) {
  if (in_array($value, $int_values)) {
      $data[$value] = $key != "" ? (int) $key : (int) "";
  } else {
      $data[$value] = $key != "" ? $key : "";
  }
     }

     if ($this->Mauthor->addPPI($data)) {
  $this->session->set_flashdata('flashdata', 'El registro fue actualizado satisfactoriamente!');
  redirect('cauthor/update/' . $this->session->userdata('author_id'), 'refresh');
     } else {
  $this->session->set_flashdata('flashdata', 'Ocurrieron errores mientras se agregaba el registro!');
  redirect('cauthor/ppi/' . $this->session->userdata('author_id'));
     }
}

$this->load->view('author/author_ppi', $this->data_view);
    }
When I call the function for first time all values are OK but if I send the form instead then I lost $this->author_id value so it's redirected to the wrong page and yes the cause is $this->author_id is build every time the page is loaded or any function is called due to __construct(). How did yours deal with this?

Any help?
#2

[eluser]nishant1234[/eluser]
session is not good for use so use meta tag for redirecting.
#3

[eluser]CroNiX[/eluser]
Aren't you already in that controller? Why redirect? Just go to the appropriate method.

instead of
Code:
redirect('cauthor/ppi/' . $this->session->userdata('author_id'));

why not
Code:
$this->ppi($this->session->userdata('author_id'));
of course then you'd slightly need to adjust ppi, like:
Code:
function ppi($author_id = 0)

you really only need to redirect if you are going to a different controller...
#4

[eluser]CroNiX[/eluser]
And $author_id would automatically be passed to the method (just like CI does for all parameters beyond controller/method)
#5

[eluser]ReyPM[/eluser]
[quote author="CroNiX" date="1355853622"]Aren't you already in that controller? Why redirect? Just go to the appropriate method.

instead of
Code:
redirect('cauthor/ppi/' . $this->session->userdata('author_id'));

why not
Code:
$this->ppi($this->session->userdata('author_id'));
of course then you'd slightly need to adjust ppi, like:
Code:
function ppi($author_id = 0)

you really only need to redirect if you are going to a different controller...[/quote]

Hi, CroNiX and thanks for your suggestion of course make sense to do things as you said before so I´ll try this way. Thanks a lot




Theme © iAndrew 2016 - Forum software by © MyBB