News:

Wondering if this will always be free?  See why free is better.

Main Menu

How can I show author on display template?

Started by dopeitspaul, July 28, 2014, 04:29:21 PM

Previous topic - Next topic

dopeitspaul

Hey everyone I was working on the display template. I rearranged the title and views on top of the context. Now I'm trying to show the author next to it but I'm not sure if it's possible.

Here's a pic: I drew a read square and typed in "started by"


I went on messageindex template and copied this code to my display template
', $topic['first_post']['member']['link'], '

But nothing happened. Is this possible? Please me know thanks!

kayeshandra85

Yeah, I am also looking for this one. I hope someone could help us.

Sir Osis of Liver

$topic is not defined in Display.template.php, so it doesn't work.  Looks like $context['first_message']['member'] will return the member id of the topic starter, and $scripturl . '?action=profile;u=', $context['first_message']['member'] will give you the profile link, but can't come up with member name.  Shouldn't have to query the database to get the name, but I'm not finding an easy way to do it, and it's late and I'm going to bed

 
 
Ashes and diamonds, foe and friend,
 we were all equal in the end.

                                     - R. Waters

dopeitspaul

Quote from: Krash on July 29, 2014, 02:00:11 AM
$topic is not defined in Display.template.php, so it doesn't work.  Looks like $context['first_message']['member'] will return the member id of the topic starter, and $scripturl . '?action=profile;u=', $context['first_message']['member'] will give you the profile link, but can't come up with member name.  Shouldn't have to query the database to get the name, but I'm not finding an easy way to do it, and it's late and I'm going to bed

 


Hey, thanks for replying and letting me know atleast. If you can find away please tell me.

I just figured out how to put the number of replies on the display template.

Sir Osis of Liver

Ok, after much dicking around, I came up with this -

Display.template.php



function template_main()
{
global $context, $settings, $options, $txt, $scripturl, $modSettings, $smcFunc;

$first_message = $context['first_message'];

$dbresult = $smcFunc['db_query']('', "
SELECT
p.id_member
FROM {db_prefix}messages as p
WHERE id_msg= $first_message");
$row = $smcFunc['db_fetch_assoc']($dbresult);

$author_id = $row['id_member'];

$dbresult = $smcFunc['db_query']('', "
SELECT
p.real_name
FROM {db_prefix}members as p
WHERE id_member= $author_id");
$row = $smcFunc['db_fetch_assoc']($dbresult);

$author_name = $row['real_name'];


echo ' first message # = ' . $context['first_message'] . '<br />';
echo 'author id = ' . $author_id . '<br />';
echo 'author name = ' . $author_name . '<br />';
echo 'link = ' . $scripturl . '?action=profile;u=', $author_id;



I'll be very surprised if there isn't an easier way to do it, and hope nobody throws rocks at me, but it works.  $context['first_message'] is the message id of the first post in the topic.  The first query uses it to retrieve the member id of the author, the second query gets the author's display name.  The echo statements will print the info at the top of the topic, so you can see what it is.  Don't forget to globalize $smcFunc.  Seems like a lot of work to do something very simple, but it's best I could do.

* Wait til Arantor sees this.
Ashes and diamonds, foe and friend,
 we were all equal in the end.

                                     - R. Waters

margarett

Se forem conduzir, não bebam. Se forem beber... CHAMEM-ME!!!! :D

QuoteOver 90% of all computer problems can be traced back to the interface between the keyboard and the chair

dopeitspaul

Quote from: Krash on July 29, 2014, 04:11:27 PM
Ok, after much dicking around, I came up with this -

Display.template.php


I'll be very surprised if there isn't an easier way to do it, and hope nobody throws rocks at me, but it works.  $context['first_message'] is the message id of the first post in the topic.  The first query uses it to retrieve the member id of the author, the second query gets the author's display name.  The echo statements will print the info at the top of the topic, so you can see what it is.  Don't forget to globalize $smcFunc.  Seems like a lot of work to do something very simple, but it's best I could do.

* Wait til Arantor sees this.

Worked out great man, thanks!
Here's what it looks like now


dopeitspaul


Arantor

Yes, it's bad style to conflate logic and presentation.

Sir Osis of Liver

I know, but it's easier to understand what it's doing, and easier to fix if it gets sideways, especially for inexperienced coders.

One thing I didn't think to check til after I got off yesterday, if $context['first_message'] is first message of topic or first message on page, in which case it may display different author on different pages.  Will have another look tonight.

Ashes and diamonds, foe and friend,
 we were all equal in the end.

                                     - R. Waters

Arantor

Why bother when you can just modify the query that gets $topicinfo and save yourself a query? You're already getting a huge bunch of stuff, another field+left join on mem.id_member or so is not a huge deal.

Sir Osis of Liver

Was going to look at that tonight.  Have to toddle off now.
Ashes and diamonds, foe and friend,
 we were all equal in the end.

                                     - R. Waters

margarett

Se forem conduzir, não bebam. Se forem beber... CHAMEM-ME!!!! :D

QuoteOver 90% of all computer problems can be traced back to the interface between the keyboard and the chair

Sir Osis of Liver

Ok, what I gave you last night displays the author of the first post on each page, so it may be different on different pages if topic is more than one page.

This will display topic starter on all pages, and eliminates a query (thx to Arantor) -

Display.template.php



function template_main()
{
global $context, $settings, $options, $txt, $scripturl, $modSettings, $smcFunc, $topicinfo;

$author_id = $topicinfo['id_member_started'];

$dbresult = $smcFunc['db_query']('', "
SELECT
p.real_name
FROM {db_prefix}members as p
WHERE id_member= $author_id");
$row = $smcFunc['db_fetch_assoc']($dbresult);

$author_name = $row['real_name'];



Don't forget to globalize $topicinfo.  Shouldn't have to change anything else you've done.
Ashes and diamonds, foe and friend,
 we were all equal in the end.

                                     - R. Waters

Arantor

WTF.

I was going to leave it at that, because I'm genuinely shocked at this, but no, let's go through it and explain why so we can stop the bad behaviours and encourage the good ones.

1. Queries in templates. NEVER DO THIS. This violates SMF's design tenet of separation of data processing and logic, it violates every tenet of sane programming in the first place. There is a reason this is one of the guidelines in the Cust rules. Rationalising it as 'it makes it easier to understand' doesn't change the fact that the entirety of SMF does it that way so if you want to modify SMF you still have to understand this principle in the first place and trying to hide it away actually makes it worse later on for debugging. Do it properly or don't do it at all.

2. I told you about $topicinfo not so you could still run a query. I told you about $topicinfo so you could add getting that data to it without any problems.

3. Since you are using a query against my recommendation at least DO IT PROPERLY. Encourage good habits rather than perpetuating bad ones.

SMF queries do not use double quotes for a reason. Aside from being slower, it's specifically to encourage you to use the query layer, which was specifically designed to handle parameterisation cleanly.

If you are going to unnecessarily add a query, at least do that the correct way:

$request = $smcFunc['db_query']('', '
SELECT mem.real_name
FROM {db_prefix}members as mem
WHERE id_member = {int:author}',
array(
'author => $topicinfo['id_member_started'],
)
);
list ($author_name) = $smcFunc['db_fetch_row']($request);
$smcFunc['db_free_result']($request);


This way we avoid allocating extra variables for no good reason which helps with not mashing the GC, we make use of the parameterisation, we use the standard taxonomy internal in SMF queries (where the heck did p come from as a table alias for members? mem is the standard alias, with memf and meml used for disambiguation in the rare instances we need to do that) and we correctly free the result. And we even follow the coding standards for indentation.

4. Even if point 3 is followed, this still gives you an edge case, in fact potentially multiple. If the account was deleted, or it was made by a guest, id_member_started will not match anything, even if you reattribute the post because 2.0 only fixes posts, it doesn't fix topics, requiring a maintenance run to do as well. If it is a guest post even after that, you are still stuffed because you never end up obtaining the poster_name attached to the first post which is used as a standby for when the actual data doesn't exist - and you get an error because db_fetch_* will not return anything you can actually use.


If you are unclear in any way about what I am suggesting, please do not assume you have understood the advice I have given and instead ask before giving out what I can only describe as bad advice encouraging bad habits.

Sir Osis of Liver



But .... it werks gud!


The query was adapted from a similar query that I've used elsewhere, that I originally adapted from one of the source files.  The p alias was in the original query, and works here.  If there are double quotes in the query, they are also in the original SMF query, I had no reason to change them. 

If id_member_started doesn't match anything, it should return a null value and the author name would be blank.  That's what it does if topic is started by guest.  Shouldn't be a big problem here, this is strictly a theme customization, not anything critical.

I rarely code from scratch anymore, for obvious reasons (among them, impaired vision and long term effects of brain damage). Double quotes/single quotes look the same to me (as do colons/semicolons, periods/commas, and some letters) even in large fonts I use for coding, unless I use a magnifier widget, which is a pia, so usually don't use one unless code doesn't work.  This code works.   It's a one-off, and there's no harm in making it simple for the OP (and for me).

I actually appreciate your taking the time to explain all this to me, and will tinker with it further and hopefully learn some new things (and maybe even remember a few).
Ashes and diamonds, foe and friend,
 we were all equal in the end.

                                     - R. Waters

Hj Ahmad Rasyid Hj Ismail

First message author is the topic author. I think we can make a simpler code to call / show the topic author. I will try to see whether I can come out with this in a simpler way.

Sir Osis of Liver

The topic starter is $topicinfo['id_member_started'], which is already available in $topicinfo from Display.php.  It's the id_member, so the only extra thing is to get real_name from smf_members, which is what the query does.  It's SIMPLE, and the code I posted is SIMPLE, and does exactly what the OP requested, even if it's not pretty.


Ashes and diamonds, foe and friend,
 we were all equal in the end.

                                     - R. Waters

live627

Quote
The query was adapted from a similar query that I've used elsewhere, that I originally adapted from one of the source files.  The p alias was in the original query, and works here.  If there are double quotes in the query, they are also in the original SMF query, I had no reason to change them. 
Yet that is clearly not code found in a stock SMF install.

Quote
But .... it werks gud!
I don't even... what?

Hj Ahmad Rasyid Hj Ismail

OP (dopeitspaul), I've been going through this and find that the easiest way is to call topic starter via $message['member']['link'] inside while ($message = $context['get_message']()). You will get its name and link this way. If you wish too have just a name then I think you can simply replace link with name.

But you can easily call the topic starter id at the place that you want (i.e. outside while ($message = $context['get_message']())) via $context['topic_starter_id'] but that displays only an id, not the topic starter name.

I also tried the following code too.

if ($message = $context['get_message']())
{
echo '
', $message['member']['name'], '';
}


While it displays the topic starter name, it has affected the first post (by not displaying the first post itself).

May be you get a better luck in trying all of the suggested code(s) earlier (after considering all of their arguments of course). Or may be my last code can be easily fixed too. ;)

Advertisement: