Note that there are some explanatory texts on larger screens.

plurals
  1. POIncrementing child element database fetching in codeigniter
    primarykey
    data
    text
    <p>I'm trying to fetch a series of ids from a database table that includes cross-referencing - each element, a "topic", includes a column for "parent topic" that is within the same table. Given a single parent topic, I want to build an array of all the subtopics that have it as their parent, and then all of the subtopics of those topics, etc.</p> <p>This doesn't seem like it's that hard, but as a self-taught programmer I feel I'm using all the wrong tools. The <code>merge-array()</code> and <code>var_dump()</code> sections, in particular, feel wrong and I'm not sure about the overall approach. What should I replace these elements with?</p> <pre><code>function get_subtopics($parent_topic) { //returns an array of subtopics minus the first $all_subs = array(); $query = $this-&gt;db-&gt;get_where('topics', array('parent_topic' =&gt; $parent_topic)); $subs = $query-&gt;result_array(); $resubs = array(); $query-&gt;free_result(); //push subs to all_subs //while the subs array has members, find their child while (count($subs)&gt;0) { foreach ($subs as $s) { $query = $this-&gt;db-&gt;get_where('topics', array('parent_topic' =&gt; $s['id'])); $resubs = array_merge($resubs, $query-&gt;result_array()); $query-&gt;free_result(); } $all_subs = array_merge($all_subs, $resubs); var_dump($resubs); } //Returns an array of ids return $all_subs; } </code></pre> <p>EDIT: The objective of this is to form a "pool" of topics from which problems will be drawn for a random generator - I'm trying to get all of the subtopics into one array, with no tree structure to differentiate them. Users that specify a parent topic, like "math" should get an even mix of math subtopics like "algebra", "algebra:quadratics" or "calculus" from which problems will be drawn. Hope that clarifies a little.</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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