Hello, how can I add an element in the middle of the loop and then continue its execution. Here is an example of a loop:

while ($date= $sql->fetch()) { //Ρ†ΠΈΠΊΠ» 1 Π²Ρ‹Π²ΠΎΠ΄ΠΈΡ‚ 10 записСй echo $date['title']; echo $date['img']; echo $date['text']; } while ($rekl= $sqlrekl->fetch()) { //Ρ†ΠΈΠΊΠ» 2 Π²Ρ‹Π²ΠΎΠ΄ΠΈ записи echo $date['title']; echo $date['img']; echo $date['text']; } 

In the middle of its execution, you need to output another cycle, then continue the first one. Execution Example:

 Π—Π°ΠΏΠΈΡΡŒ1 .. Π—Π°ΠΏΠΈΡΡŒ5 //Π’Ρ‚ΠΎΡ€ΠΎΠΉ Ρ†ΠΈΠΊΠ» Π—Π°ΠΏΠΈΡΡŒ6 .. Π—Π°ΠΏΠΈΡΡŒ10 

    2 answers 2

    assume that the pdo object is still the same.

     $date = $sql->fetch(); $dataArray = iterator_to_array($date); for($i=0;$i<count($dataArray);$i++) { echo $dataArray[$i]['title']; // etc .... if($i==cail(count($dataArray)/2)) { while ($rekl= $sqlrekl->fetch()) { //Ρ†ΠΈΠΊΠ» 2 Π²Ρ‹Π²ΠΎΠ΄ΠΈ записи echo $rekl['title']; // Ρ‚ΡƒΡ‚ я Π΄ΡƒΠΌΠ°ΡŽ ошибка ΠΏΠΎ этому date Π½Π° rekl echo $rekl['img']; echo $rekl['text']; } } } 
    • if ($ i == cail (count ($ data) / 2)) $ data where does this variable come from? - user209681
    • @ user209681 sorry was sealed up this $dataArray corrected the answer - Naumov
    • not cail, but ceil - EatMyDust
    • does not work, does not display a single record - user209681
    • var_dump($dataArray);die; before the cycle, what does it show? - Naumov

    It is enough to enter the counter $ i, at the end of the first cycle, add a unit to it at each iteration, and where it is necessary to output the second cycle, add the conditions for checking the counter.

     $i=0; while ($date= $sql->fetch()) { echo $date['title']; echo $date['img']; echo $date['text']; if($i==5) { while ($rekl= $sqlrekl->fetch()) { //Ρ†ΠΈΠΊΠ» 2 Π²Ρ‹Π²ΠΎΠ΄ΠΈ записи echo $date['title']; echo $date['img']; echo $date['text']; } } $i++; } 
    • caste better fetch to replace fetchAssoc . And while on for , and then if($i==5) on if($i==cail(count($date)) and the output to something like $date[$i]['title'] - Naumov
    • @Naumov Please tell me a sample code. - user209681
    • And why is the EatMyDust option not suitable? User209681
    • @ user209681 Yes, because you can have 10,15 records, 8,4,5 are races, if you enter i ++ in a while in a condition then it is already for . With a plus, it is not in the middle but on the 6th element displays data - Naumov
    • in the condition "// cycle 1 displays 10 records" I don’t know what could be there, as they asked and answered - EatMyDust