1

クエリを処理する 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..
4

2 に答える 2

2

クエリ ファイルで 2 つのクエリを実行するだけでよいようです。最初のクエリは、探しているものの広範なセットを取得します。2 番目のクエリは、結果に含まれる ID をクエリし、新しいクエリを実行して、その特定の ID に関する詳細を取得します。アプリケーションの顧客検索ページでこれに似たものを使用しています。

$output = array();

$query1 = $this->db->query("SELECT * FROM...WHERE id = ...");

foreach ($query->result_array() as $row1)
{
    $output[$row1['some_id']] = $row1;

    $query2 = $this->db->query("SELECT * FROM table WHERE id = {$row1['some_id']}");

    foreach ($query2->result_array() as $row2)
    {
        $output[$row1['some_id']]['data_details'][$row2['id']] = $row2;
    }
} 

次に、html を表示するページでは、2 つの foreach が必要になります。

foreach($queryresult as $key=> $field)
{
    echo $field['some_field'];

    foreach($child['data_details'] as $subkey => $subfield)
    {
        echo $subfield['some_subfield'];
    }
} 

オブジェクトを使用していることは知っていますが、おそらくこれを変換してその形式を使用できます。これが理にかなっている/役立つことを願っています。

于 2012-12-04T08:50:00.627 に答える
0

これを使って

 if ($some_id !== 0) {

それ以外の

 if ($some_id != 0) {
于 2012-12-03T17:49:51.323 に答える