php - Where to generate a unique id in codeigniter controller when two buttons should be handled in the same method
one text
My requirement is to generate unique code for each login session which will be used throughout the application cycle. But it should be generated only once after clicking a button which opens the form. I am having a codeigniter 3.11 application where in i have a form with 2 submit buttons. I am handling the action based on buttonid with $this->input->post('buttonid') for each of 2 buttons. Both buttons are handled using if condition under index() method. I want to generate unique id only the very first time the user comes to index method. This index function is being called once when page is opened by clcking 'Add New' button from another view page. Later on click of buttonid1 and buttonid2 buttons from current view, index method is called with appropriate block being executed. Due to application logic same method keeps called multiple times and one of the two buttons need to handle the action. So generating code inside if($this->input->post('buttonid')) condition doesn't serve my purpose as it will be called multiple times. So I am not sure where to call the unique code generating function so that it servers my above mentioned purpose. For clarity here i am giving pseudo code of my controller
function index() {
// some basic code
///-- place 1
if(user logged in ) {
if($this->input->post('buttonid1'))
{
///-- place 2
// do task assigned to button id 1
//load updated view1
}
else if($this->input->post('buttonid2'))
{
///-- place 3
// do task assigned to button id 2
//load updated view2
}
}
else
{
// redirect to page asking to login
}
}
In the placed marked as place 1,2 and 3 I can call unique id generating function. But in any place it is being called multiple times but I want to call it only once when the button to call index() function is clicked for the first time. Please help me. Thanks in advance
Source