General Interests & Hobbies > Web Design/Development

Exam, Results Page! Efficiency!

Pages: (1/3) >>>

Frantix:

So basically, I have this online exam and this is where all of the results are done. Basically, how would I be able to improve on this? I think the code is sloppy and there would be a 100x more efficient way of marking this. Because by using the while statement it slows down the loading speed of the site by a fudge ton.

I'll break the code down for you below:

Ok, so this bit, basically it searches through the database and gives you either 1 or 0, 1 if you're correct 0 if you got it wrong. This is then used later on in the program.


--- Code: PHP ---for ($num=0; $num<=$quizquescount; $num++) {  $response2 = mysql_query("SELECT id, answer FROM questions WHERE category_id='$category' && id='$num'") or die(mysql_error());  $name = "quiz" .$category. "a";    while($result2=mysql_fetch_array($response2)) {        if ($_POST[$num]==$result2['answer']){          mysql_query("UPDATE users SET ".$name."".$num." = '1' WHERE userid = '$userid'");        } elseif($_POST[$num]!=$result2['answer']) {          mysql_query("UPDATE users SET ".$name."".$num." = '0' WHERE userid = '$userid'");        }      }   }    $right_answer = 0;  $wrong_answer = 0;  $unanswered = 0;   $total_questions = $right_answer + $unanswered + $wrong_answer;   $response = mysql_query("SELECT id, answer FROM questions WHERE category_id='$category'") or die(mysql_error());   while($result=mysql_fetch_array($response)) {       if($_POST[$result['id']]==$result['answer']) {               $right_answer++;           } else if($_POST[$result['id']] == 5) {               $unanswered++;           } else {               $wrong_answer++;      } } 
Ok, now this bit basically sorts the data so that they can be categorized. So basically the first row of the array might be category "Test1" and the second one would be category "Test2". This is so students can get marks like literacy and numeracy.

And the reason I use a switch was so that ever different exam has it's own categorys for example one might use "Test1" "Test2" and "Test3" as the categorys. Where about the other one uses "Test4", "Test5" and "Test6".


--- Code: PHP ---$user_score = mysql_query("SELECT * FROM users WHERE userid='$userid'") or die (mysql_error()); while ($scores=mysql_fetch_array($user_score)){ switch($category) {  case '1':   $arraysorts = array(    array("19", "18", "20", "21", "23", "34", "43", "44"),    array("2", "3", "6", "15", "16", "28", "29", "30"),    array("1", "4", "5", "33", "45"),    array("7", "10", "11", "12", "13", "24", "25", "27", "36", "37", "38"),    array("8", "9", "17", "26", "31", "39", "40"),    array("22", "25", "32", "35", "41", "42")  );   $allarrays = array($arraysorts[0], $arraysorts[1], $arraysorts[2], $arraysorts[3], $arraysorts[4], $arraysorts[5]);  $arraycounts = array();  $totalarrays = array(0, 0, 0, 0, 0, 0);  break;   case '2':   $arraysorts = array(    array("39", "38", "15", "21", "16", "10", "1", "2"),    array("18", "44", "19", "14", "25", "6", "5"),    array("4", "3", "8", "12", "13", "17"),    array("36", "43", "30", "31", "23", "22", "42", "35", "9"),    array("26", "37", "34", "29", "28", "33", "32", "7", "27"),    array("41", "45", "11", "40", "24", "20")  );   $allarrays = array($arraysorts[0], $arraysorts[1], $arraysorts[2], $arraysorts[3], $arraysorts[4], $arraysorts[5]);  $arraycounts = array();  $totalarrays = array(0, 0, 0, 0, 0, 0);  break;   case '3':   $arraysorts = array(    array("1", "10", "11", "16", "18", "24", "35", "36"),    array("2", "12", "15", "19", "26", "27", "30", "31", "32"),    array("3", "6", "9", "17", "25"),    array("7", "8", "13", "14", "21", "22", "34", "39", "40"),    array("4", "5", "20", "23", "28", "29", "33", "37", "38")  );   $allarrays = array($arraysorts[0], $arraysorts[1], $arraysorts[2], $arraysorts[3], $arraysorts[4]);  $arraycounts = array();  $totalarrays = array(0, 0, 0, 0, 0);  break;   case '4':   $arraysorts = array(    array("15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74"),    array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "58", "59"),    array("3", "6", "7", "8", "10", "11", "12", "13", "14", "15", "16"),    array("43", "47", "48", "53", "54", "59", "57", "58", "45"),    array("15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29"),    array("41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", "71")  );   $allarrays = array($arraysorts[0], $arraysorts[1], $arraysorts[2], $arraysorts[3], $arraysorts[4], $arraysorts[5]);  $arraycounts = array();  $totalarrays = array(0, 0, 0, 0, 0, 0);  break;}
Ok now this is the final piece, this piece sorts it into it's categorys by comparing the arrays and whether it's correct or not. Say it was correct then it will be added to the chosen category if not it would just ignore it.


--- Code: PHP ---$count = count($allarrays) - 1; $i = -1; while($i < $count) {  $i++;  $arraycounts[$i] = count($allarrays[$i]);} $o = -1; while($o < $count) {$o++;for($i = 0; $i < $arraycounts[$o]; $i++)    if($scores[$name . $arraysorts[$o][$i]] == '1') {      $totalarrays[$o]++;    }  } switch($category) {  case '1':    mysql_query("UPDATE users SET right_answer1 = '$right_answer', wrong_answer1 = '$wrong_answer', unanswered1 = '$unanswered', internet1 = '$totalarrays[0]', word1 = '$totalarrays[1]', general1 = '$totalarrays[2]', spreadsheets1 = '$totalarrays[3]', graphics1 = '$totalarrays[4]', programming1 = '$totalarrays[5]', quiz1comp = 1 WHERE userid = '$userid'");  break;   case '2':    mysql_query("UPDATE users SET right_answer2 = '$right_answer', wrong_answer2 = '$wrong_answer', unanswered3 = '$unanswered', internet2 = '$totalarrays[0]', word2 = '$totalarrays[1]', general2 = '$totalarrays[2]', spreadsheets2 = '$totalarrays[3]', graphics2 = '$totalarrays[4]', programming2 = '$totalarrays[5]', quiz2comp = 1 WHERE userid = '$userid'");  break;   case '3':    mysql_query("UPDATE users SET right_answer3 = '$right_answer', wrong_answer3 = '$wrong_answer', unanswered2 = '$unanswered', internet3 = '$totalarrays[0]', word3 = '$totalarrays[1]', general3 = '$totalarrays[2]', spreadsheets3 = '$totalarrays[3]', graphics3 = '$totalarrays[4]', quiz3comp = 1 WHERE userid = '$userid'");  break;   case '4':    mysql_query("UPDATE users SET right_answer4 = '$right_answer', wrong_answer4 = '$wrong_answer', unanswered4 = '$unanswered',  totcharoftech = '$totalarrays[0]', totcharoftechout = '$totalarrays[1]', charoftech = '$totalarrays[2]', charoftechout = '$totalarrays[3]', technum = '$totalarrays[4]', techlit = '$totalarrays[5]', quiz4comp = 1 WHERE userid = '$userid'");  break;  }
Please ask me if you are unsure about anything. PS I know the code is trash, I just mainly short cutted it and now I regret it cause it slowed down the loading speeds and just makes me feel horrible when I look at it. I guess I could add all the $arraysorts information into a database but that's about all I can think of thanks.


Bowser jr:

I do not know much about MySQL in PHP, but it looks like you have forgot to parameterize your queries, or make sure there are no unwanted symbols in the input. Basicly you need to make sure your code is secure from MySQL injection.

Ruby:

I've given you tips on PHP and database manipulation for about a year and you've never listened. Why should we give you any further help?

1) You're STILL using mysql_* functions. They have been deprecated since you were still shitting your pants and your parents wiping it all up.

2) Even with using those disgusting mysql_ functions you're not even protecting yourself from SQL Injection.

So after telling you about this at least twice before, why should we bother wasting our time helping you with your projects if you're not going to fudgeing listen? I hate this fudgeing forum it's full of autistic fudgeing moronic pricks

RuneAgent:

http://meekro.com/docs.php is sex

Frantix:


--- Quote from: Ruby on January 25, 2015, 10:06:45 AM ---I've given you tips on PHP and database manipulation for about a year and you've never listened. Why should we give you any further help?

1) You're STILL using mysql_* functions. They have been deprecated since you were still shitting your pants and your parents wiping it all up.

2) Even with using those disgusting mysql_ functions you're not even protecting yourself from SQL Injection.

So after telling you about this at least twice before, why should we bother wasting our time helping you with your projects if you're not going to fudgeing listen? I hate this fudgeing forum it's full of autistic fudgeing moronic pricks

--- End quote ---

So basically, if I fixed up all of my mysql, it would not only speed up my site it would protect me from SQL Injection? Honestly the only people who will be using this site at my school are Year 9's whom would be around 12-13 I can almost guarantee they wouldn't even know what sql injection is.. And is the rest of the code fine? the way I use the while statements to update information.

Pages: (1/3) >>>

Go to full version