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…

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…

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:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.