クエリを処理する 1 つの php ファイルに多数の結合とサブクエリを含むクエリがあるとします。Nb: $query がどのように見えるかの例を一番下に置きます
$query = query here;
if ($query) {
return $query->result();
} else {
return false;
}
}
次に、html を処理する php ファイルには、通常の foreach ループがあり、他のクエリを作成する必要があるいくつかの条件があります。注: result には、オブジェクト $query->result() が格納されます。
foreach ($results as $item) {
$some_array = array();
$some_id = $item->id;
if ($some_id != 0) {
//id_return_other_id is a function that querys a db table and returns the specified column in the same table, it returns just one field
$other_id = id_return_other_id($some_id);
$some_query = another query that requires some joins and a subquery;
$some_array = the values that are returned from some_query in an array
//here i'm converting obj post into an array so i can merge the data in $some_array to item(Which was converted into an array) then convert all of it back into an object
$item = (object)array_merge($some_array, (array)$item);
}
//do the usual dynamic html stuff here.
}
これは完全に機能しますが、ループで多くのクエリを実行する方法が気に入らないため、クエリを処理するファイルに if $some_id != 0 を追加する方法はありますか? 私はもう試した
$query = query here;
//declaring some array as empty when some_id is 0
$some_array = array();
if ($query) {
if ($some_id != 0) {
//same as i said before
$other_id = $this->id_return_other_id($some_id);
$some_query = some query;
$some_array = array values gotten from some query;
}
$qresult = (object)array_merge($some_array, (array)$query->result);
return $qresult;
} else {
return false;
}
}
これは明らかな理由で機能しません。アイデアはありますか?
また、$query 自体でこれらの条件とクエリを作成する方法があれば、私はあなたを永遠に愛します.
Ps: デモ クエリは次のようになります。
$sql = "SELECT p.*,up.*,upi.someField,etc..
FROM (
SELECT (another subquery)
FROM table1
WHERE table1_id = 3
UNION ALL
SELECT $user_id
) uf
JOIN table2 p
ON p.id = uf.user_id
LEFT JOIN table3 up
ON .....
LEFT JOIN table4
ON ....
LEFT JOIN table5
ON ....
And so on..etc..
ORDER BY p.date DESC";
$query = mysql_query..