0

I am at the internship and I am developing a HTML table that has milestones and milestoneparts(in this project we call it milestonefase). Each milestone has a bunch of milestonefases.

As example: Milestone99 has milestonefase 10, milestonefase14.

Those data come from two diffrent database tables. My array looks like this:

Array
(
    [milestonesfases] => Array
        (
            [10] => Array
                (
                    [milestonefase_id] => int
                    [milestonefase_titel] => string
                    [milestonefase_milestone_id] => int
                )   
            [14] => Array
                (
                    [milestonefase_id] => int
                    [milestonefase_titel] => string
                    [milestonefase_milestone_id] => int
                )
        )

    [milestone_id] => int
    [milestone_titel] => string
    [client] => string
    [milestone_verkocht_id] => int
)

My question is: How can I loop trough all milestonefases that is connected with milestone 6 (in this case)

My loop looks like this:

foreach ($stones as $milestone)
{
   echo '<tr id="'. $milestone['milestone_id'] . '" class="milestone'. $milestone['milestone_id'] . '">';
     echo '<td>'. $milestone['milestone_id'] . '</td>';
     echo '<td><a href="javascript:keuzeGebruiker(\'milestone'.$milestone['milestone_id']. '\',\''.$milestone['milestone_id'].'\');">' . $milestone['milestone_titel'] . '</a></td>';
     echo '<td>'. $milestone['client'] . '</td>';
   echo '</tr>';

    for ($i = 0; $i < count($milestone['milestonesfases']); $i++)
    {
        echo '<tr>';
         echo '<td>'. $milestone['milestonesfases']['milestonefase_id'] . '</td>';
         echo '<td>'. $milestone['milestonesfases'][10]['milestonefase_titel']  . '</td>';
         echo '<td></td>';
       echo '</tr>';
    }
}

That '10' in my loop needs to be all milestonefase_id's (which is 10, 11, 12, 13, 14, 15 in this milestone).

How can I manage to get all milestonefases.

Attention! it must work for every milestones not only this please

That piece of milestones works but milestonefases does not work.

3 Answers 3

1

This is Check for if is array and you can use it using same foreach like @Aron Said:

foreach ($stones as $milestone)
{
   echo '<tr id="'. $milestone['milestone_id'] . '" class="milestone'. $milestone['milestone_id'] . '">';
     echo '<td>'. $milestone['milestone_id'] . '</td>';
     echo '<td><a href="javascript:keuzeGebruiker(\'milestone'.$milestone['milestone_id']. '\',\''.$milestone['milestone_id'].'\');">' . $milestone['milestone_titel'] . '</a></td>';
     echo '<td>'. $milestone['client'] . '</td>';
   echo '</tr>';
if(is_array($milestone['milestonesfases'])){
  foreach ($milestone['milestonesfases'] as $key => $value) {
        echo '<tr>';
       echo '<td>'. $value['milestonefase_id'] . '</td>';
       echo '<td>'. $value['milestonefase_titel']  . '</td>';
       echo '<td></td>';
       echo '</tr>';
    }
  } 
}
0

Instead of using a for loop,

for ($i = 0; $i < count($milestone['milestonesfases']); $i++)

Do a foreach loop

foreach ($milestone['milestonesfases'] as $milestonesfase) 
{
    echo '<tr>';
    echo '<td>'. $milestonesfase['milestonefase_id'] . '</td>';
    echo '<td>'. $milestonesfase['milestonefase_titel']  . '</td>';
    echo '<td></td>';
    echo '</tr>';
}
0
0

please try

foreach ($stones as $milestone)
{
 echo '<tr id="'. $milestone['milestone_id'] . '" class="milestone'. $milestone['milestone_id'] . '">';
 echo '<td>'. $milestone['milestone_id'] . '</td>';
 echo '<td><a href="javascript:keuzeGebruiker(\'milestone'.$milestone['milestone_id']. '\',\''.$milestone['milestone_id'].'\');">' . $milestone['milestone_titel'] . '</a></td>';
 echo '<td>'. $milestone['client'] . '</td>';
 echo '</tr>';

 foreach($milestone['milestonesfases'] as $milestoneRow)
 {
    if($milestoneRow['milestonefase_milestone_id'] == $milestone['milestone_id']) {
         echo '<tr>';
         echo '<td>'. $milestoneRow['milestonefase_id'] . '</td>';
         echo '<td>'. $milestoneRow['milestonefase_titel']  . '</td>';
         echo '<td></td>';
         echo '</tr>';
    }
 }
}
2
  • No, this will not work, because the iteration is wrong. You cannot (safely) iterate over an associative array with for ($i = 0; $i < $max; ++$i). What you are doing is iterating over the keys 0..$max but in an assoc array the keys can be anything. If you want to takes this route, you need to iterate over array_values($milestone['milestonefases']). And please, don't put a function call inside the comparision in a for loop. It can trigger really expensive performance issues if the dataset grows larger over time. Instead, break out the count($milestone['milestonefases']) to a variable $max. Commented Oct 12, 2017 at 13:43
  • yes you are correct i havent noticed the key. we have to use foreach instead of for loop. Corrected my answer Commented Oct 12, 2017 at 14:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.