Session or Global Variable

Associate
Joined
19 Jul 2006
Posts
1,847
Im using codeigniter to make an app and want to know which is the best way to do this.

Basically I have a user log on then they create a 'client' and then input different forms for the client into different table.

My first idea was when they created a client I store the ClientID in a session variable that I could then use with my sql to make sure its input into the table.
This would work and I could make the user go through all the forms in sequence.

However the user would probably log on at the beginning of the day and work on lots of clients. I take it I could update the session variable to reflect each new client in turn.

But there will be times where they dont have to create a new client they just have to add a record for a existing one, or update an existing record.

What would be the sensible way of making this?
 
Associate
OP
Joined
19 Jul 2006
Posts
1,847
I think I understand what your saying but Im just a bit to thick to implement it.

So my first createclient controler i have
Code:
// build array for the model
		$UID = $this->session->userdata('UserID');
		$CID = 'ClientID';
		$table = 'Client';
		$form_data = array(
					    'ClientID' => set_value('ClientID'),
					     'FirstName' => set_value('FirstName'),
					     'LastName' => set_value('LastName')
					);
					
			// run insert model to write data to db
		
			if ($this->newformadd_m->SaveForm($table, $form_data) == TRUE) // the information has therefore been successfully saved in the db
			{
				redirect('newbenefit_c/index');   // or whatever logic needs to occur
			}

So the formdata gets the data from the form first of all how do get the ClientID out of this then how would i pass it to the index function of the newbenifit_c controler.
 
Associate
OP
Joined
19 Jul 2006
Posts
1,847
Basically what I think im doing is when the user logs on I create a session variable called UserID this is the ID of the user and I have a field in everytable that is called UpdateUserID this is so we can track which user entered the information or updated it.


PHP:
<?php

class Newclient_c extends CI_Controller {
               
	function __construct()
	{
 		parent::__construct();
		$this->load->library('form_validation');
		$this->load->database();
		$this->load->helper('form');
		$this->load->helper('url');
		$this->load->model('newformadd_m');
	}	
	function index()
	{			
		$this->form_validation->set_rules('ClientID', 'ClientID', 'required|trim|xss_clean|max_length[11]');			
		$this->form_validation->set_rules('FirstName', 'FirstName', 'required|trim|xss_clean|max_length[50]');			
		$this->form_validation->set_rules('LastName', 'LastName', 'required|trim|xss_clean|max_length[50]');			
		$this->form_validation->set_rules('NationalInsuranceNumber', 'NationalInsuranceNumber', 'required|trim|xss_clean|max_length[9]');			
		$this->form_validation->set_rules('ClientReference', 'ClientReference', 'required|trim|xss_clean|max_length[50]');			
		$this->form_validation->set_rules('Gender', 'Gender', 'required|trim|xss_clean|max_length[60]');			
		$this->form_validation->set_rules('MaritalStatus', 'MaritalStatus', 'required|trim|xss_clean|max_length[50]');			
		$this->form_validation->set_rules('ContactNumber', 'ContactNumber', 'required|trim|xss_clean|max_length[20]');			
		$this->form_validation->set_rules('Nationality', 'Nationality', 'required|trim|xss_clean|max_length[50]');			
		$this->form_validation->set_rules('Ethnicity', 'Ethnicity', 'required|trim|xss_clean|max_length[50]');			
		$this->form_validation->set_rules('ClientNotes', 'ClientNotes', 'trim|xss_clean');
			
		$this->form_validation->set_error_delimiters('<br /><span class="error">', '</span>');
	
		if ($this->form_validation->run() == FALSE) // validation hasn't been passed
		{
			$view['main_content'] = 'newclient_view';
			$this->load->view('includes/template', $view);
		}
		else // passed validation proceed to post success logic
		{
		 	// build array for the model
			$UID = $this->session->userdata('UserID');
			$CID = 'ClientID';
			$table = 'Client';
			$form_data = array(
					       	'ClientID' => set_value('ClientID'),
					       	'FirstName' => set_value('FirstName'),
					       	'LastName' => set_value('LastName'),
					       	'NationalInsuranceNumber' => set_value('NationalInsuranceNumber'),
					       	'ClientReference' => set_value('ClientReference'),
					       	'CreateDate' => (date('Y-m-d H:i:s')),
					       	'Gender' => set_value('Gender'),
					       	'MaritalStatus' => set_value('MaritalStatus'),
					       	'ContactNumber' => set_value('ContactNumber'),
					       	'Nationality' => set_value('Nationality'),
					       	'Ethnicity' => set_value('Ethnicity'),
					       	'ClientNotes' => set_value('ClientNotes'),
					       	'UpdateUserID' => $UID
						);
					
			// run insert model to write data to db
		
			if ($this->newformadd_m->SaveForm($table, $form_data) == TRUE) // the information has therefore been successfully saved in the db
			{
				redirect('newbenefit_c/index');   // or whatever logic needs to occur
			}
			else
			{
			echo 'An error occurred saving your information. Please try again later';
			// Or whatever error handling is necessary
			}
		}
	}

The newformadd_m has this one function in
Code:
function SaveForm($table, $form_data)
	{
		$this->db->insert($table, $form_data);
		
		if ($this->db->affected_rows() == '1')
		{
			return TRUE;
		}
		
		return FALSE;
	}
 
Associate
OP
Joined
19 Jul 2006
Posts
1,847
There is a page and controller before this one where the user logs on thats where i get the UserID from and store it as part of the session. Thats fine as in every insert or update i will need that UserID. The user does not know their UserID this is looked up from database. IT is not included on any forms just something i am adding to the insert statements.

I may be making this more complicated then it is. Basically I want to get away from the user having to enter the clientID into every form they fill out. So when they create a new client I need someway of getting that client id to be passed to each controller so that either i can pre-populate that field in the form so they dont have to enter it. BUT can change it if they need to work on another client.
 
Associate
OP
Joined
19 Jul 2006
Posts
1,847
Thanks for sticking with me inogen. I know what i want it to do but not how to explain it.
Yeah there will be about 7 forms.
But then when they come to the end of the 7th form they will either create a new Client or want to update an existing Client so id have to change the session variable of the clientID.

is that still ok?
 
Back
Top Bottom