THIS IS THE ORIGINAL, EDITED IN MY NEXT ANSWER BELOW: I have sent the original code in next answer with very few changes. Ask me for changes or clarifications if i missed something.
What I want to do: A scoring system that connects to a database, gives some choices for the user, eg age, education.
What I have done so far: connect to database, and echo values accordingly to each choice. However, it is not convenient to make too many "if && if && if, then" statements,
What I want to improve: It is much better to build a "foreach" statement, so I have a variable that gives points accordingly to each answer. If age>20, 5 points, if age<20, 10 points. Then: if education = highschool, 5 points. If education = university, 10 points.
Which would be the best way to build such a "foreach" statement?
<?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(FieldValue)));
$query->from($db->quoteName('table'));
$query->where($db->quoteName('SubmissionId') . ' = '. $db->quote('2'));
$query->and($db->quoteName('FieldName') . ' = '. $db->quote('age'));
// (Extra, but for later: I currently have submission id = 2. It should become submission id = the same as the last user submited).
// Reset the query using our newly populated query object.
$db->setQuery($query);
rows 5,6 are age, education, etc, more will be added but i need to find a way to improve this after i fix the "foreach" statement.
$row = $db->loadObjectList();
echo nl2br("\n");
echo $row['5']->FieldValue;
echo nl2br("\n");
echo $row['6']->FieldValue;
//this is my statement so far, which i need to improve. instead of echoing the value, i better assign variables to it.
echo nl2br("\n");
if($row['5']->FieldValue==">20" && $row['6']->FieldValue=="university" )
{
echo "15 points";
}
//should be 5+10 from the variables, not just echo value.
else if($row['5']->FieldValue==">20" && $row['6']->FieldValue=="high school" )
{
echo "10 points";
}
//should be 5+5 from the variables, not just echo value.
else
{
echo "not variables given";
}
echo nl2br("\n");
// Load the results as a list of stdClass objects (see later for more options on retrieving data).
$results = $db->loadObjectList();
?>