MyBB Community Forums

Full Version: Custom PM functions problem.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I wrote a plugin recently, everything works fine except sending PMs to all.

Notice that I did worked with PM function in past and it did sent PM to multiple users, but this time, I'm fetching user's list directly by querying uids of users from db.

It won't send PM to everybody.

Fetching the value of uid:

while($person = $db->fetch_array($query))
{
$uids = $person['uids'];
}

Say, currently, this query returns 3 rows of uids, for example, uid 1,2,3 . Then I have certain part of PM function like this:

$touid = array(explode(",", (intval($uids))));

and in toid options, I have $touid, like this:

	"toid" => $touid,

I tried all possible possibilities I could, defining toid as array() and using $pm['toid'] = $touid when I have $touid value as intval($uids); but it won't work.

As I said, if I was to fetch the PM uids/username directly from plugin settings, it would have worked as I'm already using it for one plugin of mine.

But here, fetching values from db and sending those uids PM directly doesn't seem to work.

Any help is appreciated, am stuck at this.

Thank you.
while($person = $db->fetch_array($query))
{
$uids = $person['uids'];
}

Needs to be

$uids = array();
while($person = $db->fetch_array($query))
{
$uids[] = $person['uids'];
}

For all users to be picked up. Unless there is some voodoo you're not mentioning.
I tried that, still no luck. Sad

Here's a rough of my code:

$query = $db->query("SELECT column1,column2 FROM ".TABLE_PREFIX."tablename WHERE column='".$uid."'");	

if($db->num_rows($query) > 0)
{

$users = array();
while($person = $db->fetch_array($query))
{

$users[] = $person['column2'];

    
	require_once MYBB_ROOT."inc/datahandlers/pm.php";
	
	$pmhandler = new PMDataHandler();
		

	$pm = array(
		"subject" => "Subject",
		"message" => "Message",
		"icon" => -1,
		"fromid" => 0,
		"do" => '',
		"pmid" => ''
	);
	
	$pm['toid'] = $users;

	$pm['options'] = array(
		"signature" => 0,
		"disablesmilies" => 0,
		"savecopy" => 0,
		"readreceipt" => 0
	);
	$pm['saveasdraft'] = 0;
	$pmhandler->admin_override = 1;
	$pmhandler->set_data($pm);
	if($pmhandler->validate_pm())
	{
		$pmhandler->insert_pm();
	}
	else
	{
		return false;
	}
	
	return true;
  
}
}
you are overwriting the $users array every call. You need to move $users=array(); outside the while loop
Oh, sorry I forgot to mention it. I tried keeping it outside the while loop as well, but still it did sent PM only to one person out of the 3 queries.
your return functions are also inside the while loop. You need to change the return false; to continue; so the loop keeps going and then move the return true; to outside the next brace
We're little close, haha.

I've tried interchanging return false; to continue; (both creating similar scenario which I'd mention below) and moved return true; outside the while loop.

Now what happens is the system sends 3 PMs (to each user), to all list of users, in a fashion such as the latest PM received shows sent to user1, second showing sent to user1, user2, and the third one showing sent to user1, user2 and user3.

So what we actually need to eliminate now is stopping the latest 2 PMs, from being sent, because the first one is on the spot and mentions the correct list of users that were sent PM.

Thanks.
so you do want to do not want the recipients to see the whole list of users?

if you want to send all the users the same pm and they are all listed in the recipient list then:

move the pm code outside (after) the while loop and just use the while loop to build the users array.

if you want the same pm sent to each user individually and without listing the other users, then go back to what you had originally and just move the return true; after the while loop and change the return false; to continue;
Fixed, haha.

It was silly of me to keep while loop open till end and not close it right after I've fetched the variables.

What I told in last post of mine was that it was indeed sending PM to all (showing whom it sent PM to isn't/wasn't an issue), but it was sending 3 repeated PMs to each user in the list. So if there were 3 users to send PM to, it was sending 3 x 3 = 9 users instead of one per each.

Thanks a ton, this, a day problem has finally come to an end.

Thank you. Smile