Status
Not open for further replies.

theforumdrunk

Active Member
63
2010
2
0
13ed837b.png

Can you suggest how I output these to an expanding/collapsing hierarchy?

Example: http://jqueryui.com/demos/accordion/
Although the accordian is great, it wont work unless I output the html correctly which i'm finding very hard to do.

Here is the page with my current code: http://filewi.re/releases.php

And the code itself: http://code.google.com/p/filewire/source/browse/trunk/includes/releases.php

This is hours of mental torture so it's a lil messy. Thanks.
 
14 comments
First of all you have your query above the table:
PHP:
$get = mysql_query("SELECT * FROM releases ORDER BY whateveryouwant");

To be honest you should just put the table structure in plain HTML, and then within the <table> where you normally put each <tr> you just add something like this:

PHP:
<table>
// Head of the table like ID, name, etc goes here
<?php
while($row = mysql_fetch_object($get)){
?>
<tr>
<td><?php echo $row->id; ?></td>
<td><?php echo $row->name; ?></td>
...
</tr>
<?php 
}
?>
</table>

Remember, try and keep as much HTML as you can, keep the design separated from the functionality. No need to go and display HTML in within a PHP echo function :D , just the stuff you need ;)
 
Thanks whoo, no problem with that. It's more the actual process of finding who belongs to whos parent (check db pic). Then displaying them in the right higherarchy.
 
It's just a matter of thinking:

First you have to select all the parents, then for each parent, you print it, grab the children and print them too...

And after that you just do like I told you:
PHP:
<table>
// Head of the table like ID, name, etc goes here
<?php
$get = mysql_query("SELECT * FROM tablename WHERE parent = 0 ORDER BY whateveryouwant");
while($parent = mysql_fetch_object($get)){
  $get2  = mysql_query("SELECT * FROM tablename WHERE parent = $parent->id ORDER BY whateveryouwant");
?>
<tr>
<td><?php echo $parent->id; ?></td>
<td><?php echo $parent->name; ?></td>
</tr>
<?php
  while($child = mysql_fetch_object($get2)) {
?>
<tr>
<td><?php echo $child->id; ?></td>
<td><?php echo $child->name; ?></td>
...
</tr>
<?php 
}
?>
</table>

This should display

-Parent1
--Child
--Child
--Child
-Parent2
--Child
--Child
--Child


Hope this helped ;)
 
Sure, just name how many children you want, this script is horny xD

PHP:
<table>
// Head of the table like ID, name, etc goes here
<?php
$get = mysql_query("SELECT * FROM tablename WHERE parent = 0 ORDER BY whateveryouwant");
while($parent = mysql_fetch_object($get)){
  $get2  = mysql_query("SELECT * FROM tablename WHERE parent = $parent->id ORDER BY whateveryouwant");
?>
<tr>
<td><?php echo $parent->id; ?></td>
<td><?php echo $parent->name; ?></td>
</tr>
<?php
  while($child = mysql_fetch_object($get2)) {
    $get3 = mysql_query("SELECT FROM tablename WHERE parent = $child->id ORDER BY whateveryouwant");
?>
<tr>
<td><?php echo $child->id; ?></td>
<td><?php echo $child->name; ?></td>
...
</tr>
<?php
while($grandchild = mysql_fetch_object($get3)){
?>
<tr>
<td><?php echo $grandchild->id; ?></td>
<td><?php echo $grandchild->name; ?></td>
...
</tr>
<?php } ?>
<?php 
}
?>
</table>

I hope you get how this works now ;)
 
The problem is that most programmers including the better ones will always do recursive calls to the database, causing higher load on the DB.

You can do this in a single pass, using PHP References.

Example:

PHP:
$refs = array();
$list = array(); //This is the baby

$result = mysql_query("SELECT * FROM releases");

while($data = @mysql_fetch_assoc($result))
{
    //Cfreate / Select a referenced variable in the memory
    $thisref = &$refs[ $data['id'] ];

    //Set the parent id
    $thisref['parent'] = $data['parent'];

    //Set the data as your going to need it later
    $thisref['data'] = $data;

    //If its not a root, set the referenced array
    if ($data['parent'] == 0)
    {
        $list[ $data['id'] ] = &$thisref;
    }
    else
    {
        $refs[ $data['parent'] ]['children'][ $data['id'] ] = &$thisref;
    }
}

Example of why references works is like so:

53-ref-build-lists.gif


The code above will replicate hierarchy in a PHP
 
Thanks very much! Wow, i'll take a look at this in a couple days and try to get my head around it.

Can you show an example for grandchildren? I'll perhaps get a better perspective.

EDIT: @dermechove Thanks that worked!
 
The problem is that most programmers including the better ones will always do recursive calls to the database, causing higher load on the DB.

Hey,

Yeah litewarez it's true, but still, we are working with someone who is learning PHP and therefor I didn't give him any code that he wouldn't understand xD.

I have been coding (not part of my job/uni) PHP for many years now and I never even used the '&' sign or anything :P

Anyways, just had a look at your code, and that would work too.
 
references are really ugly in the over all scheme of things. Usually they can be advoided and in this case it can by loading all the data from the table into an array to begin with and instead of querying the database looking it up in the hash table/array (key=>value).

In reality although your solution has better performance litewarez, Whoo's is better for the skill level of the OP.
 
References are not ugly, if i used a custom class for storing the data they would be passed by reference anyways as of PHP5

References allow control over the data in the memory, as your referencing data and not creating new ones.


Yeah litewarez it's true, but still, we are working with someone who is learning PHP and therefor I didn't give him any code that he wouldn't understand xD.

This is a new technique that should be used as standard, if he is new to PHP, should we not teach him the best possible solution regardless weather its its slightly more demanding for a newbie ?
 
Status
Not open for further replies.
Back
Top