News:

Bored?  Looking to kill some time?  Want to chat with other SMF users?  Join us in IRC chat or Discord

Main Menu

Adding A New Function?

Started by zoolman, February 21, 2011, 05:08:51 AM

Previous topic - Next topic

Arantor

I guess it *could* be but I don't see *why* it should. What's the entire content of your action function (and template_main() to go with it)?

zoolman

function ApprovalSubmit()
{
global $user_info, $topic, $smcFunc, $context;

$context['sub_template'] = 'main'
}

function template_main
{
echo 'Hello World';
}


I have stripped everything else out until I can get it working.

Arantor

I spy a syntax error there - should be a () after template_main.

zoolman

Well spotted. I'm still getting the 500 error though.

Arantor

What's in the server error log?

zoolman

Eureka!

It was another even more stupid typo mistake. It's also missing a semi-colon on the end of $context['sub_template'] = 'main'

Embarrassed at those 2 errors ;)

It now displays the Hello World text. So my function is being called. Not sure why it is working now because all I've done is clear the cache out but I'm not complaining.

Many Thanks for all your help. Checking the error log was the way to go. Dare I say it, I didn't even know to look in there.

zoolman

I now want to display a list of members that have clicked on one of the buttons with their avatar image to the left of their name.

I know I can do this by calling loadmemberdata. I can pass an array of all the member ids that I want to this function. However, this functions retrieves a lot more info than just the avatar which just seems like a waste of memory (and will be slower than neccessary). Is there a more efficient way of doing what I want?

Arantor

If it were just the name, I'd say do a table join from your table to the members table and get the real_name column. But you want avatar as well, in which case, calling loadMemberData and then loadMemberContext is actually the easiest and most maintainable way to do that.

zoolman

It just seems woefully inefficient to be getting all the extra info from the database that I don't need. But if that's the best way then it's the best way. I'll look into the loadmemberdata and loadmembercontext. Why do I need both?

Arantor

loadMemberData only queries the database, loadMemberContext does all the preparations on the avatar that you need it to do.

Really, though, fetching a dozen columns is not significantly more expensive than fetching one from a given table. You're going to need to go to at least two tables anyway to get the avatar...

zoolman

Perhaps I am missing the point but the way I see this working is this:

1. I get a list of all the members that have clicked on a button - really easy, just all rows from my table with the correct topic id
2. At this point I have populated my array with all the other info (up to here it is already done)
3. I now have to create an array of ids and pass it to loadmemberdata
4. Now for each entry in my proper array I can call loadmembercontext and populate the avatar field in my array

I think I am being thick because this all seems like more work than neccessary. I work with C++ and databases and it just wouldn't be this complicated.

Arantor

If you want to make the queries yourself, you're more than welcome to step through the query in loadMemberData and the follow up in loadMemberContext to extend the queries to join the members table and the attachments table, to get the member attachments. I just know how much code there is figuring out the attachments link, which you'd have to duplicate, not to mention rewrite at a later date as SMF progresses, which is the benefit of using a known, stable API.

zoolman

Okay so am I right in saying that I need to get all my data as I do now (into an array), then pass an array of ids to loadmemberdata before calling loadmembercontext on each id to then populate the avatar setting in my original array? Or am I misunderstanding how these two calls are used?

It's fine if this is the SMF standard way of doing things but it just seems very inefficient to what I am used to.

Arantor

Yes, that's what you do. Or as I said you're more than welcome to do it your way, just I won't write the code for you.

zoolman

 :)

I wasn't expecting you to. You've already done more than enough. Appreciate the help.

zoolman

So I'm getting the results from my table into two arrays - those that clicked approve and those that clicked disapprove. Simple enough so far. I populate 2 arrays for this. I've now added a third array which is just an array ids from all results. I pass this array to loadMemberData. Then for each item in each of my original arrays I call loadMemberContext passing it the member id. I then attempt to populate "real_name" and "avatar" into these arrays from $membercontext[id]['real_name] and $membercontext[id]['avatar'].

This isn't working as I just end up with blank strings. Anything obviously wrong?

Arantor

Is $memberContext being made global before calling that function?

zoolman

I just added it to the top of Display() but it doesn't make any difference.

Arantor

Well, Display.php does make use of loadMemberData and loadMemberContext already, have a look at the code used there...

zoolman

This is all almost working now. I can get the member info and populate memberContext['avatar']['image'] into my custom array. This is fine and I can display it. However, I always want to display the avatars with a width and height of 50 pixels. I know how to do this in the <img> tags but the content of memberContext['avatar']['image'] seems to be already wrapped in <img> tags.

How would I go about doing this?

Advertisement: