[thelist] php/mysql help

Shashank Tripathi sub at shanx.com
Tue Feb 19 19:58:01 CST 2002


    > 1). The GROUP BY clause in the first SQL query
    >     This gets the distinct values.
    > 2). The second SQL query (in the while loop)
    >     picks a "distinct" value and then loops
    >     through the entries using "for"


Just as a matter of application design though, don't you think this is too
database intensive -- too many trips to the DB?

I would maybe prefer to err on the side of making *one* trip to the
database, getting all the records, and then using PHP to loop through the
resultset in whatever way I wanted (for convenience, I would sort the
results according to the relevant condition -- in this case, "order by date"
perhaps).

A pseudocode for instance may look as follows (not tried, just a
suggestion):


--------------------CODE----------------------
    // Assuming that this variable has the results
    // from a simple query, without any GROUP BY
    // but with an ORDER BY date
    $resultSet = ..;

    // Now loop through the results
    for ($i = 0; $i < count($resultSet); $i++
    {
        // Assuming first column in results is date
        // and 'i' is the index for the current row
        if ($tempDate == $resultSet[$i][0])
        {
            1. Echo a couple of line breaks (<br><br>)
            2. Echo the subject etc
        }
        else
        {
            1. Echo a new paragraph (<p>)
            2. Echo the date because this is a distinct date and a <br>
            3. Echo the subject etc.
            4. $tempDate = $resultSet[$i][0];
        }

    }
--------------------/CODE----------------------


Cheers
Shanx






More information about the thelist mailing list