Welcome Guest, Not a member yet? Register   Sign In
Pass variable from view to controller
#1

[eluser]besomuk[/eluser]
I want to pass some variables to my controller.
Lets say i have db query, function to show all data, and function to show some data (using where clause).
So, i want to make URL (or whatever) in my view and tell controller parameter i need for function that will retreive data i want.
#2

[eluser]Sarfaraz Momin[/eluser]
Well there are different ways of doing it. Like how you want to do it. You can use Ajax and do it. Where you can pull data using post of even URI segments. Gimme some more details about how you really want it which would make it more clear to help u.
#3

[eluser]besomuk[/eluser]
Somethin like this.
Controller: main.php
View: main_view.php
I have alphabet list, and when i click on a letter i want to pass that argument back to main.php and use it in db query.
#4

[eluser]mattthehoople[/eluser]
you could pass your array of results into your view and foreach through them...

<?php foreach ($result_passed_in_from_controller){?>
<a href="pick/&lt;?=$result_id;?&gt;">&lt;?=$result_name;?&gt;</a>
&lt;?php }?&gt;

then in the controller...

function pick($result_id){
//rest of your function

}
#5

[eluser]besomuk[/eluser]
I tried that already, but i get 404 error for that URL.
#6

[eluser]xwero[/eluser]
If pick is your controller you have to use a route to add the method, index is most common, because CI expects a controller/method url format.
#7

[eluser]besomuk[/eluser]
Ok, i solved problem.
I was playing with URI's, and i forgot to include index.php in it.
Passing arguments to index () and parsing variables with uri->segment() works ok.
#8

[eluser]mattthehoople[/eluser]
the method is at (something like)

http://localhost/yourproject/index.php/main/pick/

so any of the following could sort it out...

1. change link to <a >&lt;?=$result_name;?&gt;</a>
2. define a base at the top of your document &lt;base href="http://localhost/yourproject/index.php/" &gt;&lt;/base> and change the link to main/pick/

I'm sure this isn't the "correct" way to do it, (routes is one way, .htaccess another) but it should make it work with a bit of playing about...




Theme © iAndrew 2016 - Forum software by © MyBB