0

What's wrong with this code? I'm getting a blank page for this?

<?php
define( '_JEXEC', 1 );
define('JPATH_BASE', __DIR__);
require_once ( JPATH_BASE .'/includes/defines.php' );
require_once ( JPATH_BASE .'/includes/framework.php' );
$app = JFactory::getApplication('site');
$db = JFactory::getDbo();
$user = JFactory::getUser();
$profile = JUserHelper::getProfile($user->id);

if( $user->username == 'THE_USER_1' )
{
    $query = $db->getQuery(true);
    $query->select($db->quoteName('username'));
    ->from($db->quoteName('#table_name#'));
    ->where($db->quoteName('username') . ' = '. $db->quote($user->username));
    $db->setQuery($query);

    $result = $db->loadResult();

if($result) { 
   echo "<p> You have already filled the form.</p>";
}
else {
    // continue to form page
}
elseif( $user->username == 'THE_USER_2' )
{
    // some code 
}
else
{
    echo "<p> Sorry, you can't access this page.</p>";
}
?>
  • A blank page generally means that there are PHP errors. I would highly recommend displaying errors while writing new code. The system will highlight exact lines where you are missing things like brackets and semicolons. – David Fritsch May 28 '14 at 16:13
  • @DavidFritsch .. the code is almost fixed! check the answer below. – saibbyweb May 28 '14 at 17:00
1

Firstly, you are missing a closing brackets in your if statement. Also, you are carrying on the database query having ended the previous line with a semi-colon. I don't know what you have also done when defining the database table, but you should replace #table_name# with #__table_name.

Copy and paste the code below which includes the missing closing bracket, removal of additional semi-colons and correct table name:

<?php
define( '_JEXEC', 1 );
define('JPATH_BASE', __DIR__);
require_once ( JPATH_BASE .'/includes/defines.php' );
require_once ( JPATH_BASE .'/includes/framework.php' );
$app = JFactory::getApplication('site');
$db = JFactory::getDbo();
$user = JFactory::getUser();
$profile = JUserHelper::getProfile($user->id);

if( $user->username == 'THE_USER_1' )
{
    $query = $db->getQuery(true);
    $query->select($db->quoteName('username'))
          ->from($db->quoteName('#__table_name'))
          ->where($db->quoteName('username') . ' = '. $db->quote($user->username));
    $db->setQuery($query);

    $result = $db->loadResult();

    if($result) { 
       echo "<p> You have already filled the form.</p>";
    }
}
elseif( $user->username == 'THE_USER_2' )
{
    // some code 
}
elseif( $user->username !== 'THE_USER_1' || $user->username !== 'THE_USER_2' )
{
    echo "<p> Sorry, you can't access this page.</p>";
}
else 
{
    echo "display form";
}
?>

Hope this helps

  • still getting a blank page? – saibbyweb May 27 '14 at 16:15
  • even when i access the page as "THE_USER_2" , i still get a blank page? – saibbyweb May 27 '14 at 16:25
  • I will need to do some tests when I get back from work. Will report back soon – Lodder May 27 '14 at 16:52
  • No issues Lodder! & btw thankyou, in advance. i know you'll find a solution to this problem. ;) – saibbyweb May 27 '14 at 17:26
  • Ahh, you had an elseif after an else statement which isn't allowed. I've edited my code accordingly. Please try again – Lodder May 27 '14 at 17:48

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.