If statement not executing

by BabyCode123   Last Updated October 11, 2018 03:10 AM

I am trying to setup a simple default statement on the results page of a testing system. This seems like it would be relatively simple but I cannot get my if statement to process properly. The Joomla logic does not seem to be functioning as I would expect and I cannot find clear documentation on it... test_complete is binary. If the test has been completed, the value in the database is 1, if 0 it is not. Currently it always defaults to 0, even though in the database the test has been completed and the test_complete column has registered a 1 under the user. Any help or places to start searching would be much appreciated! I'm new to this so I would not be surprised if I have a very poor approach but this is by far the best iteration of the last couple days.


$db = JFactory::getDbo();
$user = JFactory::getUser();
$query = $db->getQuery(true)
        ->select("test_complete, user_id, test_id, final_Ans from 
#__calc_results where user_id = $user->id AND test_id 
= '1'");

if("test_complete = '0'"){
echo "Test not complete";
echo $query->dump();    // DELETE BEFORE MAKING CODE PUBLIC
$result = $db->loadObjectList();
echo $query->dump();    // DELETE BEFORE MAKING CODE PUBLIC 
echo "<p>" . $result->final_Ans ."</p>";


Answers 1

Suggested Code:

try {
    $user = JFactory::getUser();
    $db = JFactory::getDbo();
    $query = $db->getQuery(true)
                ->select(["IF(test_complete = 0, 'incomplete', 'complete') AS test_complete", "user_id", "test_id", "final_Ans"])
                ->where(["user_id = $user->id", "test_id = 1"]);
    $result = $db->loadObject();  // assuming you only want to access one row of data
    echo $query->dump();    // DELETE BEFORE MAKING CODE PUBLIC
    echo "<pre>";
    echo "</pre>";
} catch (Exception $e) {
    JFactory::getApplication()->enqueueMessage("Query Syntax Error: " . $e->getMessage(), 'error');


SELECT IF(test_complete = 0, 'incomplete', 'complete') AS test_complete,user_id,test_id,final_Ans
FROM vwxyz_calc_results
WHERE user_id = 75 AND test_id = 1

   'test_complete' => 'complete',
   'user_id' => '75',
   'test_id' => '1',
   'final_Ans' => 'yep',

You cannot evaluate the value of a column before the query has returned the resultset.

October 11, 2018 03:08 AM

Related Questions

What's the proper chaining for WHERE

Updated January 29, 2018 18:10 PM

Display a Value from a Specific DB Field

Updated April 30, 2016 08:04 AM

Delete users from Joomla V2.5

Updated November 08, 2016 08:04 AM