There is a cycle that outputs from the database. Displays in turn.
In one of the fields there is a key label in the after, before which the infu does not need to be displayed, and after that it is necessary. To find out the number of this record, you need to make a cycle with checking the position of this label and only then do a data output cycle.

The label is only in one record. And all that to this record does not need to display.
With a simple condition, it turns out that I can only withdraw what's up to the mark.
And the label may or may not be

Is it possible to optimize the code so that there are not two cycles of passage in a sample of the database?

  • And what is the usual condition inside the cycle that didn't please you? - Dmitriy Simushev
  • Start the flag, initially false . There is a label - output the record and set the flag to true . If the flag is true then output the record. - u_mulder
  • The label is only in one record. And all that to this record does not need to display. And the label may or may not be - Raaur
  • and what should be the behavior if there is no label? We derive all? From the description of your question is not entirely clear. - Igor Karpenko
  • yes show all - Raaur

2 answers 2

You can try to accumulate information for output to some variable before hitting the label, and after hitting the label (if it will) clear the value of this variable, and after the end of the cycle to display the accumulated information.

Schematic example (based on the sample from @u_mulder):

 $output = ''; foreach ($ar as $item) { if ($item['metka'] == 1) { $output = ''; } $output .= $item['value']; } echo $output; 

You can make this case a separate function / method:

 function getOutputString(array $data, callable $func, $tagKey = 'metka', $tagValue = 1) { $output = ''; foreach ($data as $row) { if ($row[$tagKey] == $tagValue) { $output = ''; } $output .= $func($item); } return $output; } 

Call example:

 $ar = [ ['metka' => 0, 'value' => 2], ['metka' => 1, 'value' => 3], ['metka' => 0, 'value' => 4], ]; $output = getOutputString($ar, function($row) { return $row['value'];} ); // output: 34 

    Here is a hypothetical code, as the initial data are unclear from your question:

     $ar = [ ['metka' => 0, 'value' => 2], ['metka' => 1, 'value' => 3], ['metka' => 0, 'value' => 4], ]; $output = false; foreach ($ar as $item) { if ($item['metka'] == 1) { $output = true; } if ($output) { echo $item['value']; } } 
    • and what will happen if there is no this label? - Raaur
    • If the label is not - nothing will be displayed. In this case, you have to go through the results again. Nothing to do with it. You can output the results in two buffers - in one, everything, in the other, everything after the label. Following the cycle, find out which of the buffers to display. - u_mulder
    • Or output to one buffer, but when it detects a label, reset it and re-fill it from the place where there is a label. ))) Well, here I am inclined to this option. - Raaur