What is the logic to make this select work?

Asked

Viewed 74 times

0

How do I make this select display according to what is selected there among the options 'Most recent, Rating, Utility'?:

The most recent would be the last comments added!
The Evaluation would be the comments that had more positive rating in stars and would download, for example 5, 4, 3 etc...
And Utility would be the most like comments.

Normal comment display:

$selecionarComentarios = $conexao->prepare("SELECT a.id_mark, a.id_user, a.comment, a.rate, a.id, a.active, b.name, b.avatar FROM tb_comment a, users b WHERE a.id_user=b.id AND a.id_mark = :post_id ORDER BY id DESC LIMIT $start_pg, $amount_pg");
$selecionarComentarios->bindParam(':post_id',$post_id, PDO::PARAM_INT);
$selecionarComentarios->execute();

Via GET got what I wanted!!

    if (isset($_GET["op_com"]))
{
    $op_com = $_GET["op_com"];
    if($op_com=="recent")
    {
        $select_comment = "SELECT a.id_mark, a.id_user, a.comment, a.rate, a.id, a.active, b.name, b.avatar FROM tb_comment a, users b WHERE a.id_user=b.id AND a.id_mark = :post_id ORDER BY active DESC LIMIT ".$start_pg.", ".$amount_pg."";
    }
    if($op_com=="rating")
    {
        $select_comment = "SELECT a.id_mark, a.id_user, a.comment, a.rate, a.id, a.active, b.name, b.avatar FROM tb_comment a, users b WHERE a.id_user=b.id AND a.id_mark = :post_id ORDER BY rate DESC LIMIT ".$start_pg.", ".$amount_pg."";
    }
    if($op_com=="good")
    {
        $select_comment = "SELECT a.id_mark, a.id_user, a.comment, a.rate, a.id, a.active, a.good, b.name, b.avatar
    FROM tb_comment a, users b
    WHERE a.id_user=b.id AND a.id_mark = :post_id ORDER BY good DESC LIMIT ".$start_pg.", ".$amount_pg."";
    }
}
else
{
    $select_comment = "SELECT a.id_mark, a.id_user, a.comment, a.rate, a.id, a.active, b.name, b.avatar FROM tb_comment a, users b WHERE a.id_user=b.id AND a.id_mark = :post_id ORDER BY id DESC LIMIT ".$start_pg.", ".$amount_pg."";
}
  • 1

    You can use Javascript to only rearrange comments. Making a new query or a call to a php resource just for this, at least in my opinion is unnecessary. Since there is no code to base and give an answer, stay there as a comment a way I would :)

  • 1

    Orde by in consultation?

  • I’ll post the PHP code to display those comments there.

  • @rray Ala posted the SELECT that I use to pull the current comment from the photo.

1 answer

1

It’s very simple: use ajax.

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.js"></script>

$(function(){
  var opcoes = $('select[name=select_comentario]');
  opcoes.on('change', function(){
    $.get('sua_pagina_de_consulta.php', { ordenar: opcoes.val() }, function( dados_retornados ){
      //retorne os dados atualizados pela consulta
    }
  });
});
<select name="select_comentario">
  <option value="tempo">Mais recente</option>
  <option value="utilidade">Utilidade</option>
  <option value="avaliacao">Avaliação</option>
</select>

Of course, on the page where you do the query in the database you have to pass the query parameters to query.

When the user selects the option, the option values are sent by jQuery to the query page and the query page sorts:

<?php
//nao esquecer da conexao com o banco de dados :p
$ordernar_por = filter_input(INPUT_GET, 'ordenar');

if($ordenar_por == 'avaliacao'){
  //selecione e ordene por avaliacao
}else if($ordenar_por == 'utilidade'){
  //selecione e ordene por utilidade
}else{
  //selecione e ordene por data
}

On the client side, you can use something that generates HTML dynamically, because if you don’t you have to mount the grid every time a sort is made by select.

  • We got a serial downvoter around here!!!

  • That part I already got, via GET, now only with difficulty to put in order the comments that have more like

  • 1

    You are storing the "Ikes" in the database, right?! Then sort in the query. Whenever you pass the query parameter, you put it to sort by this parameter. Something likeSELECT * FROM comentarios ORDER BY '$likes' DESC can be ASC. Descending and ascending order, in that order.

  • 1

    Yes, I made some changes and got :D thank you!!

Browser other questions tagged

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