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.

The test_complete column holds a binary value. 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 for 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
} else {
    $db->setQuery($query);
    $result = $db->loadObjectList();
    echo $query->dump();    // DELETE BEFORE MAKING CODE PUBLIC 
    echo "<p>" . $result->final_Ans ."</p>";
}      
  • 1
    Make sure when you test something like "if something is equal" to use the correct operators - For example, above you do not make a comparison rather you do an assignment. When you do "Something" = "Something-Else" Then you don't compare. – FFrewin Oct 11 at 3:55
  • 1
    The = isn't the only issue with the condition. The fact that the expression is double quote wrapped means that the comparison operator is just treated as any other character. See my PHP Demo link in my answer. – mickmackusa Oct 11 at 4:00
  • Thanks for the advice but I need a little clarification - so the quotes were wrong or the operator, the = was the wrong operator? I get confused by what is standard for the use of quotation marks because it seems people use them somewhat interchangeably. What rules do you follow, where can I find best practice for Joomla? – BabyCode123 Oct 11 at 19:24
  • 1
    Your incorrect conditional syntax is not a Joomla-thing, it is a php-thing. I added a php demo to my answer. You will need to research php's if statements, comparison operators, etc. We were all new at this once, just keep reading/experimenting. – mickmackusa Oct 11 at 20:10
up vote 1 down vote accepted

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")
                ->from("#__calc_results")
                ->where(["user_id = $user->id", "test_id = 1"]);
    $db->setQuery($query);
    $result = $db->loadObject();  // assuming you only want to access one row of data
    echo $query->dump();    // do not show to public
    echo "<pre>";
        var_export($result);
    echo "</pre>";
} catch (Exception $e) {
    JFactory::getApplication()->enqueueMessage("Query Syntax Error: " . $e->getMessage(), 'error'); // do not show to public
}

Output:

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

stdClass::__set_state(array(
   '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. Even if you could, "test_complete = '0'" wouldn't do what you require. PHP Demo
  • Make full use of Joomla's query building methods (from() and where()).
  • So that you don't have to write AND in your WHERE clause, you can declare the criteria as array elements.
  • Assuming you only want to access a single row of data, you should use loadObject() instead of loadObjectList()

Alternatively, if you only want the "Test not complete" string OR the final_Ans value, then this seems more appropriate:

try {
    $user = JFactory::getUser();
    $db = JFactory::getDbo();
    $query = $db->getQuery(true)
                ->select("IF(test_complete = 0, 'Test not complete', final_Ans)")
                ->from("#__calc_results")
                ->where(["user_id = $user->id", "test_id = 1"]);
    $db->setQuery($query);
    echo $query->dump();
    if (!$result = $db->loadResult()) {
        echo "No row found";
    } else {
        echo "<p>$result</p>";
    }
} catch (Exception $e) {
    JFactory::getApplication()->enqueueMessage("Query Syntax Error: " . $e->getMessage(), 'error');
}
  • This seems like good code but I am struggling to see the method of approach for how you come up with these things. After numerous iterations and research, it seemed that my approach at the top was logical because it first queried the necessary parameters, then when they are available in the memory, they are used to process the if statements. I tried incorporating both together as you have, but according to my code editor it was always a mess (not really surprised). Do you have any advice on how you approach coding in joomla? – BabyCode123 Oct 11 at 19:33
  • Thanks again for the help! It works! So is it likely for there to be a scenario where the "no row found" triggers? The default value for the test_complete column is 0, so I assume that will never trigger. Is this just good practice to have that? – BabyCode123 Oct 11 at 20:00
  • If the loggedin user doesn't have a test_id row with 1 in it, there will be no rows. The WHERE is the filtering clause of the query. – mickmackusa Oct 11 at 20:04

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.