•
•
•
•
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
![]() |
•
•
Join Date: Jun 2008
Posts: 45
Reputation:
Rep Power: 1
Solved Threads: 2
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:
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)
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
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:
php Syntax (Toggle Plain Text)
$query = query::select() ->field("u.u_id")->as("id") ->field("u.uren_begin")->as("begin") ->field("u.uren_eind")->as("eind") ->field("l.u_voornaam")->as("voornaam") ->field("loc.loc_naam")->as("vestiging") ->from("uren")->as("u") ->join ( query::select() ->from("login")->as("l") ->where("l.u_id")->equals(new field("u.u_id")) ) ->join ( query::select() ->from("locaties")->as("loc") ->where("l.u_locatie")->equals(new field("loc.loc_id")) ); ;
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)
php Syntax (Toggle Plain Text)
if($something) { // when true, add a join ->join ( query::select() ->from("something")->as("s") ->where("s.usefull")->equals(new field("a.avalue")) ) }
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
•
•
•
•
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!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
![]() |
•
•
•
•
•
•
•
•
DaniWeb PHP Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
access activation api blogger blogging blogs code code injection combo dani daniweb data debugging development dreamweaver dropdownlist epilepsy gdata google gpl griefers hackers html innovation javascript key linux microsoft module net news openbsd product programming reuse rss serial source tags vista web wysiwyg xml
- error '80020009' exception error (ASP)
- Newby to PHP here.... (Google Analytics) (PHP)
- need some help in my code please (ASP.NET)
- Keep getting 'Missing Return Statement' error (Java)
- Help Needed (PHP)
- need help with the use of arrow keys (C)
- "Craps", Game Help (C++)
- Single brace use? (C++)
- help with database assignment (Java)
Other Threads in the PHP Forum
- Previous Thread: Storing image paths into a mysql db using a php script
- Next Thread: member accound update


Linear Mode