I have created a PHP form with values that are inserted in the database. Fields dropdown boxes that are passed to the database after submit. According to what the user chooses in the dropdown box, score increases. It Contains age (dropdown box, >20 <20), degree (high school, university), etc.
What I want to build,is a function to add score values according to the choice of the dropdown box. Make specific collumn values a variable which adds scores points.
Every time someone inserts a specific choice, it should get a different amount of points. Pseudocode, i for score:
For user john doe:
i = 0.
1) If dropdown.age>20 (choice 1), 5 points, i= i +5.
If dropdown.age<20 (choice 2) 10 points, i= i +10
2) If dropdownchoicebox.education = high school, 5 points
if choice = university, 10 points.
If John Doe > 20 and university
then
i = 5 + 10 = 15 points.
The values are derived from database.
Each insert is in a different row in each record. Ie john doe is the first 5 records. For instance, we ignore rows 1-4, age would be row 4, education row 5, etc.
My code so far, I have made it display the database rows that I need, but I was unable to connect it to a score function. How should I proceed?
<?php
// Get a db connection.
$db = JFactory::getDbo();
// Create a new query object.
$query = $db->getQuery(true);
// Select all records from the user profile table.
// Order it by the ordering field.
$query->select($db->quoteName(array(name, surname, age, education)));
$query->from($db->quoteName('rsform_submission_values'));
// Reset the query using our newly populated query object.
$db->setQuery($query);
$row = $db->loadObjectList();
print_r($row['2']);
print_r($row['3']);
print_r($row['4']);
print_r($row['5']);
// Load the results as a list of stdClass objects (see later for more options on retrieving data).
$results = $db->loadObjectList();
?>
<!DOCTYPE html>
<html>
<body>
//tried for session
<?php
if (!isset($SESSION['counter'])) $SESSION['counter'] = 0; //initialize session counter to zero
if (isset($POST['submit'])) { $SESSION['counter']++; } //increments counter by 1 when submit button is clicked
echo " session counter is " . $_SESSION['counter']; //echoes current value of counter
?>
<form name="input" action="" method="post" />
<input type="submit" name="submit" value="submit" />
</body>
</html>
//tried for session
<?php
session_start();
if(empty($SESSION['count'])) $SESSION['count'] = 0;
$order = $SESSION['count']+1;
$SESSION['count'] = $order;
echo "<form action='study.php' method='GET'>
<input class='big_b' type='submit' value='next' />
</form>";
echo "$order";
?>
//tried increment/decrement
<doctype html>
<html>
<head>
<title>Increment/Decrement</title>
<script type="text/javascript">
function incrementButtons( upBtn, downBtn, qtyField )
{
var step = parseFloat( qtyField.value ) || 1,
currentValue = step;
downBtn.onclick = function()
{
currentValue = parseFloat( qtyField.value ) || step;
qtyField.value = ( currentValue -= Math.min( step, currentValue - step ) );
}
upBtn.onclick = function()
{
currentValue = parseFloat( qtyField.value ) || step;
qtyField.value = ( currentValue += step );
}
}
</script>
</head>
<body>
<form id="f1">
<input type='text' name='qty' id='qty' value='4' readonly = 'readonly'/>
<input type='button' name='add' value='+'/>
<input type='button' name='subtract' value='-'/>
</form>
<script type="text/javascript">
with( document.getElementById( 'f1' ) )
incrementButtons( add, subtract, qty );
</script>
</body>
</html>
//Modified the results for convenience. Not the real numbers.
Result: array value
Array ( [0] => over 20 [1] => highschool [2] => result 2[3] => result3
Names of rows: [0] = age, [1] = education.
What I want to do:
Button onclick: calculate points
function calculate points
//i for score
i=0
As 0 = over 20, add 5 points ( i=0+5 = 5).
i=5.
as 1 = highschool i= 5+5 = 10
$i should be reset for when I choose new user.