Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It's possible using your idea but you can do it using one query like</p> <pre><code>global $wpdb; $result=$wpdb-&gt;get_results('select a.col, b.col1, b.col2, b.col3 from a,b where a.col=b.col5'); </code></pre> <p>Assume that you have <code>tbl1</code> table and it has a <code>name</code> field and <code>tbl2</code> table and it has a field for example <code>tbl1_name</code> which has the name value of <code>tbl1</code> name field and other fields, (with no duplicates) so you can join both tables like</p> <pre><code>$result=$wpdb-&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; $names[$row-&gt;name][]=$row; &lt;?php endforeach; ?&gt; foreach($names as $name=&gt;$info): echo $name; ?&gt;&lt;table&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 &lt;?php endforeach; ?&gt; ?&gt;&lt;/table&gt;&lt;?php &lt;?php endforeach; ?&gt; </code></pre> <p>Also remember <code>$wpdb</code> is a global object and available through the template files so you dont need to use <code>new</code> to make an instanse, just use <code>global</code> keyword as I wrote in my answer.</p> <p><a href="http://codex.wordpress.org/Class_Reference/wpdb" rel="nofollow"><strong>Class Reference/wpdb</strong></a> and <a href="http://www.datamartist.com/sql-inner-join-left-outer-join-full-outer-join-examples-with-syntax-for-sql-server" rel="nofollow"><strong>sql join</strong></a> and <a href="http://w3resource.com/sql/joins/sql-joins.php" rel="nofollow"><strong>this one</strong></a>.</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.
 

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