Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Pass Variables in a Query String?

Our old forums are still available as a read-only archive.

Moderators: martimiz, Sean, biapar, Willr, Ingo, simon_w

Page: 1 2
Go to End
Author Topic: 6828 Views
  • Garrett
    Avatar
    Community Member
    245 Posts

    Pass Variables in a Query String? Link to this post

    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

  • vstrazz
    Avatar
    Community Member
    63 Posts

    Re: Pass Variables in a Query String? Link to this post

    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')

  • Garrett
    Avatar
    Community Member
    245 Posts

    Re: Pass Variables in a Query String? Link to this post

    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

  • vstrazz
    Avatar
    Community Member
    63 Posts

    Re: Pass Variables in a Query String? Link to this post

    make sure you're using Director:: in the controller.

    EDIT:// and check for simple syntax errors before line 26.

  • Garrett
    Avatar
    Community Member
    245 Posts

    Re: Pass Variables in a Query String? Link to this post

    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

  • vstrazz
    Avatar
    Community Member
    63 Posts

    Re: Pass Variables in a Query String? Link to this post

    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

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: Pass Variables in a Query String? Link to this post

    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.

  • Garrett
    Avatar
    Community Member
    245 Posts

    Re: Pass Variables in a Query String? Link to this post

    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

    6828 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.