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
https://forums.phpfreaks.com/topic/255274-how-do-i-reference-a-parent-id/
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

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.