Re: Thinking out loud - a continuation...

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 12-03-22 11:28 AM, Jay Blanchard wrote:
[snip]
...stuff...
[/snip]

Here is the explanation for what I have done and what I am trying to do
- (based on the customer's request).

A week or so ago I took a set of queries from one table and made them
into an unordered list. This will be pseudo-code so that you get idea.

SELECT DISTINCT column1 FROM table
WHERE company = '1'

while($column1 = mysql_fetch_array($query1results)){
      SELECT DISTINCT column2 FROM table
      WHERE company = '1'
      AND column1 = $column1[0]

      while($column2 = mysql_fetch_array($query2results)){
          SELECT DISTINCT column3 FROM table
          WHERE company = '1'
          AND column2 = $column2[0]
      }
}

This continues for up to 14 columns of data. I'm not worried about the
recursive data retrieval, I have that part and like I said - I can
output a nested unordered list from it quite handily.

Now the customer wants JSON as the output. The JSON must reflect the
children properly.

So I have two choices, a multidimensional array that I can use
json_encode() on or output a string that ultimately forms the JSON. We
have all agreed that doing an array would be the best thing but I cannot
wrap my head around it.

If you have more questions fire away - I'd love to get this solved and
off of my plate.

Fix this code... I've come across codebases that did this specific type of nested querying and it resulted in 10000 queries to the database on every page. Instead, create a layered approach:

    1. Select your root elements.
    2. Loop over in PHP and create an array of child IDs.
    3. Select the children from the database.
    4. Go to step 2.

This way you will only every perform "depth" number of queries.

Cheers,
Rob.
--
E-Mail Disclaimer: Information contained in this message and any
attached documents is considered confidential and legally protected.
This message is intended solely for the addressee(s). Disclosure,
copying, and distribution are prohibited unless authorized.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux