PHP: Stuck variable not updating its value even with global

I intend to run only once the “p” element, I don’t know why the value passed after it has ran is not updated in the above variable and hence not run again i.e is running always with the “not done” value. Thought it was a problem witch scope so tried global but didn’t make any difference… I do know I should use a boolean, but I wanted to be sure why it wasn’t working first.

$new_loop = array_slice($new_loop, $offset, $posts_per_page);
$separador_enviado = "not done";
$loop_counter = "";                
  
                foreach ($new_loop as $loop) {                 

                   if($separador_enviado == "not done") { 
                        
                        ?>                     
                          
                          <p>This should be outputed only once</p>
                        <?php 
                        global $separador_enviado;
                        $separador_enviado = "done";
                        
                      }
                        else { ?>
                        <p> item in the loop not running </p> <?php 
                        }  
                                                                             

            $loop_counter++;  
        }

Do note this is a simplified version of the original code…

When I run the above code an manually create a 5 element array for $new_loop, the following is displayed to the page:

enviado o no not done

no aplica

no aplica

no aplica

no aplica

What exactly are you expecting the page output to be if $new_loop contained 5 elements?

Thank you. Oh okey i realize I should made this more understandable… let me edit the original post accordingly, but basically since I’m using a pagination with array_slice I need that once
<p>This should be outputed only once</p> is outputed it isn’t outputed again. In your above run it did run once, but if there are more than 5 elements as posts_per_page and the $offset is first 0, then 5, then 10… it will loop and run again this line <p>This should be outputed only once</p> that’s what I’m trying to prevent… I’m editing the original snippet…

When I run the code, it is only displayed once. The other 4 times something else is displayed.

Yes, and thats correct. But if you consider something like this

$offset = (($page - 1) * $posts_per_page);                                          
$new_loop = array_slice($new_loop, $offset, $posts_per_page);

on each ajax call with another $page value it will load it again I’m realizing perhaps I should send this value through the ajax call maybe that’s what missing…
Some more context: this is intended to be a separator between the last starred post and the first normal post in page that lists posts

Update: Ok that did it :slight_smile: