Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One of the basic tenets of relational databases is that columns don't hold compound data, which means no arrays. It's also called "<a href="http://en.wikipedia.org/wiki/First_normal_form" rel="nofollow noreferrer">first normal form</a>" (1NF). If you don't want repeated order info within the product query, you can run two queries rather than one, one that gets the orders and the second that gets the products. </p> <p>Since the result of the query is being processed anyway, you can also take care of aggregation within PHP once you get the result. You can aggregate first:</p> <pre><code>$orders=array(); while ($row = $result-&gt;fetch()) { if (! isset($orders[$row['order_id']])) { $orders[$row['order_id']] = new Order($row); } else { $orders[$row['order_id']]-&gt;addProducts($row); } } </code></pre> <p>(assuming a suitable Order class), or aggregate in-line:</p> <pre><code>class OrderList { ... function printXML($result) { $currOrderID = null; echo '&lt;orders&gt;' while ($row = $result-&gt;fetch()) { if ($currOrderID != $row['order_id']) { $this-&gt;closeOrder(); $this-&gt;openOrder($row); } $this-&gt;printProduct($row); } $this-&gt;closeOrder(); echo '&lt;/orders&gt;'; } function openOrder($row) { echo "&lt;order id='$row[order_id]'&gt;&lt;total&gt;$row[order_total]&lt;/total&gt;"; $this-&gt;printCustomer($row); echo '&lt;products&gt;'; } function printProduct($row) { echo "&lt;product&gt;&lt;title&gt;$row[title]&lt;/title&gt;&lt;price&gt;$row[price]&lt;/price&gt;&lt;/product&gt;\n"; } function closeOrder() { echo '&lt;/products&gt;&lt;/order&gt;'; } function printCustomer($row) { echo &lt;&lt;EOS; &lt;customer&gt; &lt;email&gt;$row[primary_email]&lt;/email&gt; &lt;first_name&gt;$row[delivery_first_name]&lt;/first_name&gt; ... EOS; $this-&gt;printAddress($row); echo '&lt;/customer&gt;'; } function printAddress($row) { echo &lt;&lt;EOS; &lt;address&gt; &lt;street line="1"&gt;$row[delivery_street1]&lt;/street&gt; &lt;street line="2"&gt;$row[delivery_street2]&lt;/street&gt; ... &lt;/address&gt; EOS; } } </code></pre> <p>Of course, the specifics of the above is not a good design for production code. The <code>print*</code> methods (and even order printing, via the <code>openOrder</code> and <code>closeOrder</code>) probably deserve to be OrderWriter, CustomerWriter, AddressWriter and ProductWriter classes. You might also want to use <a href="http://php.net/XMLWriter" rel="nofollow noreferrer">XMLWriter</a> rather than echoing.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload