Stop running a particular php snippet

Asked

Viewed 34 times

1

I am in need of stopping the functioning of a particular chunk of code. Exit, how die, stops the whole foreach code block.

I just need it to stop inside the if, but keep running the foreach, then go back to those ifs, but not running the first if

for example

  foreach ($stmt->fetchAll() as $item => $value){

            $this->return = '
                <div class="form-group">
                    <div class="col-lg-12">
                       ';
                        if($value->nome_categoria === 'USERS') {
                            $this->return .= 'USERS';
                            $this->achouUsers = true;
                            while (!$this->achouUsers){
                                continue;
                            }

                        }

                        if($value->nome_categoria === "OPTIONS"){
                            $this->return .= 'OPTIONS';
                            $this->achouUsers = true;
                            while (!$this->achouOptions){
                                continue;
                            }
                        }


                        if( $value->nome_categoria === "MFP E CONNECTOR" ){
                            $this->return .= 'MFP E CONNECTOR';
                            while (!$this->achouMDF){
                                continue;
                            }
                        }
                    $this->return .= '
                    </div>
                    <div class="col-lg-12">                            
                         <div class="col-lg-10">
                              <input type="radio" name="inputUsuarios">
                              '.utf8_encode($value->nome_item).'
                              '.(utf8_encode($value->nome_item) === 'Até' ? '<input type="text" > usuários <button  class=" btn btn-warning"><i class="fa fa-calculator"></i> </button>' : '').'

                         </div>
                         <div class="col-lg-2 text-right">
                              '.number_format($value->valor_item,2,',','.').'
                         </div>
                    </div>                           

                </div>

                ';

            echo $this->return;
        }

ran if from USERS only once. from there ran everything, and continued running, but testing instead of USERS, but OPTIONS instead

EDITION 1

The expected result was something like

USERS inserir a descrição da imagem aqui

OPTIONS inserir a descrição da imagem aqui

Showing the category only once, the USERS on top of this image, the OPTIONS on top of this image and etc. It pulling from the bank but only appearing once.

With this code my return is being something like

inserir a descrição da imagem aqui

  • This chunk of code is very confusing and doesn’t seem to make sense. What exactly do you need to do? I say this in the sense of what is the intended result (and not in the medium described in the question). What is the expected result?

  • I will edit in the question the idea

  • it goes there... the idea, and the return as it is coming

  • You always concatenate into return the value of nome_categoria, then why do the if?

  • the idea would be to stop the execution of USERS, OPTIONS

  • Notice that it’s coming USERS, something, USERS something

  • Okay, I think I’m starting to understand. All of these options are generated from the loop, right? You want the title to appear Users at the beginning and when to change the title you want to appear again, in case, Options, etc.?

  • Yeah, that’s exactly it

Show 3 more comments

1 answer

1


Where do you do:

if($value->nome_categoria === 'USERS') {
    $this->return .= 'USERS';
    $this->achouUsers = true;
    while (!$this->achouUsers){
        continue;
    }

}

if($value->nome_categoria === "OPTIONS"){
    $this->return .= 'OPTIONS';
    $this->achouUsers = true;
    while (!$this->achouOptions){
        continue;
    }
}

if( $value->nome_categoria === "MFP E CONNECTOR" ){
    $this->return .= 'MFP E CONNECTOR';
    while (!$this->achouMDF){
        continue;
    }
}

Like I said, it doesn’t make much sense. Since all the options are generated from the same repeat loop and you want the title to be displayed whenever you change the category, you can create an extra control variable that keeps the name of the current category and, when different, display the title again. Something like:

if ($value->nome_categoria != $categoriaAtual) {
    $this->return .= $value->nome_categoria;
    $categoriaAtual = $value->nome_categoria;
}

Remembering that before the foreach you need to start the variable:

$categoriaAtual = "";

Browser other questions tagged

You are not signed in. Login or sign up in order to post.