Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This was hand-written in a text editor... I don't have a dev environment handy so appologies if this is just plain wrong or contains typos.</p> <p>But regardless I think the idea is clear/sound.</p> <p>The <code>$sql</code> would be:</p> <pre><code>SELECT parent.id as parentid, parent.[name] as parentname, child.[name] as childname FROM category child left join category parent -- Can't exclude parents with no children, else they won't appear in the list! on child.parent = parent.id ORDER BY parentid </code></pre> <p>And then the PHP would be:</p> <pre><code>function categoryLevels(){ $bullet = "*"; // You can specify your HTML/CSS :-) $indent = " "; // You can specify your HTML/CSS :-) $sql = "_as above_"; $query = mysql_query($sql); $lastParentId = 0; while($row = mysql_fetch_array($query)){ $parentId = $row['parentid'] if ($parentId != 0) { if ($parentId != $lastParentId) { echo $bullet . $row['parentname'] $lastParentId = $parentId; } if (!is_null($row['childname']) { // this line is psuedo-code! - don't know the php equiv for is_null, sorry. echo $indent . $bullet . $row['childname']; } } } } </code></pre> <p>The gist of this is basically:</p> <ol> <li>Get a result set of all categories (excluding those with no parents), sorted by the parent to which they belong </li> <li>Iterate that result set, and for each change in parent, output a "header" with the parent name.</li> <li>Output the child.</li> </ol> <p>Edited this to be less complex (and faster). Now the SQL simply excludes the parent rows altogether, rather than excluding them with the <code>IF</code> statement in the PHP.</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