Multisort array setting in PHP

Asked

Viewed 25 times

0

opa, everything right?! I took this script to give continuity and has the select that brings me the database data. I’m having difficulty in doing Multisort.

<?php

$sql = mysql_query("select if(extra50 = 0, '', extra50) AS extra50, if(extra100 = 0, '', extra100) AS extra100, if(extra50not = 0, '', extra50not) AS extra50not, if(extra100not = 0, '', extra100not) AS extra100not, ds_nome from resumida_" . $cd_ano . " where cd_mes = " . $cd_mes . " ", $conexao);
while ($RR = mysql_fetch_array($sql)) {

    $motorista[$m]['nome'] = strtoupper($RR["ds_nome"]);
    $motorista[$m]['hr50'] = $RR["extra50"];
    $motorista[$m]['hr100'] = $RR["extra100"];
    $motorista[$m]['hr51'] = $RR["extra50not"];
    $motorista[$m]['hr101'] = $RR["extra100not"];

    if ($cd_ordenar == 50) {
        foreach ($motorista as $res)
            $sortAux[] = $res['hr50'];
    }
    if ($cd_ordenar == 51) {
        foreach ($motorista as $res)
            $sortAux[] = $res['hr51'];
    }
    if ($cd_ordenar == 60) {
        foreach ($motorista as $res)
            $sortAux[] = $res['hr60'];
    }
    if ($cd_ordenar == 100) {
        foreach ($motorista as $res)
            $sortAux[] = $res['hr100'];
    }
    if ($cd_ordenar == 101) {
        foreach ($motorista as $res)
            $sortAux[] = $res['hr101'];
    }
    if ($cd_ordem == 1) {
        array_multisort($sortAux, SORT_DESC, $motorista);
    }
    if ($cd_ordem == 0) {
        array_multisort($sortAux, SORT_ASC, $motorista);
    }
    foreach ($motorista as $vl) {

        $html .= "<tr>";
        $html .= "<td>" . $vl['nome'] . "</td>";
        $html .= "<td align='center'>" . MinToHrMinB($vl['hr50']) . "</td>";
        $html .= "<td align='center'>" . MinToHrMinB($vl['hr100']) . "</td>";
        $html .= "<td align='center'>" . MinToHrMinB($vl['hr51']) . "</td>";
        $html .= "<td align='center'>" . MinToHrMinB($vl['hr101']) . "</td>";
        $html .= "</tr>";

        $tot50 += $vl['hr50'];
        $tot51 += $vl['hr51'];
        $tot100 += $vl['hr100'];
        $tot101 += $vl['hr101'];
    }
}

$cd_sort and $cd_order comes from a header to sort as described in the script. I tried some ways printing on the screen to see how I was progressing but I didn’t succeed. He just doesn’t order. Always keep the same result scheme.

1 answer

0

I ended up solving changing the way the array was created and throwing the rest of the code out of while():

while($RR = mysql_fetch_array($sql)){

    $motorista[] = array('nome'=>strtoupper($RR["ds_nome"]), 'hr50'=>$RR["extra50"], 'hr100'=>$RR["extra100"], 'hr51'=>$RR["extra50not"], 'hr101'=>$RR["extra100not"] );

}

if($cd_ordenar == 50){foreach($motorista as $res) $sortAux[] = $res['hr50'];}
if($cd_ordenar == 51){foreach($motorista as $res) $sortAux[] = $res['hr51'];}
if($cd_ordenar == 60){foreach($motorista as $res) $sortAux[] = $res['hr60'];}
if($cd_ordenar == 100){foreach($motorista as $res) $sortAux[] = $res['hr100'];}
if($cd_ordenar == 101){foreach($motorista as $res) $sortAux[] = $res['hr101'];}

if($cd_ordem == 1){array_multisort($sortAux, SORT_DESC, $motorista);}
if($cd_ordem == 0){array_multisort($sortAux, SORT_ASC, $motorista);}
foreach($motorista as $vl){
    if($vl['hr50'] != 0 || $vl['hr51'] != 0 || $vl['hr60'] != 0 || $vl['hr100'] != 0 || $vl['hr101'] != 0){
        $html .= "<tr>";
        $html .= "<td>".$vl['nome']."</td>";
        $html .= "<td align='center'>".MinToHrMinB($vl['hr50'])."</td>";
        $html .= "<td align='center'>".MinToHrMinB($vl['hr100'])."</td>";
        $html .= "<td align='center'>".MinToHrMinB($vl['hr51'])."</td>";
        $html .= "<td align='center'>".MinToHrMinB($vl['hr101'])."</td>";
        $html .= "</tr>";

        $tot50 += $vl['hr50'];
        $tot51 += $vl['hr51'];
        $tot100 += $vl['hr100'];
        $tot101 += $vl['hr101'];
    }
}

Browser other questions tagged

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