Problem in the order of select

3

I fed this chart

ButIhaveaprobleminselectingit

<?php$sql_2_vis=mysqli_query($config,"SELECT data, uniques, pageviews FROM tb_visitas ORDER BY id DESC LIMIT 7") or die(mysqli_error($config));

    if(@mysqli_num_rows($sql_2_vis) <= '0'){
        echo "
            <div class=\"col-lg-6\">
                <div class=\"alert alert-danger\">
                    <strong>Erro!</strong> $erro
                </div>
            </div>                  
        ";  
    }else{
        $count_vis = 0;
        while($r_sql_2_vis = mysqli_fetch_array($sql_2_vis)){
            $data_sel      = $r_sql_2_vis[0];
            $uniques_sel   = $r_sql_2_vis[1];
            $pageviews_sel = $r_sql_2_vis[2];

            if($count_vis++) echo ', ';
            echo '["' . date("d/m", strtotime($data_sel)) . '",' . $uniques_sel . ']';
        }
    }
?>

It would be necessary to display, in this case, 05/05, 06/05, 07/05 ... And it's the other way around.

If I give a ORDER BY id ASC , it will get the first records. I have seen in some places that it is possible to do a select within the select, but it did not work either.

    
asked by anonymous 11.05.2018 / 14:02

3 answers

4

The following query will work:

SELECT data, uniques, pageviews 
FROM ( SELECT data FROM tb_visitas ORDER BY data DESC LIMIT 7 ) aux 
ORDER BY aux.data ASC 
LIMIT 7
    
11.05.2018 / 14:09
2

You can use array_reverse() :

$count_vis = 0;
while($r_sql_2_vis = mysqli_fetch_array($sql_2_vis)){
    $row[]=array('data_sel'=>$r_sql_2_vis[0],'uniques_sel'=>$r_sql_2_vis[1],'pageviews_sel'=>$r_sql_2_vis[2]);
}
$row=array_reverse($row);
foreach ($row as $key => $value) {
    $data_sel      = $value['data_sel'];
    $uniques_sel   = $value['uniques_sel'];
    $pageviews_sel = $value['pageviews_sel'];

    if($count_vis++) echo ', ';
    echo '["' . date("d/m", strtotime($data_sel)) . '",' . $uniques_sel . ']';
}
  

Function reference array_reverse ()

    
11.05.2018 / 14:43
2

In addition to the other answers, to do so incrementally (ASC) you simply put the beginning of the date that will take up the registry and sort by date.

By date:

SELECT data, uniques, pageviews FROM tb_visitas 
WHERE data >= '05/05/2018' 
ORDER BY data ASC LIMIT 7

Your code looks like this:

$dataInicial = "05/05/2018";
$sql_2_vis = mysqli_query($config, "SELECT data, uniques, pageviews FROM tb_visitas WHERE data >= '$dataInicial' ORDER BY data ASC LIMIT 7") or die(mysqli_error($config));
    
11.05.2018 / 19:02