Automatically add users name

16 posts by 5 authors in: Forums > CMS Builder
Last Post: November 11, 2009   (RSS)

Is there a simple way to add a user's name to a page?

I have members signed up who write film reviews. I want to be able to show the user;s name with the review, without them having to type it in every time. The name I want shown is the 'full name' as shown on the admin section.
High quality residential training for writers, actors & Film Making - Click Here for further information

Re: [chris] Automatically add users name

By Moonworks - August 29, 2009 - edited: August 29, 2009

Thanks. I tried that, but get the following error:

Notice: Undefined variable: productsRecord in /var/www/vhosts/horroruk.com/httpdocs/filmreviewsdetail.php on line 21 MySQL Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') ORDER BY fullname, username LIMIT 1' at line 3



Here is the code I have:

<?php

require_once "/var/www/vhosts/horroruk.com/httpdocs/admin/lib/viewer_functions.php";

list($film_reviewsRecords, $film_reviewsMetaData) = getRecords(array(
'tableName' => 'film_reviews',
'where' => whereRecordNumberInUrl(1),
'limit' => '1',
));
$film_reviewsRecord = @$film_reviewsRecords[0]; // get first record

// show error message if no matching record is found
if (!$film_reviewsRecord) {
print "Record not found!";
exit;
}
$accountNum = $productsRecord['createdByUserNum'];
list($accountsRecords, $accountsMetaData) = getRecords(array(
'tableName' => 'accounts',
'where' => "num = {$accountNum}",
'limit' => '1',
));
$accountsRecord = @$accountsRecords[0]; // get first record
?>

High quality residential training for writers, actors &amp; Film Making - Click Here for further information

Re: [Moonworks] Automatically add users name

By Chris - August 29, 2009

Hi Moonworks,

In my example, the detail page variable I'm using is called $productsRecord. In your detail page, I can see that your variable is called $film_reviewsRecord.

Simply change this line:

$accountNum = $productsRecord['createdByUserNum'];

to this:

$accountNum = $film_reviewsRecord['createdByUserNum'];
All the best,
Chris

Re: [chris] Automatically add users name

By Moonworks - August 29, 2009 - edited: August 29, 2009

Thanks, I should have seen that. These 17 hour working days are starting to get to me now I think [crazy]
High quality residential training for writers, actors &amp; Film Making - Click Here for further information

Re: [Moonworks] Automatically add users name

By Chris - August 29, 2009

Glad to hear you've got everything working! :D
All the best,
Chris

Re: [chris] Automatically add users name

By Moonworks - August 29, 2009 - edited: August 29, 2009

I got it to work great on the detail page, but it doesn't seem to want to do it on a list page. is there something different that needs to be on a list page?
High quality residential training for writers, actors &amp; Film Making - Click Here for further information

Re: [Moonworks] Automatically add users name

By Chris - August 29, 2009

Hi Moonworks,

This gets a little more complicated on a List page since you'll be doing many lookups.

Here's the code I used on my list page:

list($productsRecords, $productsMetaData) = getRecords(array(
'tableName' => 'products',
));

// collect a list of all createdByUserNum in this record set
function collectCreatedByUserNum($record) { return $record['createdByUserNum']; }
$accountNums = array_unique(array_map('collectCreatedByUserNum', $productsRecords));
// fetch only the account records we need
list($accountsRecords, $accountsMetaData) = getRecords(array(
'tableName' => 'accounts',
'where' => "num IN (" . join(",", $accountNums) . ")"
));
// index account records by num
function collectNum($record) { return $record['num']; }
$accountsRecordsByNum = array_combine(array_map('collectNum', $accountsRecords), $accountsRecords);
// add 'createdByUserNumAccount' key to records which links to account record
foreach ( array_keys($productsRecords) as $key ) {
$record =& $productsRecords[$key];
$record['createdByUserNumAccount'] = $accountsRecordsByNum[$record['createdByUserNum']];
}


Then, later in my page:

Author: <?php echo $record['createdByUserNumAccount']['fullname'] ?><br/>

You'll find the variable $productsRecords used several times above, make sure you replace them all with your own variable name. :)

Sorry the code is so complicated! If I think of a simpler way to do that, I'll post it here.

Hope this helps! Let us know if you have any questions or comments.
All the best,
Chris

Re: [chris] Automatically add users name

Thanks, got it all fully working now
High quality residential training for writers, actors &amp; Film Making - Click Here for further information

Re: [Moonworks] Automatically add users name

By Chris - August 29, 2009

Great to hear!

Keep up the good work! :)
All the best,
Chris