I have a list of questions. Each question can have children and children can also have children. My database is set up as follows:
id: incrementing id, question: string, parentId: integer, industryId: integer
So basically when i save a parent level question it goes in with a parent ID of "0". If that question was saved as id = 1. Then the sub child would be parentId = 1 and so on.
I need to infinitely loop through the products array to determine if there are more children, but i can't figure out a way to do it that is not manual. Here is my code:
<?php
//Does this question have sub questions?
$hasChildren = FALSE;
foreach($questions as $q) {
if($q->parentId === $question->id) {
$hasChildren = TRUE;
}
}
if($hasChildren === TRUE):
$unique = uniqid();
?>
<ul class="sub-list-{{$unique}}">
@foreach($questions as $q)
@if($q->parentId === $question->id)
<li>{{$q->question}}
<?php
//Does this question have sub questions?
$hasChildren = FALSE;
foreach($questions as $q1) {
if($q1->parentId === $q->id) {
$hasChildren = TRUE;
}
}
if($hasChildren === TRUE):
$unique = uniqid();
?>
<ul class="sub-list-{{$unique}}">
@foreach($questions as $q1)
@if($q1->parentId === $q->id)
<li>{{$q1->question}}
</li>
@endif
@endforeach
</ul>
<?php endif; ?>
</li>
@endif
@endforeach
</ul>
<?php endif; ?>
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire