2

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.

<?php 
    $db = JFactory::getDbo();
    $query = $db->getQuery(true)
            ->select($db->quoteName(array('userid')))
            ->from($db->quoteName('#__rbid_users'));
    $rbiduser = $db->loadObjectList();
if (in_array(joomlausers->id, $rbiduser->userid))
{

}
else
{
  echo '<html>
        <head>
            <meta http-equiv="refresh" content="url=profile" />
        </head>
        <body>';
}
?>
4

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)
    ->select($db->quoteName('userid'))
    ->from($db->quoteName('#__rbid_users'));
$db->setQuery($query);

$array = $db->loadColumn();

if (in_array($joomlausers->id, $array, true))
{
    // Id exists
}
else
{
    JFactory::getApplication()->redirect(JRoute::_('index.php?option=com_something&view=something'));
}

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 at 12:05
  • Is $joomlausers an instance of the JUser object? How are you defining this? – Lodder Jul 18 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 at 13:19
3

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:

$user = JFactory::getUser();

if ($user->guest) {
    JFactory::getApplication()->redirect(JRoute::_('index.php?option=com_something&view=something'));
}

try {
    $db = JFactory::getDbo();
    $query = $db->getQuery(true)
                ->select("COUNT(1)")
                ->from("#__rbid_users")
                ->where("userid = " . (int)$user->id);

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

    $db->setQuery($query);
    if (!$db->loadResult()) {  // loadResult() should return either 0 or 1
        // no error and no count, execute redirect
        JFactory::getApplication()->redirect(JRoute::_('index.php?option=com_something&view=something'));
    } else {
        // id matched
    }
} catch (Exception $e) {
    JFactory::getApplication()->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 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.