17488 Posts in 4473 Topics by 1978 members
| Go to End | Next > | |
| Author | Topic: | 5839 Views |
-
Pass Variables in a Query String?

9 August 2008 at 7:35am
Hi,
What is the best way to pass variables from page to page (template to template) in Silverstripe?
I'm on a page which lists clients, and I want to click on the client and pass that client's name to a page whose controller takes the name and get all the projects for that particular client from the database.
The link is "projects-by-client?Client=Client1". In my ProjectsByClient controller, I have the following function:
function ProjectsByClient($Client) {
$whereStatement = "ProjectClient = '".$Client."'";
return DataObject::get("ProjectPage", $whereStatement);
}Thwe query works fine if you hard-code a client name. But my controller is saying "missing argument" when I try to pass it in dynamically. How can I pass the client name to the controller?? Seems like this should be easy.
Thanks in advance,
Garrett -
Re: Pass Variables in a Query String?

9 August 2008 at 8:40am
It doesn't look like your getting or setting $Client in the get request there.
retrieve the get variable and set it with the following code
$Client = Director::urlParam('client')
-
Re: Pass Variables in a Query String?

9 August 2008 at 8:56am
Hi,
Thanks so much for your reply. I am using your code ($Client = Director::urlParam('Client');) and am getting the follownig error:
Parse error: syntax error, unexpected T_VARIABLE, expecting T_FUNCTION in C:\wwwroot\wamp\www\silverstripe-v2.2.2\mysite\code\ProjectsByClient.php on line 26
Any ideas?
Thanks again,
Garrett -
Re: Pass Variables in a Query String?

9 August 2008 at 9:16am Last edited: 9 August 2008 9:16am
make sure you're using Director:: in the controller.
EDIT:// and check for simple syntax errors before line 26.
-
Re: Pass Variables in a Query String?

9 August 2008 at 9:20am
Hello again--
What do you mean by "using" Director:: in the controller? Do I need to instantiate that in some way, or...? And there is no other code in the controller, by the way-- if I take out the line you sent me the error goes away.
Thanks,
Garrett -
Re: Pass Variables in a Query String?

9 August 2008 at 9:27am Last edited: 9 August 2008 9:28am
for instance if you put this code on HomePage.php it would need to go inside of the page_controller of hompage.php
class HomePage_Controller extends Page_Controller {
function ProjectsByClient($Client) {
if ($Client = Director::urlParam('Client')){
$whereStatement = "ProjectClient = '".$Client."'";
}
return DataObject::get("ProjectPage", $whereStatement);// End of controller
}Good Luck
-
Re: Pass Variables in a Query String?

9 August 2008 at 10:15pm
Director::urlParam('Client'))?
I dont think thats correct as the default URL arrays is something like /Action/ID/OtherID. Also in the If you are using = rather then == so it will always return true, And since you are going to be inserting the URL parameter into a SQL query you need to make sure its safe! so what you would have is
function client() {
if (isset(Director::urlParam('ID'))){
$where = Convert::raw2SQL(Director::urlParam('ID'));
$whereStatement = "ProjectClient = '". $where ."'";
}
return DataObject::get("ProjectPage", $whereStatement);}and you would go mysite.com/home/client/Bob and it will list the projects where client = bob. The problem is that it doesnt work if you have spaces in the client name - as you are passing the name via the URL. So you might need to use the ID number of the client rather then the name.
-
Re: Pass Variables in a Query String?

12 August 2008 at 1:19am
Hi-- thanks for chiming in, willr. However, it's still not working. Here is my code, converted from yours:
function ProjectsByClient() {
if (isset(Director::urlParam('ClientID'))) {
$where = Convert::raw2SQL(Director::urlParam('ClientID'));
$whereStatement = "ID = '". $where ."'";
}
return DataObject::get("ProjectPage", $whereStatement);
}My URL is: portfolio-projects-by-client?ClientID=20
Here is the error I am getting:
Fatal error: Can't use function return value in write context in ...\ProjectsByClient.php on line 34.
Line 34 is this one:
if (isset(Director::urlParam('ClientID'))) {
Any ideas?
Thanks again,
Garrett
| 5839 Views | ||
| Go to Top | Next > |



