CodeIgniter Forums
Add multiple values from DB field to session. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Add multiple values from DB field to session. (/thread-48172.html)



Add multiple values from DB field to session. - El Forum - 01-06-2012

[eluser]Unknown[/eluser]
**UPDATE**
I'm updating how I'm asking this question. Trying to make it a little more clearn

The problem

This is for a multipart form. I need to validate multiple fields (serial numbers) against one column in a table. If those serial numbers exist in the column they are added to a session so that on the next step of the form they can be pulled into the values of hidden form fields and submitted with the user registration information. This way I can record the serial numbers along with the user information into a new table.

Below is the code for my MVC that I have so far. The process seems to work just fine with only one serial number, but not when trying to add more than one. IE. I can't seem to validate multiple serials with a database column and pass them to the session.

I would appreciate any help that anyone may have on the matter.


Model

Code:
class M_register extends CI_Model
{
function register($number)
{
  $this -> db -> select('id, number');
  $this -> db -> from('reg_numbers');
  //$this -> db -> where_in('number = ' . "'" . $number . "'");
  $this->db->where_in('number', $number);
  //$this -> db -> limit(1);
  
  
  $query = $this -> db -> get();

  if($query -> num_rows() > 0)
  {
   return $query -> result();
  }
  else
  {
   return false;
  }


}


}

Controller 1 Form step 1 - submit multiple serial numbers, validate against existing database, and add to session for step 2

Code:
class Register_one extends CI_Controller
{

function __construct()
  {
   parent::__construct();
   $this->load->model('m_register','',TRUE);
}

function index()
{
   //This method will have the credentials validation
   $this->load->library('form_validation');

   $this->form_validation->set_rules('number', '', 'required|xss_clean|callback_check_database');
   //$this->form_validation->set_rules('number_two', 'Invalid ID', 'trim|xss_clean|callback_check_database');

   if($this->form_validation->run() == FALSE)
   {
     //Field validation failed.  User redirected to login page
     $this->load->view('v_register_one');
   }
   else
   {
     //Field validation pass. Go to private area
     redirect('register_two');
   }

}

function check_database($number)
{
   //Field validation succeeded.  Validate against database
   $number = $this->input->post('number');
  // $number_two = $this->input->post('number_two');
      
   //query the database
   $result = $this->m_register->register($number);

   if($result)
   {
     foreach($result as $row)
     {
       $sess_array = array(
         'id' => $row->id,
         'number' => $row->number
       );
       $this->session->set_userdata('logged_in', $sess_array);
     }
     return TRUE;
   }
   else
   {
     $this->form_validation->set_message('check_database', 'Invalid ID');
     //$this->form_validation->set_message('number_two', '', 'Invalid ID');
     return false;
   }
  
}

}

Controller 2 Form step 2 - serial numbers are added to hidden field values and user registration information is submitted to second table

Code:
session_start(); //we need to call PHP's session object to access it through CI

class Register_two extends CI_Controller
{

function __construct()
  {
    parent::__construct();
  }

  function index()
  {
    if($this->session->userdata('logged_in'))
    {
     $session_data = $this->session->userdata('logged_in');
     $data['number'] = $session_data['number'];
      
    //  $this->load->view('v_register_two', $data);
      
      //This method will have the credentials validation
       $this->form_validation->set_rules('name' , 'Name' , 'trim|required|xss_clean|callback_check_database');
       $this->form_validation->set_rules('addyone' , 'Address One' , 'trim|xss_clean|callback_check_database');
       $this->form_validation->set_rules('addytwo' , 'Address Two' , 'trim|xss_clean|callback_check_database');
       $this->form_validation->set_rules('city' , 'City' , 'trim|required|xss_clean|callback_check_database');
       $this->form_validation->set_rules('state' , 'State or Province' , 'trim|required|xss_clean|callback_check_database');
       $this->form_validation->set_rules('country' , 'Country' , 'trim|required|xss_clean|callback_check_database');
       $this->form_validation->set_rules('zip' , 'Zip Code' , 'trim|xss_clean|callback_check_database');
       $this->form_validation->set_rules('phone' , 'Prefered Phone Number' , 'trim|xss_clean|callback_check_database');
       $this->form_validation->set_rules('fax' , 'Fax Number' , 'trim|xss_clean|callback_check_database');
       $this->form_validation->set_rules('email' , 'Prefered Email' , 'required|valid_email|is_unique[users.email]');
       $this->form_validation->set_rules('brand' , 'Gas Brand' , 'trim|xss_clean|callback_check_database');
      
       if ($this->form_validation->run() == FALSE)
        {
         $session_data = $this->session->userdata('logged_in');
         $data['number'] = $session_data['number'];
      
         $this->load->view('v_register_two' , $data);
        }
       else
        {
         $this->db->insert('users' , $_POST);
         redirect('thanks' , 'refresh');
        }
        
    }
    else
    {
      //If no session, redirect to login page
      redirect('register_one', 'refresh');
    }
    
  }

  function cancel()
  {
    $this->session->unset_userdata('logged_in');
    session_destroy();
    redirect('register_one', 'refresh');
  }

}

View 1 Form fields

Code:
<input class="xlarge" id="xlInput" name="number" type="text" value="<?php echo set_value('number'); ?>" />

<input class="xlarge" id="xlInput" name="number_two" type="text" value="<?php echo set_value('number_two'); ?>" />

Thanks for your help in advance. I really appreciate it!