Visualizzazione dei risultati da 1 a 2 su 2

Discussione: Gestione voti + Sql

  1. #1

    Gestione voti + Sql

    Buongiorno, ho realizzato un concorso a votazione, i voti variano da 1 a 5 e chi ha il voto più alto diviene vincitore. Il punto è questo, se si iscrive un utente (l'ultimo arrivato) e prende anche un unico voto pari a 5 , consegue il primo posto.

    Vorrei che dal db venissero presi coloro che hanno il voto più alto ma cn il maggior numero di voti ricevuti. Allora creo un campo e lo chiamo n_voti all'interno della tabella "voti".

    Come dovrei fare questo insert ?
    Il code che gestisce i voti è questo:

    if($saction==vota_2){
    $sel_us = mysql_query("SELECT * FROM utenti WHERE id = '$_GET[id]' ");
    $a_us = mysql_fetch_assoc($sel_us);

    $queryTot= "INSERT INTO voti (username, from_username, sesso, voto) VALUES('$a_us[username]', '$_SESSION[username]', '$_SESSION[sesso]', '$_GET[v]') ";
    $ris_totale = mysql_query($queryTot);


    $queryv = "SELECT SUM(voto) FROM voti WHERE username='$a_us[username]'";
    $queryv2 = mysql_query("SELECT * FROM voti WHERE username = '$a_us[username]' ");

    $resultv = mysql_query($queryv) or die(mysql_error());
    $num_voti = mysql_num_rows($queryv2);
    $nvc = "0";

    if($num_voti==0){
    $num_voti = "1";
    $nvc = "1";
    }


    $rowv = mysql_fetch_array($resultv);
    $media_voto = $rowv['SUM(voto)']/$num_voti;

    $num_voti = ($num_voti-$nvc);

    $queryTotM= "UPDATE utenti SET voto = '$media_voto' WHERE username = '$a_us[username]';";
    $ris_totaleM = mysql_query($queryTotM);



    ****************************
    Mentre in main.php ( ovvero la pagina dove vengono visualizzate le classifiche ) ho questo:

    $sel_mr = mysql_query("SELECT * FROM utenti WHERE sesso = 'M' ORDER BY voto DESC LIMIT 1 ");
    $a_mr = mysql_fetch_assoc($sel_mr);
    $sel_mr_foto = mysql_query("SELECT * FROM foto WHERE username = '$a_mr[username]' AND wi > 300 AND wi > he ORDER BY RAND() LIMIT 1 ");
    $a_mr_foto = mysql_fetch_assoc($sel_mr_foto);

    Grazie amici!

  2. #2
    Utente di HTML.it L'avatar di garlick
    Registrato dal
    Dec 2001
    Messaggi
    385
    Immagino che ogni utente possa ricevere più voti, quindi avrai una tabella del tipo:

    |id_utente| voto

    Che senso ha aggiungere un campo n_voti quando basta che fai un count(*) nella query?

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.