Problem in select order

Asked

Viewed 68 times

3

I fed this graph

inserir a descrição da imagem aqui

But I have a problem with his selection

<?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, He’ll get the first records. I saw in some places that it is possible to make a select within the select, but it did not work either.

  • check on the chart that should have option to invert the display

  • If your question has been answered, please choose the answer to finalize the question.

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
  • There’s a way to fix it. Hold on.

  • Good... But I think it’s ASC. Remember that the result is... 05/05, 06/05, 07/05..

  • 1

    Well remembered, missed fix the second order by. Thanks @Andreicoelho

2

You can use the 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()

2

In addition to the other answers, to do increasingly (ASC) just put the beginning of the date that will pick us record and order by date.

By date:

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

Your code would look 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));
  • 1

    I do not disagree with the reply of @Davidalves is good, but DESC will bring the last results, not reverse them

  • @Weessmith has rasão. He would have to put in ASC only this.

  • He wants ASC results reversed, so I suggested array_reverse

  • @Weessmith alias, if it puts in ASC it will have to start from a date anyway...

  • @Weessmith he wants the dates increasing 05/05, 06/05, 07/05... right?

  • 1

    Yes, it can put another condition on WHERE tbm, just as you suggested, tbm will run as expected

  • @Exact Weessmith. We think alike then =)

  • 1

    Yes, and I believe that using the date is more effective, by not needing two loops as in my example

  • @Weessmith and actually his answer (david) is wrong.

  • Yes, you are wrong

  • I disagree with you :P Since it is not very well explained the real interest of the query, from what I understand he wants the results of the last 7 days to display on the chart. That way, taking DESC by date and limit 7 solves his problem. In the case of the question, he would like to reverse the results he has precisely because he obtained the last 7 days in the reverse order to the desired.

  • @Davidalves would solve if it were of descending order.. the result he wants is... 05/05, 06/05, 07/05.... ie, increasing.

  • @Davidalves this result he already has... 11/05, 10/05, etc...

  • 1

    True, I corrected my answer.

  • I told you about the @Davidalves bug but I was making the same mistake. kkkkk

  • 1

    It happens hahaha

Show 11 more comments

Browser other questions tagged

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