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:

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

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

Recommended Answers

All 3 Replies

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.

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/browse/trunk/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 */
	)

Thanks, you give me another look how to reach the goal. Thank you.

Topic solved

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.