PDA

View Full Version : Comparing Friend UserID


Infinity101
08-12-2012, 06:32 AM
Hello, I am in the process of writing a script which highlights buddy posts. I'm having trouble however passing a condition to only highlight the buddies and not everyone.

I have tried several conditionals, all either ending in showing no posts, or all posts. Below are variations of code I have used.

In myphp file:

When pulling information (used from misc.php):

$postbuddies = $db->query_read_slave("
SELECT
user.username, (user.options & " . $vbulletin->bf_misc_useroptions['invisible'] . ") AS invisible, user.userid, session.lastactivity
FROM " . TABLE_PREFIX . "userlist AS userlist
LEFT JOIN " . TABLE_PREFIX . "user AS user ON(user.userid = userlist.relationid)
LEFT JOIN " . TABLE_PREFIX . "session AS session ON(session.userid = user.userid)
WHERE userlist.userid = {$vbulletin->userinfo['userid']} AND userlist.relationid = user.userid AND type = 'buddy'
ORDER BY username ASC, session.lastactivity DESC
");


When Comparing

if(in_array(postbuddies['userid'], array(memberpost['userid'])){}


Pulling in separate instance (the above would be removed):
postbuddies = explode(' ', $buddies);
and in template:
<vb:if condition="is_array($memberpost[userid], array($postbuddies))"></vb:if>

I have tried several other variations similar to the above. I have ran a loops to make sure I was gathering the buddy userid which was successful. The problem seems to be when comparing with a poster's userid.

I hope my post makes sense, it's very late night for me and at this point I feel like my mind is going bonkers. Than you kindly for any help.

kh99
08-12-2012, 01:53 PM
You don't need array() in the conditional, it should be:
<vb:if condition="is_array($memberpost[userid], $postbuddies)"></vb:if>


But that will only work if you register $postbuddies to the template. A way around that would be to add it to an array that is already registered, like:
$memberpost[postbuddies] = $postbuddies;


then
<vb:if condition="is_array($memberpost[userid], $memberpost[postbuddies])"></vb:if>


I should also mention that rendered posts are cached, so that may also give you trouble. With the post caching, you can't really have the background change based on something that is specific to the user viewing the post.

Infinity101
08-12-2012, 04:55 PM
Hi kh99, thank you for your response. This is not the first time you have helped me and I'm very thankful.

If I do not include the array() in the conditional, I receive a malformed conditional error when compiling.

<vb:if condition="in_array($memberpost[userid], $memberpost[postbuddies])">

If I do this (below) I receive the same error even when including array().
<vb:if condition="is_array($memberpost[userid], $memberpost[postbuddies])">

In the PHP I have:

$postbuddies= explode(' ', trim($userinfo['buddylist']));


$memberpost[postbuddies] = $postbuddies;

Thank you again for taking the time in teaching me.

kh99
08-12-2012, 05:06 PM
Sorry, one thing I didn't notice - you should be using in_array(), not is_array().

Which hook is your plugin using, and which template is your conditional in?

Infinity101
08-13-2012, 02:46 AM
Hi, I'm using misc_buddylist_start for the hook. I'm using a module from vbadvanced cmps, more or less similar to the "News" module if you're familiar with it.

Thanks!

Infinity101
08-27-2012, 05:13 AM
I continue to have difficulty with this. Any input? Thanks!

kh99
08-27-2012, 11:29 AM
One very important thing I missed the first time around - you can't directly use the return value of query_read_slave(), it won't be an array. If you're only expecting one row from your query, you should call query_read_first(), which will return an array representing the first row returned from the query.

Infinity101
08-27-2012, 04:12 PM
Excellent, thank you kh99. I'm going to try this out.

--------------- Added 1346099203 at 1346099203 ---------------

Thank you kh99, I have had more success. I wanted to verify whether or not I was pulling the information correctly through the php hook. I used print_r to see the array. When using query_first(); unfortunately their was no return. When using query_ready() was able to return the array however.


Array ( [0] => 111 ) Array ( [0] => 2379 ) Array ( [0] => 103 ) Array ( [0] => 28 ) Array ( [0] => 6 ) Array ( [0] => 49 ) Array ( [0] => 192 ) Array ( [0] => 2399 )

while ($buddy = $db->fetch_array($buddys))
{
if ($doneuser["$buddy[userid]"])
{
continue;
}
$buddylist= explode(' ', trim($buddy['userid']));
$comment['buddylist'] = $buddylist;
print_r($comment['buddylist']);
$doneuser["$buddy[userid]"] = true;

}



When comparing in the template however, I receive this error:


<vb:if condition="in_array($comment[buddylist], $comment[userid])">A friend posted!</vb:if>
The following error occurred when attempting to evaluate this template:
%1$s
This is likely caused by a malformed conditional statement. It is highly recommended that you fix this error before continuing, but you may continue as-is if you wish.

Do you think this is a problem with the template reading the variable? Or should I use a different method in comparing the values?

Thanks for your continued support.