What logic to make this select work?

0

How do I make this select display according to what is selected there from the 'Latest, Evaluation, Utility' options ?:

Themostrecentwouldbethelastcommentsadded!Theevaluationwouldbethecommentthathadmorepositiveevaluationinstarsandwouldbelowering,forexample5,4,3etc...
AndtheUtilitywouldbethemostlikecomment.

ViewingNormalComments:

$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 I 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."";
}
    
asked by anonymous 17.01.2016 / 00:54

1 answer

1

It's quite simple: use ajax.

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.js"></script>$(function(){varopcoes=$('select[name=select_comentario]');opcoes.on('change',function(){$.get('sua_pagina_de_consulta.php',{ordenar:opcoes.val()},function(dados_retornados){//retorneosdadosatualizadospelaconsulta}});});
<select name="select_comentario"> <option value="tempo">Mais recente</option> <option value="utilidade">Utilidade</option> <option value="avaliacao">Avaliação</option> </select>

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

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

<?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 otherwise you have to mount the grid every time a sort order is made.

    
17.01.2016 / 04:02