setQuery($query);$result = $db->loadObjectList();echo "hello";echo $result->username;?>username ?>" />district ?>" />"> [SOLVED] retrieve data from other table based on user login - Forums

Forums

[SOLVED] retrieve data from other table based on user login

jmarian1 23 Feb, 2012
Hi. I wanted to retrieve a data from a different table in database based on login user but somehow I can't make it work. This is how I wanted to get my data. Once the user click the submit button in the login page, I want to include the user name in the textbox and load/retrieve all data from another table based on the username automatically to the login redirect page like below but how can I do that? I guess my code is a mess. Please help. It's urgent. Thanks.

HTML Code to get data
<?php 
// Get user object -information from Joomla
$user= JFactory::getUser();
?>
Dear <input type="text" name="username" id="username" size="50" maxlength="40" value ="<?= $user->name; ?>" readonly />
<?php
$db =& JFactory::getDBO();
$query = "
    SELECT  'username','district'
        FROM `#__chronoforms_data_principal_project`
        WHERE `username` = $user";
$db->setQuery($query);
$result = $db->loadObjectList();
echo "hello";
echo $result->username;
?>
<input type="hidden" name="h_username" id="h_username" size="50" maxlength="40" value ="<?php $result->username ?>" />
<input type="hidden" name="h_district" id="h_district" size="50" maxlength="40" value ="<?php $result->district ?>" />
GreyHead 23 Feb, 2012
Hi jmarian1,

From a quick look this line
WHERE `username` = $user";
needs to be
WHERE `username` = '{$user->name}' ";
$user is an object and can't be used in MySQL directly and string values must be quoted.

Bob
jmarian1 23 Feb, 2012
Hi Bob,

I tried what you suggested and somehow still never give me data. I am able to get the user name, however, I am not successful in getting the data from the principal form based on the user name. Am I using the right code? Kindly help check my code please? I really need it badly to work. Below is the code with your suggestion. It is not grabbing the username data based from the user table to compare to my principal table:

<?php 
// Get user object -information from Joomla
$user= JFactory::getUser();
?>
Dear <input type="text" name="username" id="username" size="50" maxlength="40" value ="<?= $user->name; ?>" readonly />
<?php
$db =& JFactory::getDBO();
$query = "
    SELECT  'username','district','school_code'
        FROM `#__chronoforms_data_principal_project`
        WHERE `username` = '{$user->name}' ";
$db->setQuery($query);
$result = $db->loadObjectList();
echo "hello";
echo $result->username;
?>
<input type="text" name="h_username" id="h_username" size="50" maxlength="40" value ="<?php $result->school_code ?>" />
<input type="text" name="h_district" id="h_district" size="50" maxlength="40" value ="<?php $result->district ?>" />

P.S. I also tried to use the record loader using but somehow I have no luck. Please help. Thanks in advance.
GreyHead 23 Feb, 2012
Hi jmarian1,

Should it be $user->username instead of $user->name ??

Bob
jmarian1 23 Feb, 2012
HI Bob,

It took me a while to figure something out but making progress (yes!🙂). My code is working. It is showing the data that I want based on the login user. However, my problem is, how can I include a name tag and id tag in each <input> to use for validation? Please help. I really need this to work. Thanks.
<?php 
// Get user object -information from Joomla
$user= JFactory::getUser();
$name=$user->name;
$db =& JFactory::getDBO();
$query = "
    SELECT  `name`, `category_code`
        FROM `#__chronoforms_data_principal_project` 
        WHERE `name` = '{$name}' ";
$db->setQuery($query);
$data = $db->loadObjectList();
print_r ($data);
?>
<table>
<?php
foreach($data as $d) { 
echo "<tr><td>";
echo " <input value='".$d->name."' />";
echo "</td><td>";
echo "<input value='".$d->category_code."' />";
echo "</tr>";
}
?>
</table>

Result is:
[attachment=0]Screen Shot 2012-02-23 at 6.33.28 PM.png[/attachment]
GreyHead 24 Feb, 2012
Hi jmarian1,

But what is saved in the table? Is it the name or the username that is in the column that you are looking up in the principal_project table?

Oops I see that the post changed and I've replied to the old version.

Bob

PS In general it's better to use the User ID as a cross-reference as usernames can be changed.
GreyHead 24 Feb, 2012
Hi jmarian1,

Replying to the new post now.

You could do something like this
<?php
$i = 0;
foreach($data as $d) {
  echo "<tr><td>";
  echo "<input type='text' name='name_{$i}' id='name_{$i}' value='".$d->name."' />";
  echo "</td><td>";
  echo "<input type='text' name='cat_{$i}' id='cat_{$i}' value='".$d->category_code."' />";
  echo "</tr>";
  $i++;
}
?>

Bob
jmarian1 24 Feb, 2012
Thanks Bob. That's just what I need. Thanks a lot! 🙂
This topic is locked and no more replies can be posted.