Jump to content

how do i reference a parent id ?


ricky spires

Recommended Posts

hello.

 

say i have list and i want the correct children under each parent

 

so i have

$id

$parent_id

$type

$name

 

in the db i have

4 parents each with 2 children

 

id=1 - type=parent - parent_id=0 - name=p1

id=2 - type=parent - parent_id=0 - name=p2

id=3 - type=parent - parent_id=0 - name=p3

id=4 - type=parent - parent_id=0 - name=p4

id=5 - type=child - parent_id=1 - name=c1

id=6 - type=child - parent_id=1 - name=c2

id=7 - type=child - parent_id=2 - name=c3

id=8 - type=child - parent_id=2 - name=c4

id=9 - type=child - parent_id=3 - name=c5

id=10 - type=child - parent_id=3 - name=c6

id=11 - type=child - parent_id=4 - name=c7

id=12 - type=child - parent_id=4 - name=c8

 

so how would i do the code?

 

i thought i could do it like this but its not working.

 

<?php

$family = Family::find_all();
foreach($family as $familys){

$id = $familys->id;
$type = $familys->type;
$parent_id = $familys->parent_id;
$name = $familys->name;


echo'
<ul>';
if($type == "parent"){
echo $name;
} 
echo' </ul>


<li>';
if($type == "child" && $parent_id == $id){
echo $name;
} 
echo' 
</li>';
}
?>

 

 

all i get back is

 

    p1

 

    p2

 

    p3

 

    p4

 

 

no children

 

??

 

 

 

if i remove

$parent_id == $id
from
if($type == "child" && $parent_id == $id){

 

i get this

 

    p1

 

    p2

 

    p3

 

    p4

 

c1

 

c2

 

c3

 

c4

 

c5

 

c6

 

c7

 

c8

 

but they are not listed under the correct parent

 

 

 

 

 

i also tried moving the </ul> to the bottom but i get the same

<?php

$family = Family::find_all();
foreach($family as $familys){

$id = $familys->id;
$type = $familys->type;
$parent_id = $familys->parent_id;
$name = $familys->name;



echo'
<ul>';
if($type == "parent"){
echo $name;
} 
echo'


<li>';
if($type == "child"){
echo $name;
} 
echo' 
</li>
</ul>';
}
?>

 

 

 

any thoughts?

 

thanks

 

rick

Link to comment
Share on other sites

i've done it :)

 

public static function find_all(){
	$sql = "SELECT * FROM ".self::$table_name."";
	return self::find_by_sql($sql);
}


public static function find_by_parent($parent_id){
	$sql = "SELECT * FROM ".self::$table_name."  WHERE parent_id=".$parent_id."";
	$result_array = self::find_by_sql($sql);
	return $result_array;
}



<?php

$family = Family::find_all();
foreach($family as $familys){

$parent_id = $familys->id;
$type = $familys->type;
$name = $familys->name;



echo'
<ul>';
if($type == "parent"){
echo $name;
} 
echo'


<li>';

	$child = Family::find_by_parent($parent_id);
	foreach($child as $childs){	
	$child = $childs->name;
	echo $child;
	} 
echo' 
</li>
</ul>';
}
?>

 

that echos

 

    p1

    c1c2

 

    p2

    c3c4

 

    p3

    c5c6

 

    p4

    c7c8

 

 

thanks all

 

 

p.s

 

mikosiko = your way looks interesting. im not sure if i fully understand what your doing but if it can be used to make my code better i would love to see it. :)

thanks

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.