-1

I trying fetchd data from database with array. but when i combine foreach inside while, it looping as much data in database. how to stop looping ? i already make break function but not work. what is missing from my code.

here is mycode :

<?php
  $no=1;                                
  if(mysqli_num_rows($query) > 0)
    {
      $useArray = array("A","B","B","C");
      while($row = mysqli_fetch_array($query))
      {
        foreach ($useArray as $array)
        {
          echo"<tr>";
          echo "<td>".$no."</td>";
          echo "<td>".$row['id']."</td>";
          echo "<td>".$row['name']."</td>";
          echo "<td>".$row['grade']."</td>";
          if($row['v1']>=2.000)
            echo "<td class='bg-success'>".$row['v1']."</td>";
          elseif($row['v1']>=1.500)
            echo "<td class='bg-warning'>".$row['v1']."</td>";
          elseif($row['v1']>=0.100)
            echo "<td class='bg-danger'>".$row['v1']."</td>";
          else
          {
            echo "<td class='bg-dark'>".$row['v1']."<h5>D</h5></td>";
          }
          echo "<td>".$array."</td>";
          if ($array == "C") {
            break;
           }
          echo "</tr>";
        }
      $no++;
    }
  }
 else
  {
?>
</tbody>
</table>
<?php
echo "No Point";
}
?>

the result like below

Looping Foreach

i do searching first, but not working. maybe i'm wrong implementing the code. here is my reference : Combining foreach and while in PHP , break out of if and foreach.

my expected result is :

enter image description here

total data is fixed with how much string in array, so if in array is ("A","B","B","C") so in database also just 4 rows. any help is apreciated.

  • What are you expecting for the output? How does the `foreach()` loop relate to the SQL query? – Nigel Ren Aug 13 '20 at 06:20
  • i just edit my questions. @NigelRen – Dox The augbey Aug 13 '20 at 06:39
  • @NigelRen SQL query for fetch data for displaying data and `foreach` to put string array for each data shown. – Dox The augbey Aug 13 '20 at 06:43
  • Could you add an SQL excerpt from the database? You are currently iterating through the fixed array and only breaking the loop when it is anyways at the last element "C". I cannot really grasp what you want to do. In the image of your expected result from where do you know which letter the row should have? – Malte Aug 13 '20 at 10:19

2 Answers2

1

break leave only the near block. In you code :

while($continue && $row = mysqli_fetch_array($query))
{
    foreach ($useArray as $array)
    {
        echo "<tr>";
        echo "<td>".$no."</td>";
        ...
        if ($array == "C") {
            break;
        }
        echo "</tr>";
    }
  $no++;
}

The break leave the foreach, but the while continue.

You can add a variable to leave the while like :

$continue = true;
while($continue && $row = mysqli_fetch_array($query))
{
    foreach ($useArray as $array)
    {
        echo "<tr>";
        echo "<td>".$no."</td>";
        ...
        if ($array == "C") {
            $continue = fase;
            break;
        }
        echo "</tr>";
    }
  $no++;
}
vernou
  • 6,818
  • 5
  • 30
  • 58
0

What happened here is your break is breaking the foreach, instead of the while that loops through you database query. You can try to break out of the while using break 2.

The 2 here indicates how many "breakable" loops you are trying to jump out of.

Although, I believe you could improve your code by modifying your database query to avoid unnecessarily loops.

A Web-Developer
  • 868
  • 8
  • 29
  • what does `break 2` mean? – Dox The augbey Aug 13 '20 at 06:40
  • @DoxTheaugbey the `2` in `break 2` means break 2 layers of breakable blocks. the `foreach` being the first layer, and the `while` you use to loop through all your database rows is another. – A Web-Developer Aug 13 '20 at 06:43
  • means i have use break outside while too ? – Dox The augbey Aug 13 '20 at 06:51
  • ok, to have an idea of what you are trying to do, can you show us your database query and also what you are trying to achieve? i have a feeling that you are on the wrong path. – A Web-Developer Aug 13 '20 at 07:25
  • this is all i have : https://jsfiddle.net/uLfyoh72/ – Dox The augbey Aug 13 '20 at 07:35
  • oke thanks mate for answering my questions, note that i don't meant asking you to code for me. i wanted to know what is missing from my code. i don't get when you said `break2` on layers 2 cause i'm not clearly understand for complicated foreach usage, that code above i got from another stackoverflow topic, try to put all in together but still not work. simple is, give a good explanation what is wrong with my code. – Dox The augbey Aug 13 '20 at 07:47