Customizing SMF > SMF Coding Discussion
db_query function problem
The Wizard:
I think I'm close on this.
--- Code: ---function onUser () {
global $smcFunc, $user_info;
$request = $smcFunc['db_query']('', '
SELECT shop_Dog
FROM {db_prefix}members
WHERE id_member = {int:member}
LIMIT 0,1', // it's good practice to limit your query length
array(
'member' => $user_info['id'],
)
);
// if user has data in shop_Dog show it on their posts only.
if (isset($message['member']['shop_Dog']))
{
$temp = $smcFunc['db_fetch_assoc']($request);
echo $temp["shop_Dog"];
}
}
--- End code ---
Sorck:
That will only show the 'dog' of the current viewing user ($user_info['id'] is the current user's ID, not that of the post in question).
The query above could result in an extra 25 queries a page (if you're set to 25 posts per page) all in templates I would assume. It might be more efficient to add it to the query in Display.php to include 'shop_Dog' in the FROM statement.
You might then need to make sure it's put in the $message['member'] array, again, in Display.php
EDIT: is this in the user's profile or under forum posts?
The Wizard:
Sorry I should have posted all my work to this point to give you all I better understanding of the problem
but I was up very late last night.
So here is what I have done so far:
just before the
--- Code: ---?>
--- End code ---
in Load.php I have the following function:
--- Code: ---//BEGIN SMFShop Item Cute Dog Image Version 0.1
function onUser ($id_member) {
global $db_prefix, $smcFunc, $user_info, $boardurl;
$request = $smcFunc['db_query']('', '
SELECT shop_Dog
FROM {db_prefix}members
WHERE id_member = {int:id_member}',
LIMIT 1', // it's good practice to limit your query length
array(
'id_member' => $id_member,
)
);
$temp = $smcFunc['db_fetch_assoc']($request);
if($temp['shop_Dog'] !== "") {
echo "Cute Dog: <img src='{$boardurl}/Sources/shop/dog_images/{$temp['shop_Dog']}'><br>";
}
}
//END SMFShop Item Cute Dog Image Version 0.1
--- End code ---
Also in Load.php in the $memberContext[$user] = array after this code:
--- Code: ---'gender' => array(
'name' => $gendertxt,
'image' => !empty($profile['gender']) ? '<img class="gender" src="' . $settings['images_url'] . '/' . ($profile['gender'] == 1 ? 'Male' : 'Female') . '.gif" alt="' . $gendertxt . '" />' : ''
),
--- End code ---
I added the following code:
--- Code: ---'dog' => onUser($profile['id_member']),
--- End code ---
Then in the Display.template.php after this code:
--- Code: ---// Show the member's gender icon?
if (!empty($settings['show_gender']) && $message['member']['gender']['image'] != '' && !isset($context['disabled_fields']['gender']))
echo '
<li class="gender">', $txt['gender'], ': ', $message['member']['gender']['image'], '</li>';
--- End code ---
I added the following code:
--- Code: ---// Show SMFShop Item Cute Dog Image Version 0.1
if (!empty($settings['show_gender']) && $message['member']['dog'] != '')
echo'
<li class="im_icons">', $message['member']['dog'], '</li>';
--- End code ---
I do get the result but not the way I want it displayed. See picture to understand.
Sorck:
The function onUser() doesn't return what's needed for the $memberContext[$user] array does it not? It only echo's out your data so therefore $memberContext[$user]['dog'] should equal null.
Or at least that appears to be the case. Making it return rather than echo'ing the string might be better?
The Wizard:
Hello:
I agree with this and came to the conclusion that the coded approach is the wrong way to go. Is there another simpler way to read data from the database in SMF?
Wiz
Navigation
[0] Message Index
[*] Previous page
Go to full version