Note that there are some explanatory texts on larger screens.

plurals
  1. PONested queries using wpdb
    primarykey
    data
    text
    <p>Im trying to display a few rows of data for each <code>&lt;h3&gt;&lt;?php echo $name; ?&gt;&lt;/h3&gt;</code> item that appears in the loop.</p> <p>With the current code it only shows one row for each item <code>&lt;h3&gt;&lt;?php echo $name; ?&gt;&lt;/h3&gt;</code></p> <p>I have this in my functions.php file <code>$newdb = new wpdb('login', 'pass', 'db', 'host');</code></p> <pre><code>&lt;?php $result=$newdb-&gt;get_results('select tbl1.name, tbl2.col1, tbl2.col2, tbl2.col3 from tbl1, tbl2 where tbl1.name=tbl2.tbl1_name'); $names=array(); foreach($result as $row): ?&gt; &lt;?php $names[$row-&gt;name][]=$row; endforeach; ?&gt; &lt;?php foreach($names as $name=&gt;$info): ?&gt; &lt;h3&gt;&lt;?php echo $name; ?&gt;&lt;/h3&gt; &lt;table&gt; &lt;tr&gt;&lt;th&gt;col1&lt;/th&gt;&lt;th&gt;col2&lt;/th&gt;&lt;th&gt;col3&lt;/th&gt;&lt;/tr&gt; &lt;?php foreach($info as $n):?&gt; &lt;tr&gt; &lt;td&gt;&lt;?php echo $n-&gt;col1; ?&gt;&lt;/td&gt; &lt;td&gt;&lt;?php echo $n-&gt;col2; ?&gt;&lt;/td&gt; &lt;td&gt;&lt;?php echo $n-&gt;col3; ?&gt;&lt;/td&gt; &lt;/tr&gt; &lt;?php endforeach; ?&gt; &lt;/table&gt; &lt;?php endforeach; ?&gt; </code></pre> <p>So the loop displays the heading followed by a few rows of records, not just one.</p> <pre><code>&lt;h2&gt;Name&lt;/h2&gt; &lt;table&gt; &lt;tr&gt;&lt;th&gt;col1&lt;/th&gt;&lt;th&gt;col2&lt;/th&gt;&lt;th&gt;col3&lt;/th&gt;&lt;/tr&gt; col1-value1 col2-value1 col3-value1 col1-value2 col1-value2 col1-value2 etc. &lt;/table&gt; &lt;h2&gt;Name&lt;/h2&gt; &lt;table&gt; &lt;tr&gt;&lt;th&gt;col1&lt;/th&gt;&lt;th&gt;col2&lt;/th&gt;&lt;th&gt;col3&lt;/th&gt;&lt;/tr&gt; col1-value1 col2-value1 col3-value1 col1-value2 col1-value2 col1-value2 etc. &lt;/table&gt; &lt;h2&gt;Name&lt;/h2&gt; &lt;table&gt; &lt;tr&gt;&lt;th&gt;col1&lt;/th&gt;&lt;th&gt;col2&lt;/th&gt;&lt;th&gt;col3&lt;/th&gt;&lt;/tr&gt; col1-value1 col2-value1 col3-value1 col1-value2 col2-value2 col3-value2 etc. &lt;/table&gt; .....` </code></pre>
    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. This table or related slice is empty.
    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