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:
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.
– user24136
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)?– Bacco
And I took advantage edited the title and a part of the post for a better understanding.
– user24136
Or if you prefer
ORDER BY FIELD( 'S', ps, ss, ts, qs )
– Bacco
Although in this case it seems that you have a serious problem in modeling, but then you escape the question.
– Bacco
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.
– user24136
Test the order by field, I believe it solves if it is to keep the current modeling.
– Bacco