title: PHP- assign value variable to each answer
body: <p>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.</p>
  
  <p>What I want to do:
  A scoring system that connects to a database, gives some choices for the user, eg age, education.</p>
  
  <p>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 &amp;&amp; if &amp;&amp; if, then" statements,</p>
  
  <p>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&lt;20, 10 points.
  Then: if education = highschool, 5 points. If education = university, 10 points.</p>
  
  <p>Which would be the best way to build such a "foreach" statement?</p>
  
  <pre><code>&lt;?php
  
  // Get a db connection.
  $db = JFactory::getDbo();
  
  // Create a new query object.
  $query = $db-&gt;getQuery(true);
  
  // Select all records from the user profile table.
  // Order it by the ordering field.
  $query-&gt;select($db-&gt;quoteName(array(FieldValue)));
  $query-&gt;from($db-&gt;quoteName('table'));
  $query-&gt;where($db-&gt;quoteName('SubmissionId') . ' = '. $db-&gt;quote('2'));
  $query-&gt;and($db-&gt;quoteName('FieldName') . ' = '. $db-&gt;quote('age'));
  </code></pre>
  
  <p>//  (Extra, but for later: I currently have submission id = 2. It should become submission id = the same as the last user submited).</p>
  
  <pre><code>// Reset the query using our newly populated query object.
  $db-&gt;setQuery($query);
  </code></pre>
  
  <p>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.</p>
  
  <pre><code>$row = $db-&gt;loadObjectList();
  echo nl2br("\n");
  echo $row['5']-&gt;FieldValue;
  echo nl2br("\n");
  echo $row['6']-&gt;FieldValue;
  </code></pre>
  
  <p>//this is my statement so far, which i need to improve. instead of echoing the value, i better assign variables to it.</p>
  
  <pre><code>echo nl2br("\n");
  
  if($row['5']-&gt;FieldValue=="&gt;20" &amp;&amp; $row['6']-&gt;FieldValue=="university" )
  {
      echo "15 points";
  }
  </code></pre>
  
  <p>//should be 5+10 from the variables, not just echo value.</p>
  
  <pre><code>else if($row['5']-&gt;FieldValue=="&gt;20" &amp;&amp; $row['6']-&gt;FieldValue=="high school" )
  {
      echo "10 points";
  }
  </code></pre>
  
  <p>//should be 5+5 from the variables, not just echo value.</p>
  
  <pre><code>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-&gt;loadObjectList();
  ?&gt;
  </code></pre>