The Basics
The function of this code is to take a user id (hard coded in this example) and pull up that user's ranking in 4 subjects. The users rankings are then used to pull any questions from a questions table which have a ranking less than that of the user's ranking in the 4 subjects. That part I've accomplished.
The questions
The next step will be to take the user's ranking, and subtract the users rank in each subject from each of the questions rank in each subject, then adding those together to generate a question score.
The code I have below calculates the value between the the user ranking and the question ranking for subject.
1. I've done it the manual non-looped way, and have tried to write some loops, but haven't had any luck. I tried adding the $x variables to an array, and tried to use another for loop inside the foreach, but am not sure how to create a variable to hold each subject value in order to calculate the separate values for each subject, so that I can add them together. My first thought is to create a temporary array for each of the subtracted values, then another entry for the sum. Should I be looking at that, or is there a better easier way?
for example:
$score = array('$userRating->subject_1 - $rows[1]', $userRating->subject_2 - $rows[2]...
2. After being able to calculate the question score my first thought is to try to add it to the $entry array as an additional value for each of the rows (which are arrays themselves, not sure if thats the right terminology). I should then be able to sort the $entry array by those values to select the question with the lowest score (and therefore closest to the users ranking.) Is that a good direction, or should I be looking at some other way?
<pre>
<?php
$mysql = new mysqli("localhost", "root", "pass", "development");
$getUserRating = $mysql->query("SELECT subject_1, subject_2, subject_3, subject_4 FROM user_rating WHERE user_id=6");
$userRating= $getUserRating->fetch_object();
print_r($userRating);
?>
<br><br>
<?php
$getQuestions = $mysql->query("SELECT ques_id, subject_1, subject_2, subject_3, subject_4 FROM questions WHERE subject_1 <= $userRating->subject_1 AND subject_2 <= $userRating->subject_2 AND subject_3 <= $userRating->subject_3 AND subject_4 <= $userRating->subject_4");
$entry = $getQuestions->fetch_all();
foreach($entry as $rows)
{
print_r($rows);
$x1 = $userRating->subject_1 - $rows[1];
$x2 = $userRating->subject_2 - $rows[2];
$x3 = $userRating->subject_3 - $rows[3];
$x4 = $userRating->subject_4 - $rows[4];
$y = $x1+$x2+$x3+$x4;
echo($y . "</br>");
}
?>
</pre>
Output from current code
stdClass Object
(
[subject_1] => 1
[subject_2] => 2
[subject_3] => 3
[subject_4] => 4
)
Array
(
[0] => 1
[1] => 1
[2] => 1
[3] => 1
[4] => 1
)
6
Array
(
[0] => 5
[1] => 1
[2] => 2
[3] => 3
[4] => 4
)
0
Array
(
[0] => 24
[1] => 1
[2] => 2
[3] => 2
[4] => 2
)
3
Array
(
[0] => 40
[1] => 1
[2] => 2
[3] => 1
[4] => 4
)
2
The DB Schema
questions
Field Type Null
ques_id int(11) No
ques_text text
subject_1 int(11)
subject_2 int(11)
subject_3 int(11)
subject_4 int(11)
user_rating
Field Type Null
user_id int(11) No
subject_1 float
subject_2 float
subject_3 float
subject_4 float
users
Field Type Null
user_id int(11) No
user_name varchar(40)
This post has been edited by auromed: 16 August 2010 - 02:24 PM

New Topic/Question
Reply




MultiQuote





|