Forums

Almost there ...

gtownwebdev 27 Dec, 2009
I have my second-ever Chronoform up and running and calling data from the Joomla User table as well as a secondary table. The "District" field is from another table, and it's dependent on the user_id. That's working (thanks to reading these forums and trying stuff out):

http://draft.masc.org/index.php?option=com_chronocontact&chronoformname=RegistrationAdmin&user_id=92

That user has "Georgetown" for their District, and that shows up properly.

But I'm already using getUser in the form itself, so I can call the username and email up without any special URL handling. Isn't there some way to display their District too without having to actually put something in the URL?

If I must put the user_id in the URL, how do I add that in the link dynamically?

Thanks!
GreyHead 27 Dec, 2009
Hi gtownwebdev,

If you have accessed the Joomla User object with say $user =& JFactory::getUser(); then the name is availalbe as $user->name and the email as $user->email, etc.

Use print_r with the User Object to see all the variables; or check the Joomla docs.

Bob
gtownwebdev 27 Dec, 2009
You mis-understood my question. I can get the user information, that's all set. I want to know if, since I'm already getting the user information via getUser, and I've linked another table (not jos_user) using the Profile plugin ... can I get fields from that OTHER table without having to call the user_id in the URL?

In other words, it seems roundabout to be calling the user_id from the URL, just to show one field from a linked table, when I already have the user id available to me in the form, via getUser. Is the only way to do that really via the URL? If so, how do I call the user_id in the URL dynamically? I only know the hard-coded version:

http://draft.masc.org/index.php?option=com_chronocontact&chronoformname=RegistrationAdmin&user_id=92

Thanks!
GreyHead 29 Dec, 2009
Hi gtownwebdev,

Sorry for the delay - it's been the Xmas holidays over here and my wife has had other plans for me . . .

You don't need the user id in the url = you can look it up at the beginning of the Form HTML if that is what you need. Search here on 'getUser' for dozens of examples.

Bob
gtownwebdev 29 Dec, 2009
I AM using getUser. That's how I'm pulling in the name and e-mail. But I can't seem to pull in District, which is from ANOTHER table, without putting the user ID in the URL.

Here's the code:
Your Name: <b><?php echo $user->name; ?></b> *<br>
Your Email: <b><?php echo $user->email; ?></b> *<br>
Your District: <b>{District}</b> *<p>

Name and Email show up for any logged-in user. But District is from another table, and only shows up if user ID is in the URL. I tried changing District to:
Your District: <b><?php echo $add_masc_member->District; ?></b>

and that doesn't work. Is there a way to get District (which is NOT in jos_user) without calling it via the URL?
GreyHead 29 Dec, 2009
Hi gtownwebdev,

Add the MySQL query to look it up:
<?php
$user =& JFactory::getUser();
$db =& JFactory::getDBO();
$query = "
  SELECT `district`
    FROM `#__some_other_table`
    WHERE `user_id` = ".$user->id." ;
";
$db->setQuery($query);
$district = $db->loadResult();
?>
. . .
Your District: <b><?php echo $district; ?></b>

Bob
gtownwebdev 29 Dec, 2009
Yes, thank you! That's what I didn't know how to do. It's not working, but it must just be a syntax error:


<?php
$user =& JFactory::getUser();
$db =& JFactory::getDBO();
$query = "
  SELECT 'District'
    FROM '\__add_masc_member'
    WHERE 'user_id' = ".$user->id." ;
";
$db->setQuery($query);
$district = $db->loadResult();
?>
and then below:
<?php echo $district; ?>


The field is definitely "District" and the table is "add_masc_member." Is there a syntax error in there somewhere? I don't know much PHP although I'm learning as I go along with Chronoforms here. What's the "\__" in front of the table name do? I tried it without and no effect.

Sorry, I know it's not your job to troubleshoot PHP/mySQL queries for people. I'm so close though -- if I could get this (and the data save issue from my other thread), the form would be 100%!
GreyHead 29 Dec, 2009
Hi gtownwebdev,

You are correct, the \__ should be #__ (my keyboard has switched from UK English to US English for no apparent reason). :-(

Bob
gtownwebdev 29 Dec, 2009
I had tried that earlier -- saw the # on another post in the forum and figured might as well see. Tried it again now and it still doesn't return a result. Then I thought maybe there's an extra semicolon after the WHERE but whether it take it out or leave it, no luck.

Arrgghh!!!! What can it be??!!!!!!

I don't even know how to debug the PHP part.

Does the Profile plugin have any effect here (or can it be of any help)? I had been using it to get that District field -- so it's enabled and has cf_user_id as the target and user_id as the parameter. Maybe that's doing something?
GreyHead 29 Dec, 2009
Hi gtownwebdev,

Sorry I missed that you had changed the quotes. You *must* use back-ticks (or possibly no quote)round column and table names in the query (straight quotes are only for strings).
$query = "
  SELECT `District`
    FROM `#__add_masc_member`
    WHERE `user_id` = ".$user->id." ;
";

Bob
gtownwebdev 29 Dec, 2009
I didn't know that! WHOOPS. I thought the formatting got corrupted! I fixed that and thought NOW it will work ... but no.

Then I realized maybe the match must be wrong. I think it should be cf_user_id, like this:

WHERE `cf_user_id` = ".$user->id." ;

Because the field in add_masc_member that matches the id field in jos_user is cf_user_id. I should have realized that earlier.

But ... still nothing is being returned. Maybe it's not pulling in the jos_user ID right? I don't even know how to debug it -- I just keep going in and changing things without really knowing what I'm doing. I have probably gone in and changed something and saved that form 200 times today. I'm at a total loss with what should be such a simple query!!!!!! Please, please, take a look at the code and help me. I don't know what to do.
GreyHead 29 Dec, 2009
Hi gtownwebdev,

It's late here now. The easiest thing to do is to turn on Site debug and look for the query in the output, copying and pasting it into PHPMyAdmin may help to see an error message if there isn't an obvious error.

Bob
gtownwebdev 30 Dec, 2009
I got it! I was reading how to use the database classes in the Joomla documentation (yes, I was that desperate) and it mentioned the cross-hatch # is for the prefix. Except I didn't give my table a prefix (maybe I was supposed to). So this works:


<?php
$user =& JFactory::getUser();
$db =& JFactory::getDBO();
$query = "SELECT `District` FROM add_masc_member WHERE `cf_user_id` = ".$user->id.";
";

$db->setQuery($query);
$district = $db->loadResult();
?>


!! Thanks for helping me through it.
GreyHead 30 Dec, 2009
Hi gtownwebdev,

Glad you got this working.

The table prefix (usually 'jos_') is used to identify all of the Joomla related tables in the current database schema. It can be changed so that you could have more than one copy of joomla running from the same schema with each identifying it's own tables from the prefix.

The prefix is set on installation and can be changed in the site Global Configuration. All the Joomla database functions know to change it for the current site value before passing the query to MySQL.

Bob
This topic is locked and no more replies can be posted.