Confuse displaying Data from Database

i want to display data from database. i want to display 4 tables named ‘score’, ‘listening_score’, ‘reading_score’ and ‘structure_score’. ‘score’ table contained final score.

index.php

<? php
   echo "Your TOEFL Score, <br>";
         echo "<table class=\"score\">";
         $email = $_SESSION['email'];
         $q = mysql_query("SELECT * FROM score where email='$email'");
         $query = mysql_query("SELECT * FROM structure_score where email='$email'");
         $query1 = mysql_query("SELECT * FROM listening_score where email='$email'");
         $query2 = mysql_query("SELECT * FROM reading_score where email='$email'");
         while ($row = mysql_fetch_array($q)) {
            echo "
               <h3> <span >".$row['Score']."</span> </h3>";

         while ($row = mysql_fetch_array($query)) {
            echo "
               <tr><td>Score Structure: </td> <td>".$row['Score']."</td><td>Right Answer: </td> <td>".$row['Right_Answer']."</td></tr>";   

         while ($row = mysql_fetch_array($query1)) {
            echo "
               <tr><td>Score Listening: </td> <td>".$row['Score']."</td><td>Right Answer: </td> <td>".$row['Right_Answer']."</td></tr>";

         while ($row = mysql_fetch_array($query2)) {
            echo "
               <tr><td>Score Reading: </td> <td>".$row['Score']."</td><td>Right Answer: </td> <td>".$row['Right_Answer']."</td></tr>";
               }
             }
           }
         }
        echo "</table><br><br>";
?>

i want to display:

233
Score Structure:    24  Right Answer:   0
Score Listening:    24  Right Answer:   0
Score Reading:     22 Right Answer:   0

BUT when i test twice, data will be displaying like this:

233

263
Score Structure:    24  Right Answer:   0
Score Listening:    24  Right Answer:   0
Score Reading:  22  Right Answer:   0

Score Reading:  31  Right Answer:   12
Score Listening:    33  Right Answer:   10
Score Structure:    32  Right Answer:   8

How to make data displaying like this:

233
Score Structure:    24  Right Answer:   0
Score Listening:    24  Right Answer:   0
Score Reading:  22  Right Answer:   0

263
Score Reading:  31  Right Answer:   12
Score Listening:    33  Right Answer:   10
Score Structure:    32  Right Answer:   8

Is there just a single record in each table for a given email address? If there isn’t, what’s the structure of each table?

Is the value for email that is grabbed from the session array ultimiatly supplied by the user, if it is, have you validated it somewhere?

Are you aware that the mysql_* extension that you’ve used was removed from version 7 of PHP. You should now be using either the mysqli_* extension or PDO (whichever one of them two you move to, you should always use prepared statements)

Yes, if you can describe how the database tables relate to each other, there’s probably a much easier way to handle it, perhaps with a single query.

You’re making many assumptions in that code - you’re not sorting the data in any way, so you have no control over what order it comes out, and hence what order it displays. Unless there’s a lot of other data in each table, I’d also have thought that keeping the three individual scores as columns in the same table might make things easier. And personally, I don’t like the use of the same $row[] variable to retrieve separate data in loops that are nested within each other, though in this specific example it’s not causing any problems.

Maybe something like

SELECT score.score AS s_score
 , structure_score.score AS st_score
 , listening_score.score AS l_score
 , reading_score.score AS r_score
FROM score, structure_score, listening_score, reading_score
WHERE score.email LIKE ?
AND structure_score.email LIKE ?
AND listening_score.email LIKE ?
AND reading_score.email LIKE ?

followed by a
bind_param('ssss', $validated_email_address, $validated_email_address, $validated_email_address, $validated_email_address);

I don’t like having a field in a table being the same name as the table.

Without knowing the database structure, it’s hard to know how the title line (called $row['Score']) related to the individual scores that need to be grouped below it (which are also called $row['Score']) - presumably the title row is some kind of question ID that would be used in ordering the data.

ok i will try it soon

hi, i need your opinion guys. i want to make toefl test. fisrt, user do structure test, then system will save structure score in table ‘structure_score’. then user automatically do listening test, then system will save listening score in table ‘listening_score’. then user automatically do reading test and system will save reading score in ‘reading_score’ then take structure and listening score from their tables and do calculating to get final score and save it in table ‘score’.

the first time i do the test. the score will be calculate correctly, but when i do the test twice, the calculate will not correct. structure score and listening score was false. please give me some advice

Show us the code you use to calculate and store the score.

so i calculate the score after user do the last test, that is reading test

reading_result.php

<?php
              include "connection.php";
              $right = 0;
              $wrong = 0;
              if (isset($_POST["question"])){
              foreach($_POST['question'] as $key
              => $value){
                  $cek = mysql_query("SELECT * FROM reading WHERE no=$key");
                  while($c = mysql_fetch_array($cek)){
                      $answer = $c['answer'];
              } 
              if($value==$answer){
                  $right++;
              }else{
                  $wrong++;
              }
              
              } 
              $count
              = $_POST['cquestion'];
              $unanswer = $count - $right - $wrong;
              if ($right==50) {
                $score=67;
              }elseif ($right==39) {
                $score=66;
              }elseif ($right==39) {
                $score=65;
              }elseif ($right==39) {
                $score=63;
              }elseif ($right==39) {
                $score=61;
              }elseif ($right==45) {
                $score=60;
              }elseif ($right==44) {
                $score=59;
              }elseif ($right==43) {
                $score=58;
              }elseif ($right==42) {
                $score=57;
              }elseif ($right==41) {
                $score=56;
              }elseif ($right==40) {
                $score=55;
              }elseif ($right==39) {
                $score=54;
              }elseif ($right==38) {
                $score=54;
              }elseif ($right==37) {
                $score=53;
              }else {
                $score=22;
              } 


              $email = $_SESSION['email'];
              $simpan = "INSERT INTO reading_score VALUES ('$email', '$count', '$right', '$wrong', '$score')";
              $listening ="SELECT * FROM listening_score WHERE email='$email'";
              $ex=mysql_query($listening);
              $data=mysql_fetch_array($ex);
              $Listening = $data ['score'];
              $Right_listening = $data['right'];

              $structure ="SELECT * FROM structure_score WHERE email='$email'";
              $ex=mysql_query($structure);
              $data=mysql_fetch_array($ex);
              $Structure = $data ['score'];
              $Right_structure = $data['right'];
            
              $final_score = (($Listening + $Structure + $score)/3) * 10; 

              $NA = "INSERT INTO score VALUES ('$email', '$final_score', '$Listening', '$Structure', '$score', '$Right_listening', '$Right_structure', '$right')";
              if(mysql_query($simpan) && mysql_query($NA)){
                   header("location:index.php");
              }else {
                    echo mysql_error();
              }
              }

              ?>

I am confused by this section of code:

              if ($right==50) {
                $score=67;
              }elseif ($right==39) {
                $score=66;
              }elseif ($right==39) {
                $score=65;
              }elseif ($right==39) {
                $score=63;
              }elseif ($right==39) {
                $score=61;
              }elseif ($right==45) {
                $score=60;
              }elseif ($right==44) {
                $score=59;
              }elseif ($right==43) {
                $score=58;
              }elseif ($right==42) {
                $score=57;
              }elseif ($right==41) {
                $score=56;
              }elseif ($right==40) {
                $score=55;
              }elseif ($right==39) {
                $score=54;
              }elseif ($right==38) {
                $score=54;
              }elseif ($right==37) {
                $score=53;
              }else {
                $score=22;
              } 

In particular I don’t understand why there are so many conditions for if $right==39, though I don’t know if it makes any difference to your expected results.

Other questions:

  1. What is $nilai and where does it come from? You use it in the score calculation.
  2. When you say it doesn’t calculate correctly, what’s wrong with it? What should it calculate, and what does it calculate instead?

that was how to count toefl score, if the questions were answered as many as 40 questions that will be converted to be 55. then user score is 55 not 40.

  1. i am sorry that was typo. i have repair it.

  2. doesn’t calculate correctly that mean if i do the test twice, listening score taken from table ‘listening_score’ and structure score taken from table ‘structure_score’ was wrong. listening and structure score taken was score when i do the test at the first time. not the score when i do the test at the second time. but the reading score was right.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.