Table of answers with real results

0

I put a test to some students, I have a software so that they enter with their username and password, they choose one of 5 answers that there is in the page and already that is uploaded to a database.

The tables that I have in the database are: One that has the 5 answers, another that has the information of the students, and another that relates the Student_ID with the Id_Response.

At this moment they are already voting, but the answers that have 0 elections do not appear to me. How do I print the entire table and the answers that have 0 choices?

<?php

include 'Conexion.php';

//$sql = mysql_query("SELECT * FROM 'respuestas' WHERE 'Id_Respuesta'= 3 ORDER BY 'Id_Relacion' ASC",$con_mysql);
//$cuenta = mysql_num_rows($sql);
//echo $cuenta;

$sql = "SELECT * FROM 'respuestas' WHERE 'Id_Respuesta'= 1 ORDER BY 'Id_Relacion' ASC";
$res = mysql_query($sql,$con_mysql);
$row = mysql_num_rows($res);

$sql1 = "SELECT * FROM 'respuestas' WHERE 'Id_Respuesta'= 2 ORDER BY 'Id_Relacion' ASC";
$res1 = mysql_query($sql1,$con_mysql);
$row1 = mysql_num_rows($res1);

$sql2 = "SELECT * FROM 'respuestas' WHERE 'Id_Respuesta'= 3 ORDER BY 'Id_Relacion' ASC";
$res2 = mysql_query($sql2,$con_mysql);
$row2 = mysql_num_rows($res2);

$sql3 = "SELECT * FROM 'respuestas' WHERE 'Id_Respuesta'= 4 ORDER BY 'Id_Relacion' ASC";
$res3 = mysql_query($sql3,$con_mysql);
$row3 = mysql_num_rows($res3);

$sql4 = "SELECT * FROM 'respuestas' WHERE 'Id_Respuesta'= 5 ORDER BY 'Id_Relacion' ASC";
$res4 = mysql_query($sql4,$con_mysql);
$row4 = mysql_num_rows($res4);

echo '<table border="1">
        <tr>
            <td colspan="2">Total de votos</td>
        </tr>
        <tr>
            <td>Candidatos</td>
            <td>Votos</td>
        </tr>';

if(($row = mysql_num_rows($res)) && ($row1 = mysql_num_rows($res1)) && ($row2 = mysql_num_rows($res2)) && ($row3 = mysql_num_rows($res3)) && ($row4 = mysql_num_rows($res4))) 
    {
    echo '<tr>
            <td> Messi </td>
            <td> '.$row.' </td>
        </tr>
        <tr>
            <td> Cristiano Ronaldo </td>
            <td> '.$row1.' </td>
        </tr>
        <tr>
            <td> David Ospina </td>
            <td> '.$row2.' </td></tr>
        <tr>
            <td> Arturo Vidal </td>
            <td> '.$row3.' </td></tr>
        <tr>
            <td> Alexis Sanchez </td>
            <td> '.$row4.' </td></tr>
    </table>';
    }
?>

I hope that with this little information you can help me.

    
asked by FOX 19.09.2017 в 15:33
source

1 answer

3

I think the problem with your code is precisely in the if you are commented on in one of the comments, for php the 0 is similar to a false. When you do the if that with the function to count rows, at the moment in which one of the queries returns 0 results (and since all the logical operations are and) the result is a false. That is why it is possible that it does not show you the table with the results if some value is 0. A possible solution is to always show the table (by removing the if) or modifying the if in the following way:

if  (($row = mysql_num_rows($res)) >= 0 && ($row1 = mysql_num_rows($res1)) >= 0 && ($row2 = mysql_num_rows($res2)) >= 0 && ($row3 = mysql_num_rows($res3)) >= 0 && ($row4 = mysql_num_rows($res4)) >= 0){...}
    
answered by 19.09.2017 / 16:42
source