1

This is the simplified of my code:

$html = "";
$posts = $sth->fetch(PDO::FETCH_ASSOC);
while ( $post = array_shift($posts) ){

    if ( $post['deleted'] == true ){

        /* jump to the four lines above (while)
         * to ignore this post, becuase it is deleted
         */
    }

    $html .= '<div class="content">' . $content . '</div>';

}

As I've commented in the middle of my code, I need to go to the upper lines. Is it possible?


Noted that in the example I mention above, I can put $html .= ... line into else {} block. But in reality everything is more complicated that doing such a thing won't work. All I need to know is something like jumping to up in PHP?

Martin AJ
  • 6,261
  • 8
  • 53
  • 111
  • shouldn't be that the `->fetch()` is supposed to be inside the while condition? (so that it continually fetch the rows until it runs out) and can you elaborate what _I need to go to the upper lines_ mean – Kevin May 21 '18 at 09:12
  • what do you mean by "I need to go to the upper lines" what upper lines? – madalinivascu May 21 '18 at 09:12
  • I mean I want to tell to compiler, go to the line `while ( ...` – Martin AJ May 21 '18 at 09:13
  • [php goto syntax](http://php.net/manual/en/control-structures.goto.php) But this is not recommended in high level programming language, just for your reference. – CK Wong May 21 '18 at 09:14
  • can you give a little bit of context of why would you need such thing? by the way, you can explicitly change the current pointer on the fetch method, just add that parameter its in the docs – Kevin May 21 '18 at 09:16

2 Answers2

3

You can use continue keywords may be help,As i understand your problem.

nageen nayak
  • 1,262
  • 2
  • 18
  • 28
1

why not do it like this:

for($i=4;$i<count($posts);$i++)
{
    if ($current['deleted'] === true) 
    {
      $i -= 4; // jump back
    }
    $current = $posts[$i];

    $html .= '<div class="content">' . $content . '</div>';
}

EDIT: all you need is the continue statement..

Eriks Klotins
  • 4,042
  • 1
  • 12
  • 26