When users login, I am trying to push them to fill their 3rd party component's Profile form properly.

When they filled the form their user id is added to the "#__rbid_users" table (user id is same as the Joomla user id).

This is what I made but there is something wrong - the code is not matching.

    $db = JFactory::getDbo();
    $query = $db->getQuery(true)
    $rbiduser = $db->loadObjectList();
if (in_array(joomlausers->id, $rbiduser->userid))

  echo '<html>
            <meta http-equiv="refresh" content="url=profile" />

The first problem is that you're using joomlausers->id which doesn't look correct. It should be $joomlausers->id (with a $ at the beginning).

Secondly, loadObjectList() returns an indexed array of PHP objects, so I don't believe in_array will work with this.

Instead, I'd suggest using $db->loadColumn(), which will return an array of all userid's, like so:

$db = JFactory::getDbo();
$query = $db->getQuery(true)

$array = $db->loadColumn();

if (in_array($joomlausers->id, $array, true))
    // Id exists

Note, do not start trying to inject custom metas anywhere as you've done in your current code. Instead use Joomla's redirect method as shown above.

Hope this helps

  • unfortunately it is not working. Even if id is exist still redirecting to the url – okisan Jul 18 '19 at 12:05
  • Is $joomlausers an instance of the JUser object? How are you defining this? – Lodder Jul 18 '19 at 12:19
  • It is my bad. Sorry, you are right. I had to change $joomlausers to $user->id and add $user = JFactory::getUser(); to the top – okisan Jul 18 '19 at 13:19

I don't think it is very direct to filter the result set when sql is perfectly qualified to do such logic.

As Lodder corrected, you also need to use setQuery(), fix your missing $, adjust your result set generating method call, and improve your redirect technique.

I've included some helpful diagnostic lines to help you to understand the query that is being generated and if there are any syntax errors, they will be displayed (so long as you don't run the redirect portion of your script).

After running my script, you may discover that:

  1. you have an sql error (for instance userid is not the same as user_id)
  2. you don't actually have the $joomlausers->id value that you think you have
  3. ...or something else that you haven't posted in your question is causing trouble.

Untested Snippet:

$app = JFactory::getApplication();
$user = JFactory::getUser();

if ($user->guest) {

try {
    $db = JFactory::getDbo();
    $query = $db->getQuery(true)
                ->where("userid = " . (int)$user->id);

    $app->enqueueMessage($query->dump(), 'info');
    //                   ^^^^^^^^^^^^^^ - use only during debuggin

    if (!$db->loadResult()) {  // loadResult() should return either 0 or 1
        // no error and no count, execute redirect
    } else {
        // id matched
} catch (Exception $e) {
    $app->enqueueMessage("Query Syntax Error: " . $e->getMessage(), 'error');
    //                 use only during debugging -^^^^^^^^^^^^^^^^

It is important that I state that you must never show the exact query or error messages to the public as a matter of site security!

To be clear, the enqueueMessage() calls are not required, they are just to assist your debugging.

  • You and @Lodder is right about it. Instead of $joomlausers i had to $user->id and add $user = JFactory::getUser(); to the top. Now it is working. Thank you for warning. – okisan Jul 18 '19 at 13:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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