• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how do i pass an object or array to controller function

[eluser]Salvo P[/eluser]
i have a view which has displays a result set (contained in an object array which was correctly passed from a controller). I have an anchor on the page to show the data in another view (i.e. as locations plotted on google maps).

So the anchor links to another controller function which then does the appropriate prep and calls the appropriate view with the same resultset

But how do i get my resultset to the new contoller? If I append it to the controller functions URL in the anchor declaration, all the controller function recieves in the parameter is the word 'array', not the contents.

Am I missing something obvious? Surely I don't have to re-generate my result set in the new controller, which is processer intensive? And I couldn't anyway without recapturing the users original input that was used to create the result set.

Help appreciated.

In your Controller...

... ... ...
//Populate your result set. I assumed that you populate the result set in $query.

$data = array(
'query' => $query
... ... ...

$this->load->view('view_name', $data);

In your view...
... ... ...
//use normally $query variable.

I hope this will be your answer.

[eluser]Salvo P[/eluser]
Thanks for the reply. I am aware of how to pass data from a controller to a view, it is the other way that causes me a problem. I don't think I explained my issue very well.

So, I have controllerA, controllerB, view1, view2

view1 and view2 are different views of the same data.

So, controllerA processes a search request and then loads view1.
view1 displays the data fine. It also has a link (to controllerB) for a user to see the same data displayed differently.

So controllerB loads view2 (with the same data) to display it differently.

But how do I get the data (from view1) to controllerB?

You can pass in paramaters to the controller by appending them to the URI in the anchor declaration, but only if the paramater is a string.

I do not want controllerB to have to reprompt ther user and do all the search processing again when I already have the results in an array.


Try this in your view!
echo anchor("controller/function/$param", $textname);


[eluser]Salvo P[/eluser]
Ah, should that work? I tried it and still get a problem.

When I try
echo anchor("controller/function/$param", $textname);

I get the same as before. i.e In my controller, $param just contains the string literal 'Array'. Not the values. This is what I was trying before I posted here. i.e.
echo anchor("controller/function".$param, $textname);
Same problem.

Interestingly, if I try single quotes
echo anchor('controller/function/$param', $textname);

I get this error
"The URI you submitted has disallowed characters."

In case it matters, my $param represents a resultset. It is an array, each element of the array being an object representing data from one DB record.

controller is your controller name.
function is your controller name and funtion ie. ( index.php )
$param should be the name of your object or array!

echo anchor("controller/function/".$param, $textname);

Also your receiving function needs to accept and object or array.

Can you show your controller and view code?


[eluser]Salvo P[/eluser]

This is the link in my view.

echo anchor("things/gmap_all_results/$things", "See map of all results");

Obviously $things is the array of objects. And yes, things is my controller and gmap_all_results is the function within it.

So in the controller I have
function gmap_all_results($things)
        $data['title'] = "Map";
        $data['headerjs'] = $this->gmap->getHeaderJS();
        $data['headermap'] = $this->gmap->getMapJS();
        $data['onload'] = $this->gmap->printOnLoad();
        $data['map'] = $this->gmap->printMap();
        $data['sidebar'] = $this->gmap->printSidebar();

Note this isn't the completed function, as it will loop through $things to get all the postscodes. Once I can actually pass $things in!!
Remember, the code function gets called fine, it's just that when I look at $things it only contains the literal 'Array'. So this line then fails

Thanks for your help so far.

[eluser]Federico Baña[/eluser]
you shouldnt be passing that information via url.
i'd use javascript somehow to change the display of the information, but if that's not a solution i'd try something like this http://ellislab.com/codeigniter/user-gui...ching.html.
you have to run the query again and the data array must come from the controller, otherwise you could be having a security hole.

[eluser]Federico Baña[/eluser]
or else
echo anchor("controller/method/" . implode('/', $things));

[eluser]Salvo P[/eluser]
implode doesn't really work for me. oh well guess CI just doesn't support it. i'll think of a way around it.

Thanks for the help anyway guys.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.