Sort words coming from Mysql BD according to their respective columns

Asked

Viewed 37 times

0

I have the following table:

tb_dispensa_medica

Iddispensa | Idempresa | Idusuario | PS | SS | TS | QS

PS, SS, TS and QS columns are CHAR(1) type that store S or N strings. I need to sort by name according to the respective columns, e.g.: First week (corresponds to the results in column PS), Second week (corresponds to the results in column SS), Third week (corresponds to the results in column TS) and Fourth week (corresponds to the results in column QS). For this I am trying as follows:

....
    $sqlVisualiza = mysqli_query($this->conexao,"SELECT * FROM pe_dispensa_medica WHERE IdUsuario = '".$peVisualizar->IdUsuario."'");
    while($peVisualiza = mysqli_fetch_object($sqlVisualiza)){
      if($peVisualiza->PS == 'S'){ $visualizar .= '<p>Primeira semana</p>'; }
      if($peVisualiza->SS == 'S'){ $visualizar .= '<p>Segunda semana</p>'; }
      if($peVisualiza->TS == 'S'){ $visualizar .= '<p>Terceira semana</p>'; }
      if($peVisualiza->QS == 'S'){ $visualizar .= '<p>Quarta semana</p>'; }            
    }
    ....
    return $visualizar;

Only he’s returning me this way:

inserir a descrição da imagem aqui

I tried the Sort as below, but did not succeed:

 if($peVisualiza->PS == 'S'){ $a = '<p>Primeira semana</p>'; }
 if($peVisualiza->SS == 'S'){ $b = '<p>Segunda semana</p>'; }
 if($peVisualiza->TS == 'S'){ $c = '<p>Terceira semana</p>'; }
 if($peVisualiza->QS == 'S'){ $d = '<p>Quarta semana</p>'; }            
 $array = array($a,$b,$c,$d);
 sort($array);
 foreach($array as $val){
         $visualizar .= $val;
 }

How could I order correctly?

  • Hello Bacco. Thank you for indicating some links, but none of them correspond to my problem, because I need to order the results according to the columns, that is, the results of PS come first, SS in second, TS in third and QS come last. I tried to use Sort(), but I did not succeed. It includes my new attempt in the post.

  • And that’s exactly what you have in the linked posts, just one ORDER BY ps="N",ss="N",ts="N",qs="N" no (possibly decreasing)?

  • And I took advantage edited the title and a part of the post for a better understanding.

  • Or if you prefer ORDER BY FIELD( 'S', ps, ss, ts, qs )

  • Although in this case it seems that you have a serious problem in modeling, but then you escape the question.

  • You’re right, Bacchus. I think the problem lies in modeling. I will reshape the table. I apologize for the lost time and thank you.

  • 1

    Test the order by field, I believe it solves if it is to keep the current modeling.

Show 2 more comments
No answers

Browser other questions tagged

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