Welcome Guest, Not a member yet? Register   Sign In
how do i pass an object or array to controller function
#1

[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.
#2

[eluser]Drubo[/eluser]
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.
#3

[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.

Thanks
#4

[eluser]InsiteFX[/eluser]
Try this in your view!
Code:
echo anchor("controller/function/$param", $textname);

InsiteFX
#5

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

When I try
Code:
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.
Code:
echo anchor("controller/function".$param, $textname);
Same problem.


Interestingly, if I try single quotes
Code:
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.
Thanks
Salvo
#6

[eluser]InsiteFX[/eluser]
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!

Code:
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?

InsiteFX
#7

[eluser]Salvo P[/eluser]
Sure.

This is the link in my view.

Code:
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
Code:
function gmap_all_results($things)
    {
        $data['title'] = "Map";
        $this->load->library('GMap');
        $this->gmap->GoogleMapAPI();
        $this->gmap->setMapType('map');
        $this->gmap->addMarkerByAddress($things[0]->postcode);
        $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();
        $this->load->view('v_gmap',$data);
    }

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
Code:
$this->gmap->addMarkerByAddress($things[0]->postcode);


Thanks for your help so far.
Salvo
#8

[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.
#9

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

[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.
salvo




Theme © iAndrew 2016 - Forum software by © MyBB