2

I want to send an array using a variable from view to controller, so that I can use it to get some data from database according to that array! For example, in my view I will have a variable with different equations like:

$data = 'setter';

and somewhere else:

$data = 'libero';

Then in my controller I will have a code like:

if($query = $this->players_model->get_players(array('player_Position' => $data, 'limit' => 3))) { $data['players'] = $query; }

what should I do to get it work?!

5
  • Use a form to send the values from your View to Controller. Suggest that you check out this tutorial series net.tutsplus.com/sessions/codeigniter-from-scratch Commented Jul 31, 2013 at 20:55
  • 1
    Whatever you are trying to achieve passing an array from a view to a controller is un-logic , check better what a MVC is ;) Commented Jul 31, 2013 at 20:57
  • I just wanted to shorten the ways! thanks for informing me of this :) Commented Jul 31, 2013 at 21:14
  • @cartalot, I don't wanna use form in my view file! and that's why I was looking for another way! Commented Jul 31, 2013 at 21:46
  • ok then you want jquery ! that will send values from View to Controller. google for tutorials: Codeigniter jquery - use their code to learn, then modify for your project. Commented Aug 1, 2013 at 20:09

2 Answers 2

3

first i would like to advise you , that whatever you are trying to achieve, the "pass an array from a view to a controller" is wrong , MVC pattern is not standing for passing data from view to a controller.

Then, i'l do like this:

//in view
$data = json_encode($myArray);

//in controller

$array = json_decode($data);
Sign up to request clarification or add additional context in comments.

Comments

1

I would use session userdata

View

$data = array(
    'setter'  => 'value',
    'libero'  => 'value'
);
$this->session->set_userdata($data);

Controller

$setter = $this->session->userdata('setter');
$libero = $this->session->userdata('libero');

or

$data['setter'] = $this->session->userdata('setter');
$data['libero'] = $this->session->userdata('libero');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.