简体   繁体   中英

How to preserve information when loading controller codeigniter?

I'm doing a proyect in Codeigniter. One of the views that I have requires to select a student and a account. The student and the account are in different views and controllers. I select the student successfully, and I fill some fields with the student information, but then I want to select the account and it fill the fields for the account, but the information for the student dissapears.

Here is the view that I'm working on:

<?php

    /*******Information for the student ********/
    $parametroExpediente = "";
    $parametroNombre = "";
    $parametroPrimerApellido = "";
    $parametroSegundoApellido = "";
    $parametroCedula = "";


    if (isset($expediente))
    {
        $parametroExpediente = set_value('expediente', $expediente);
        $parametroNombre = set_value('nombre', $nombre);
        $parametroPrimerApellido = set_value('primerApellido', $primerApellido);
        $parametroSegundoApellido = set_value('segundoApellido', $segundoApellido);
        $parametroCedula = set_value('cedula', $cedula);
    }
    /************************************/

    /*******Information for the account********/
    $parametroIDPlan = "";
    $parametroDescripcionPlan = "";
    $parametroTotalAPagarPlan = "";
    $parametroCuantosPagosPlan = "";
    $parametroFrecuenciaPagoPlan = "";
    $parametroAcivoPlan = "";
    $parametroIDCursoPlan = "";
    $parametroMultiGrupoPlan = "";

    if (isset($idPlan))
    {
        $parametroIDPlan = set_value('idPlan', $idPlan);
        $parametroDescripcionPlan = set_value('descripcion', $descripcion);
        $parametroTotalAPagarPlan = set_value('totalApagar', $totalApagar);
        $parametroCuantosPagosPlan = set_value('cuantosPagos', $cuantosPagos);
        $parametroFrecuenciaPagoPlan = set_value('cadaCuantosDiasPaga', $cadaCuantosDiasPaga);
        $parametroAcivoPlan = set_value('activo', $activo);
        $parametroIDCursoPlan = set_value('idCurso', $idCurso);
        $parametroMultiGrupoPlan = set_value('multiGrupo', $multiGrupo);    
    }
    /************************************/
?>

<!--BUTTON TO SELECT A STUDENT -->
<a href="http://localhost/ProyectoNetbeans/CodeIgniter_2.1.3/index.php/Alumnos_controller/loadAlumnoView/seleccionarParaCuenta" class="btn btn-primary">Seleccionar alumno</a>
<span class="badge badge-info">Expediente: <?php echo $parametroExpediente ?></span>
<!---------------------------------->
<br/>
<br/>
<br/>

<!--BUTTON TO SELECT AN ACCOUNT -->
<a href="http://localhost/ProyectoNetbeans/CodeIgniter_2.1.3/index.php/PlanDePago_controller/loadPlanDePagoView/seleccionarParaCuenta/" class="btn btn-primary">Seleccionar plan de pago</a>
<span class="badge badge-info">Plan escogido: <?php echo $parametroDescripcionPlan ?></span>

When the student and the account are selected, they both redirect their information to the same controller, which is the following:

<?php
class Cuentas_controller extends CI_Controller {

   public $data = array();

   public function __construct()
   {
        parent::__construct();
        // Your own constructor code

        $this->load->helper('url');

        $this->load->helper('form');

        $this->load->helper('url');

        $this->load->model('Alumnos_model');
        $this->load->model('PlanDePago_model');

    }

   function index()
   {
        $this->load->view('cuentas_view');
   }

   /*Take the information for the student*/
   function setStudent($elExpediente)
   {
       $query = $this->Alumnos_model->getAllData($elExpediente);
       foreach ($query->result_array() as $row)
       {
           $this->data = array(
                //'expediente' => $row['expediente'],
                'expediente' => $elExpediente,
                'primerApellido' => $row['primerApellido'],
                'segundoApellido' => $row['segundoApellido'],
                'nombre' => $row['nombre'],
                'cedula' => $row['cedula']
            );
       }
       $this->load->view('cuentas_view',$this->data);
   }

   /*Take the information for the account*/
   function setAccount($id)
   {           
       $query = $this->PlanDePago_model->getAllDataNoParameters($id);
       foreach ($query->result_array() as $row)
       {
           $this->data = array(
                'idPlan' => $id,
                'totalApagar' => $row['totalApagar'],
                'descripcion' => $row['descripcion'],
                'cuantosPagos' => $row['cuantosPagos'],
                'cadaCuantosDiasPaga' => $row['cadaCuantosDiasPaga'],
                'activo' => $row['activo'],
                'idCurso' => $row['idCurso'],
                'multiGrupo' => $row['multiGrupo']
            );
       }
       $this->load->view('cuentas_view',$this->data);
   }
}

?>

As you can see, the setStudent and setAccount methods take the information sent by those views, and then open the view that I'm working on and fill some information, but it's not working as expected, the first that I selected dissapears when I select the second one.

Some suggestion to get this working? I want both student and account showing information when I select them.

You don't save Expediente. In my example, we save $elExpediente and $idPlan in session variable, and redirect to view. Then in $this->load->view('cuentas_view') we load data about this Expediente and idPlan in view from models as one row each.

Try like this, think I helped you.

Controller

class Cuentas_controller extends CI_Controller 
{
   public function __construct()
   {
        parent::__construct();

        $this->load->helper('url');
        $this->load->helper('form');
        $this->load->helper('url');

        $this->load->model('Alumnos_model');
        $this->load->model('PlanDePago_model');

    }

    function index()
    {
         $this->load->view('cuentas_view', array(
             'elExpediente' => $this->session->userdata('elExpediente'),
             'idPlan'       => $this->session->userdata('idPlan'),
         ));
    }

    function flashParams()
    {
        // to erase session data elExpediente and idPlan
        $this->session->unset_userdata('elExpediente');
        $this->session->unset_userdata('idPlan');
    }

    function setAccount($id)
    {
        $this->session->set_userdata('idPlan', $id);
        redirect('http://localhost/ProyectoNetbeans/CodeIgniter_2.1.3/index.php/Cuentas_controller');
    }

    function setStudent($elExpediente)
    {
        $this->session->set_userdata('elExpediente', $id);
        redirect('http://localhost/ProyectoNetbeans/CodeIgniter_2.1.3/index.php/Cuentas_controller');
    }   
}

Alumnos_model:

class Alumnos_model extends CI_Model
{
    public function getItem($elExpediente)
    {
        $this->db->select('*');
        $this->db->where('someColumn', $elExpediente);
        $this->db->limit(1);
        $this->db->from('someTable');
        return $this->db->get()->row_array();
    }
}

PlanDePago_model:

class PlanDePago_model extends CI_Model
{
    public function getItem($id)
    {
        $this->db->select('*');
        $this->db->where('someIdColumn', $id);
        $this->db->limit(1);
        $this->db->from('someTable');
        return $this->db->get()->row_array();
    }
}

cuentas_view.php file:

<?php

    /*******Information for the student ********/
    $parametroExpediente      = "";
    $parametroNombre          = "";
    $parametroPrimerApellido  = "";
    $parametroSegundoApellido = "";
    $parametroCedula          = "";

    /*******Information for the account********/
    $parametroIDPlan             = "";
    $parametroDescripcionPlan    = "";
    $parametroTotalAPagarPlan    = "";
    $parametroCuantosPagosPlan   = "";
    $parametroFrecuenciaPagoPlan = "";
    $parametroAcivoPlan          = "";
    $parametroIDCursoPlan        = "";
    $parametroMultiGrupoPlan     = "";


    if($row = $this->Alumnos_model->getItem($elExpediente))
    {
        $parametroExpediente      = set_value('expediente',      $elExpediente);
        $parametroNombre          = set_value('nombre',          $row['nombre']);
        $parametroPrimerApellido  = set_value('primerApellido',  $row['primerApellido']);
        $parametroSegundoApellido = set_value('segundoApellido', $row['segundoApellido']);
        $parametroCedula          = set_value('cedula',          $row['cedula']);
    }

    if($row = $this->PlanDePago_model->getItem($idPlan))
    {
        $parametroIDPlan             = set_value('idPlan',              $idPlan);
        $parametroDescripcionPlan    = set_value('descripcion',         $row['descripcion']);
        $parametroTotalAPagarPlan    = set_value('totalApagar',         $row['totalApagar']);
        $parametroCuantosPagosPlan   = set_value('cuantosPagos',        $row['cuantosPagos']);
        $parametroFrecuenciaPagoPlan = set_value('cadaCuantosDiasPaga', $row['cadaCuantosDiasPaga']);
        $parametroAcivoPlan          = set_value('activo',              $row['activo']);
        $parametroIDCursoPlan        = set_value('idCurso',             $row['idCurso']);
        $parametroMultiGrupoPlan     = set_value('multiGrupo',          $row['multiGrupo']);    
    }
?>

your html code here

not sure in set_value, i'm never use it

I'm posting this answer just to take out some of my Stackoverflow points:

Codeigniter is not designed to understand Spanish variables.

There you go...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM