User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 423,502 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,679 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 497 | Replies: 3 | Solved
Reply
Join Date: Jun 2008
Posts: 45
Reputation: kvdd is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 2
kvdd kvdd is offline Offline
Light Poster

If else statement in a 'closed' code

  #1  
Jun 9th, 2008
Hallo everyone,

Im new to this forum, but not new to PHP, I do a lot programming in it, but sometimes I have to less knowledge to solve the problems I get.

The title is not very explained what my problem is, so mods, edit it if you want.

The problem
The goal I currently aim in the code I write is to build a dynamic query. Dynamic, because the user can choose to want other output or values.
To archive my goal, I found this query builder: http://code.google.com/p/mqb/ and I found this one working very well. (Found more query builders, but this one is the only one - i think - that works for me)

In this code I want my if else:
  1. $query = query::select()
  2. ->field("u.u_id")->as("id")
  3. ->field("u.uren_begin")->as("begin")
  4. ->field("u.uren_eind")->as("eind")
  5. ->field("l.u_voornaam")->as("voornaam")
  6. ->field("loc.loc_naam")->as("vestiging")
  7. ->from("uren")->as("u")
  8. ->join
  9. (
  10. query::select()
  11. ->from("login")->as("l")
  12. ->where("l.u_id")->equals(new field("u.u_id"))
  13. )
  14. ->join
  15. (
  16. query::select()
  17. ->from("locaties")->as("loc")
  18. ->where("l.u_locatie")->equals(new field("loc.loc_id"))
  19. );
  20.  
  21. ;

What I want is within this code or else an if else statement, like adding and ->join when some statement is true

Now I show you a sample to add to the above code: (and that is not possible I know, but you know what I want)
  1. if($something) { // when true, add a join
  2. ->join
  3. (
  4. query::select()
  5. ->from("something")->as("s")
  6. ->where("s.usefull")->equals(new field("a.avalue"))
  7. )
  8. }

As you know that would never work. My question is now, can you help me to archive such a manner of if else in this code?

Thanks in advance
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2008
Posts: 45
Reputation: kvdd is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 2
kvdd kvdd is offline Offline
Light Poster

Re: If else statement in a 'closed' code

  #2  
Jun 10th, 2008
Look I can put it in a string, but then it would run you know.

Please can someone look at the code, or give me a usefull hint. Im not a totally newbe.
Reply With Quote  
Join Date: Sep 2005
Posts: 689
Reputation: digital-ether has a spectacular aura about digital-ether has a spectacular aura about 
Rep Power: 6
Solved Threads: 41
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Practically a Master Poster

Re: If else statement in a 'closed' code

  #3  
Jun 10th, 2008
Originally Posted by kvdd View Post
Look I can put it in a string, but then it would run you know.

Please can someone look at the code, or give me a usefull hint. Im not a totally newbe.


I'm not too familiar with PHP5 but it would look to me that the only place you can put conditionals in is the function parameters.

I looked at the source at: http://code.google.com/p/mqb/source/.../query.php?r=3
Looks like you can't pass empty parameters to a join. You could extend the class however, or modify the sql() method to allow empty parameters.

/**
         * Creates and returns the object as sql string. 
         *
         * @param       int             $as
         * @return      string
         */
        public function sql($as = self::SQL_AS_DEFAULT)
                {
                switch ($this->stats["query-type"])
                        {
                        case self::QUERY_TYPE_SELECT:
                                switch ($as)
                                        {
                                        case self::SQL_AS_DEFAULT:
                                                $join = "";
                                                if (isset($this->token["join"]))
                                                        {
                                                        foreach ($this->token["join"] as $query)
                                                                {
                                                                $join .= " " . $query->sql(self::SQL_AS_JOIN);
                                                                }
                                                        }
                                                return "SELECT" . (isset($this->token["flag"]) ? " " . implode(" ", $this->token["flag"]) : "") . " " . implode(", ", $this->token["select"]) . " FROM " . $this->token["from"] . $join . 
                                                        (isset($this->token["where"]) ? " WHERE " . implode(" AND ", $this->token["where"]) : "") . 
                                                        (isset($this->token["orderby"]) ? " ORDER BY " . implode(", ", $this->token["orderby"]) : "") . 
                                                        (isset($this->token["limit"]) ? " LIMIT " . $this->token["limit"] : "");
                                                break;
                                        case self::SQL_AS_JOIN:
                                                return "LEFT JOIN " . $this->token["from"] . (isset($this->token["where"]) ? " ON (" . implode(" AND ", $this->token["where"]) : "") . ")";
                                                break;
                                        }
                                break;
                        case self::QUERY_TYPE_INSERT:
                        case self::QUERY_TYPE_UPDATE:
                        case self::QUERY_TYPE_DELETE:
                                return false;
                                break;
                        }
                }

to

/**
         * Creates and returns the object as sql string. 
         *
         * @param       int             $as
         * @return      string
         */
        public function sql($as = self::SQL_AS_DEFAULT)
                {
                switch ($this->stats["query-type"])
                        {
                        case self::QUERY_TYPE_SELECT:
                                switch ($as)
                                        {
                                        case self::SQL_AS_DEFAULT:
                                                $join = "";
                                                if (isset($this->token["join"]))
                                                        {
                                                        foreach ($this->token["join"] as $query)
                                                                {
                                                                $join .= " " . $query->sql(self::SQL_AS_JOIN);
                                                                }
                                                        }
                                                return "SELECT" . (isset($this->token["flag"]) ? " " . implode(" ", $this->token["flag"]) : "") . " " . implode(", ", $this->token["select"]) . " FROM " . $this->token["from"] . $join . 
                                                        (isset($this->token["where"]) ? " WHERE " . implode(" AND ", $this->token["where"]) : "") . 
                                                        (isset($this->token["orderby"]) ? " ORDER BY " . implode(", ", $this->token["orderby"]) : "") . 
                                                        (isset($this->token["limit"]) ? " LIMIT " . $this->token["limit"] : "");
                                                break;
                                        case self::SQL_AS_JOIN:
                                                return $this->token["from"] ? "LEFT JOIN " . $this->token["from"] . (isset($this->token["where"]) ? " ON (" . implode(" AND ", $this->token["where"]) : "") . ")" : '';
                                                break;
                                        }
                                break;
                        case self::QUERY_TYPE_INSERT:
                        case self::QUERY_TYPE_UPDATE:
                        case self::QUERY_TYPE_DELETE:
                                return false;
                                break;
                        }
                }

That would just make an empty join return an empty string so you can do:

->join
	(
$something ?  /*your condition */
	query::select() /* a join if condition is ok
	->from("something")->as("s")
	->where("s.usefull")->equals(new field("a.avalue"))
: '' /*nothiing if condition is false */
	)
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote  
Join Date: Jun 2008
Posts: 45
Reputation: kvdd is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 2
kvdd kvdd is offline Offline
Light Poster

Re: If else statement in a 'closed' code

  #4  
Jun 10th, 2008
Thanks, you give me another look how to reach the goal. Thank you.

Topic solved
Last edited by kvdd : Jun 10th, 2008 at 7:19 am.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb PHP Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 4:19 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC