Name as number in Array foreach

Asked

Viewed 129 times

0

I have this foreach below that lists results of a Select, I would like to name according to the number, as I did below in IF, but it hasn’t worked yet..

foreach($result as $row)  
    {  

     $segurado[]           = $row['SEGURADO'];     
     $data[]           = $row['DATA'];     
     $status_seguro1       = $row['STATUS_SEGURO'];

        if($status_seguro1 == '1') {$status_seguro[] = 'Vistoria';}
        if($status_seguro1 == '2') {$status_seguro[] = 'Primeira Parcela';}
        if($status_seguro1 == '3') {$status_seguro[] = 'Apólice';}
        if($status_seguro1 == '4') {$status_seguro[] = 'Cancelado';}

    }
  • Not working means what? how the final format should look?

  • If it comes from the BD the number 1 in the status_seguro1 should be 'Survey' in the final format, and so on...

  • And what happens to that code? Doesn’t the writing go? The number goes?

  • is giving some error as I am getting the number '2' but is appearing written Policy (which is referenced to the number '3'

  • Use strtr http://php.net/manual/en/function.strtr.php

2 answers

1

Since in this ifs you are only making simple assignments, you can exchange this approach for an array, then just access the correct Indice and already assign the respective value.

$status = array(1 => 'Vistoria', 2 => 'Primeira Parcela', 3 => 'Apólice', 4 => 'Cancelado');
foreach($result as $row){  
    $segurado[] = $row['SEGURADO'];     
    $data[] = $row['DATA'];     
    $status_seguro[] = isset($status[$row['STATUS_SEGURO']]) ? $status[$row['STATUS_SEGURO']] : 'Inválido';
}

-1

Why not create a table with the status? So, in the future if you need to change a status or even include a new one, just include it in your BD. Without using the database the best option is to rray.

Browser other questions tagged

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