Welcome Guest, Not a member yet? Register   Sign In
Passing a variable on redirect
#1

[eluser]The Revel[/eluser]
What i am trying to do is redirect to the same page after I am done, however the page is called via a variable pulled from a search.

Its a barcode system, you scan the barcode in a search and it comes up with the users page, along with their transactions, you make a new transaction and it should come back to the users page, however, upon redirect I get a blank page because I did not pass the barcode back. Is there a way to pass it via a reditect or some way that i do not have to duplicate my code (which I had done previously, but its just shorter that after you do the DB sequences it redirects instead of re-rendering the page).

Here is the full function, you can see why I don't wanna increase it any more:

Had to break it in two cuz of forum limitations:
Part 1:
Code:
function search_scancode()
    {
        
        if (!$this->ion_auth->logged_in())
  {
   //redirect them to the login page
   redirect('auth/login', 'refresh');
  }
  elseif (!$this->ion_auth->is_admin())
  {
   //redirect them to the home page because they must be an administrator to view this
   redirect($this->config->item('base_url'), 'refresh');
  }
  else
  {
    //validate form input
  $this->form_validation->set_rules('qty1', 'First Quantity', 'required|xss_clean');
  $this->form_validation->set_rules('pr1', 'First Price', 'required|xss_clean');
  $this->form_validation->set_rules('admin', 'Admin ID', 'required|xss_clean');
        $this->form_validation->set_rules('qty2', 'Second Quantity', 'xss_clean');
        $this->form_validation->set_rules('qty3', 'Third Quantity', 'xss_clean');
        $this->form_validation->set_rules('qty4', 'Fourth Quantity', 'xss_clean');
        $this->form_validation->set_rules('qty5', 'Fifth Quantity', 'xss_clean');
        $this->form_validation->set_rules('qty6', 'Sixth Quantity', 'xss_clean');
        $this->form_validation->set_rules('pr2', 'Second Price', 'xss_clean');
        $this->form_validation->set_rules('pr3', 'Third Price', 'xss_clean');
        $this->form_validation->set_rules('pr4', 'Forth Price', 'xss_clean');
        $this->form_validation->set_rules('pr5', 'Fifth Price', 'xss_clean');
        $this->form_validation->set_rules('pr6', '6th price', 'xss_clean');
        
        if ($this->form_validation->run() == true)
  {
      $datestring = "%Y-%m-%d";
            $time = time();
            $date = mdate($datestring, $time);
  
            $new_tr1 = array('date' => $date,
                'qty' => $this->input->post('qty1'),
                'total' => $this->input->post('qty1') * $this->input->post('pr1'),
                'code' => $this->input->post('code1'),
                'admin' => $this->input->post('admin'),
                );
            $this->db->insert('records', $new_tr1);
            if ($this->input->post('qty2'))
            {    
            $new_tr2 = array('date' => $date,
                'qty' => $this->input->post('qty2'),
                'total' => $this->input->post('qty2') * $this->input->post('pr2'),
                'code' => $this->input->post('code2'),
                'admin' => $this->input->post('admin'),
                );
                $this->db->insert('records', $new_tr2);
            }
            if ($this->input->post('qty3'))
            {
            $new_tr3 = array('date' => $date,
                'qty' => $this->input->post('qty3'),
                'total' => $this->input->post('qty3') * $this->input->post('pr3'),
                'code' => $this->input->post('code3'),
                'admin' => $this->input->post('admin'),
                );
                $this->db->insert('records', $new_tr3);
            }
            if ($this->input->post('qty4'))
            {
            $new_tr4 = array('date' => $date,
                'qty' => $this->input->post('qty4'),
                'total' => $this->input->post('qty4') * $this->input->post('pr4'),
                'code' => $this->input->post('code4'),
                'admin' => $this->input->post('admin'),
                );
                $this->db->insert('records', $new_tr4);
            }
            if ($this->input->post('qty5'))
            {
            $new_tr5 = array('date' => $date,
                'qty' => $this->input->post('qty5'),
                'total' => $this->input->post('qty5') * $this->input->post('pr5'),
                'code' => $this->input->post('code5'),
                'admin' => $this->input->post('admin'),
                );
                $this->db->insert('records', $new_tr5);
            }
            if ($this->input->post('qty6'))
            {
            $new_tr6 = array('date' => $date,
                'qty' => $this->input->post('qty6'),
                'total' => $this->input->post('qty6') * $this->input->post('pr6'),
                'code' => $this->input->post('code6'),
                'admin' => $this->input->post('admin'),
                );
                $this->db->insert('records', $new_tr6);
            }
            $total = (($this->input->post('qty1') * $this->input->post('pr1')) + ($this->input->post('qty2') * $this->input->post('pr2')) + ($this->input->post('qty3') * $this->input->post('pr3')) + ($this->input->post('qty4') * $this->input->post('pr4') + ($this->input->post('qty5') * $this->input->post('pr5')) + ($this->input->post('qty6') * $this->input->post('pr6')))) * -1 ;
            
            
            $full_tr = array('email' => $this->input->post('email'),
                'amount' => $total,
                'date' => $date,
                'admin' => $this->input->post('admin'),
            );
            $this->db->insert('transactions', $full_tr);
            
            $this->session->set_flashdata('message', "Transaction(s) Added");
            
            //redirect them to the login page
   redirect('auth/search_scancode', 'refresh');
  }
#2

[eluser]The Revel[/eluser]
Code:
else
         {
          
         $searchcode = $this->input->post('searchcode', true);
         //Query 1 - Search database for user records that match the searchcode
         $q1 = $this->db->from('users')
            ->where('scancode', $searchcode)
            ->get();
         if ($q1->num_rows() > 0)
            {
            $this->data['user'] = $q1->row();
         //Query 2 - Get sum of account from transaction DB  
            $q2 = $this->db->select('SUM(amount) as sum_amount', FALSE)
            ->from('transactions')
            ->where('email', $this->data['user']->email)
            ->group_by('email')
            ->get();
            if ($q2->num_rows() > 0)
            {
             $this->data['sum'] = $q2->row()->sum_amount;  
            }
         //Query 3 - Get the last 3 transaction records for user
            $q3 = $this->db->from('transactions')
            ->where('email', $this->data['user']->email)
            ->order_by('tid','DESC')
            ->limit(3)
            ->get();          
            if ($q3->num_rows() > 0)
            {
            $this->data['amount'] = $q3->result();
            }
            
          // pass new transaction form fields to the view page  
            $this->data['email'] = array('name' => 'email',
    'id' => 'email',
    'type' => 'hidden',
                'value' => $this->data['user']->email,
   );
            $this->data['searchcode'] = array('name' => 'searchcode',
    'id' => 'searchcode',
    'type' => 'hidden',
                'value' => $this->input->post('searchcode', true),
   );    
            $this->data['qty1'] = array('name' => 'qty1',
    'id' => 'qty1',
    'type' => 'text',
                'value' => $this->form_validation->set_value('qty1'),
   );
            $this->data['qty2'] = array('name' => 'qty2',
    'id' => 'qty2',
    'type' => 'text',
                'value' => $this->form_validation->set_value('qty2'),
   );
            $this->data['qty3'] = array('name' => 'qty3',
    'id' => 'qty3',
    'type' => 'text',
                'value' => $this->form_validation->set_value('qty3'),
   );
            $this->data['qty4'] = array('name' => 'qty4',
    'id' => 'qty4',
    'type' => 'text',
                'value' => $this->form_validation->set_value('qty4'),
   );
            $this->data['qty5'] = array('name' => 'qty5',
    'id' => 'qty5',
    'type' => 'text',
                'value' => $this->form_validation->set_value('qty5'),
   );
            $this->data['qty6'] = array('name' => 'qty6',
    'id' => 'qty6',
    'type' => 'text',
                'value' => $this->form_validation->set_value('qty6'),
   );
            $this->data['options'] = array(
                  'B' => 'B/W',
                  'C' => 'Color',
                  'F' => 'Staples',
                  'U' => 'USB',
                  'S' => 'Scanning',
                  'E' => 'Scan Email',
                  'D' => 'Deposit',
                  'R' => 'Refund',
                );
            $this->data['pr1'] = array('name' => 'pr1',
    'id' => 'pr1',
    'type' => 'text',
                'value' => $this->form_validation->set_value('pr1'),
   );
            $this->data['pr2'] = array('name' => 'pr2',
    'id' => 'pr2',
    'type' => 'text',
                'value' => $this->form_validation->set_value('pr2'),
   );
            $this->data['pr3'] = array('name' => 'pr3',
    'id' => 'pr3',
    'type' => 'text',
                'value' => $this->form_validation->set_value('pr3'),
   );
            $this->data['pr4'] = array('name' => 'pr4',
    'id' => 'pr4',
    'type' => 'text',
                'value' => $this->form_validation->set_value('pr4'),
   );
            $this->data['pr5'] = array('name' => 'pr5',
    'id' => 'qty5',
    'type' => 'text',
                'value' => $this->form_validation->set_value('pr5'),
   );
            $this->data['pr6'] = array('name' => 'pr6',
    'id' => 'pr6',
    'type' => 'text',
                'value' => $this->form_validation->set_value('pr6'),
   );
            $this->data['admin'] = array('name' => 'admin',
    'id' => 'admin',
    'type' => 'text',
                'value' => $this->form_validation->set_value('admin'),
   );
          
            $this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));

            $this->template
             ->title( 'Last Minute Printing', 'Print Card')
             ->set_layout('default')
             ->build('auth/scan_view', $this->data);
             }
            }
      }
              
    }
#3

[eluser]The Revel[/eluser]
Now I do realize I could have simplified my code even more by using arrays and such, but to be honest, I did it in a way I felt comfortable. Bit in order to render the page I need to pass the Barcode on refresh.
#4

[eluser]BrokenLegGuy[/eluser]
uhh, I'm not going to read through all that Smile

Take a look at the example included in Ion Auth System to see how messages are passed around from page to page

http://ellislab.com/forums/viewthread/145263/

I use the same method, which is to put variable(s) in flashdata

http://ellislab.com/codeigniter/user-gui...sions.html
#5

[eluser]The Revel[/eluser]
Ok, I managed to do it this way:

In the top part, I added the value to the session data:

Code:
//redirect them to the login page
            $this->session->set_userdata('searchcode', $this->input->post('searchcode', true));
            
   redirect('auth/search_scancode/', 'refresh');

So that when it redirects it checks for the post variable, if it doesn;t exist grab it from the session, and then delete the session:

Code:
if ($this->input->post('searchcode', true))
         {
          $searchcode = $this->input->post('searchcode', true);  
         }
          else
          {
            $searchcode = $this->session->userdata('searchcode');
            $this->session->unset_userdata('searchcode');
          }

This seems to work ok.
#6

[eluser]ojcarga[/eluser]
Cool, just a little tip:

When you use
Code:
set_userdata()
you dont need to unset it, CI does it automatic.
#7

[eluser]The Revel[/eluser]
Then I have one quick questions, How do you set a session that does not expire till they log out? In another part of the code I need to set a session variable that will be needed throught the users session (storeID) which governs which information they can see.
#8

[eluser]The Revel[/eluser]
ojcarga - I thnk you mean flashdata and not userdata

According the the userguide
Code:
$this->session->set_flashdata('item', 'value');
will only be available for the next server request
Gonna change my code to Flashdata.
#9

[eluser]ojcarga[/eluser]
Sure, sorry, my mistake, I meant "set_flashdata()",,, it is useful.

For your last question I think you got the response, right?

Code:
//SET DATA TO THE SESSION
$this->session->set_userdata('some_name', 'some_value');

//RETRIEVE DATA FROM THE SESSION
$this->session->userdata('item');

//DESTROY THE SESSION WHEN THE USER HIT LOGOUT BUTTON
$this->session->sess_destroy();
#10

[eluser]The Revel[/eluser]
Thanks for the Help Ojcarga, yeah I figures out the answer once I read teh difference between flashdata and userdata.




Theme © iAndrew 2016 - Forum software by © MyBB