MyBB Community Forums

Full Version: PHP Help - MyBB Plugin
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I'm working on a simple plugin for a paid project I'm doing which automatically adds forum subscriptions on registration after a user has chosen some custom field options. These custom fields are dropdowns which list the forums on the site. Therefore the fields need to be matched to the actual forum id.

Here is the code;
<?php
if(!defined("IN_MYBB")) {
    die("This file cannot be accessed directly.");
}

$plugins->add_hook("member_do_register_end", "forumsubs_addsubs");

function forumsubs_info() {
    return array(
        "name"				=> "Forum Subscriptions on Registration",
        "description"		=> "Allows you to automatically add forum subscriptions on registration.",
        "website"			=> "http://yhm.co.uk",
        "author"			=> "Polarbear541",
        "authorsite"		=> "http://yhm.co.uk",
        "version"			=> "1.0",
        "compatibility" => "*"
    );
}

function forumsubs_activate()
{
}

function forumsubs_deactivate()
{
}

function forumsubs_addsubs()
{
	global $db, $mybb;
	
	$uid = $mybb->user['uid'];
	
	if ($uid == 0)
	{
		$query = $db->simple_select('users', 'uid', 'LOWER(username)=\''.$db->escape_string(my_strtolower($mybb->input['username'])).'\'', array('limit' => 1));
		$uid = $db->fetch_field($query, 'uid');
	}
	
	$getfields = $db->query("SELECT * FROM ".TABLE_PREFIX."userfields WHERE ufid = '$uid'");
	$fields = $db->fetch_array($getfields);
	
	$field1 = $fields['fid4'];
	$field2 = $fields['fid7'];
	$field3 = $fields['fid8'];
	$field4 = $fields['fid9'];
	
	$getfid1 = $db->query("SELECT * FROM ".TABLE_PREFIX."forums WHERE name = '$field1'");
	$fid1 = $db->query($getfid1);
	$fid1 = $fid1['fid'];

	$getfid2 = $db->query("SELECT * FROM ".TABLE_PREFIX."forums WHERE name = '$field2'");
	$fid2 = $db->query($getfid2);
	$fid2 = $fid2['fid'];
	
	$getfid3 = $db->query("SELECT * FROM ".TABLE_PREFIX."forums WHERE name = '$field3'");
	$fid3 = $db->query($getfid3);
	$fid3 = $fid3['fid'];
	
	$getfid4 = $db->query("SELECT * FROM ".TABLE_PREFIX."forums WHERE name = '$field4'");
	$fid4 = $db->query($getfid4);
	$fid4 = $fid4['fid'];
	
	$insertsub1 = $db->query("INSERT INTO ".TABLE_PREFIX."forumsubscriptions (`fid`, `uid`) VALUES ('$fid1', '$uid')");
	$insertsub2 = $db->query("INSERT INTO ".TABLE_PREFIX."forumsubscriptions (`fid`, `uid`) VALUES ('$fid2', '$uid')");
	$insertsub3 = $db->query("INSERT INTO ".TABLE_PREFIX."forumsubscriptions (`fid`, `uid`) VALUES ('$fid3', '$uid')");
	$insertsub4 = $db->query("INSERT INTO ".TABLE_PREFIX."forumsubscriptions (`fid`, `uid`) VALUES ('$fid4', '$uid')");
}
?>

Here's a breakdown;
  1. Get the uid for use when getting data and inserting the subscriptions.
  2. Get the field content from the DB for the user.
  3. Get the forum id's by using the field as the forum name.
  4. Insert the subscriptions.

Everything works fine and the subscriptions get inserted however, the forum id's are not successfully found. There is no SQL error, just an empty array. I've even grabbed the forum name directly via it's specific id and compared the field, this was successful. For some reason however the SQL query getting the forum id is not successful.

I know this isn't the best way to find the forum however I don't really know how else I could do this other than putting the forum id's in the dropdown fields. This would cause confusion to the userbase we are aiming the site at.

Any help is appreciated Smile

you have not fetched the record from the result set, you are simply trying to pull the ['fid'] from the query result object.

function forumsubs_addsubs()
{
    global $db, $mybb;
    
    $uid = $mybb->user['uid'];
    
    if ($uid == 0)
    {
        $query = $db->simple_select('users', 'uid', 'LOWER(username)=\''.$db->escape_string(my_strtolower($mybb->input['username'])).'\'', array('limit' => 1));
        $uid = $db->fetch_field($query, 'uid');
    }
    
    $getfields = $db->query("SELECT * FROM ".TABLE_PREFIX."userfields WHERE ufid = '$uid'");
    $fields = $db->fetch_array($getfields);
    
    $field1 = $fields['fid4'];
    $field2 = $fields['fid7'];
    $field3 = $fields['fid8'];
    $field4 = $fields['fid9'];
    
    $getfid1 = $db->query("SELECT * FROM ".TABLE_PREFIX."forums WHERE name = '$field1'");
    $fid1 = $db->fetch_field($getfid1, 'fid');

    $getfid2 = $db->query("SELECT * FROM ".TABLE_PREFIX."forums WHERE name = '$field2'");
   $fid2 = $db->fetch_field($getfid2, 'fid');

    
    $getfid3 = $db->query("SELECT * FROM ".TABLE_PREFIX."forums WHERE name = '$field3'");
   $fid3 = $db->fetch_field($getfid3, 'fid');

    
    $getfid4 = $db->query("SELECT * FROM ".TABLE_PREFIX."forums WHERE name = '$field4'");
    $fid4 = $db->fetch_field($getfid4, 'fid');

    
    $insertsub1 = $db->query("INSERT INTO ".TABLE_PREFIX."forumsubscriptions (`fid`, `uid`) VALUES ('$fid1', '$uid')");
    $insertsub2 = $db->query("INSERT INTO ".TABLE_PREFIX."forumsubscriptions (`fid`, `uid`) VALUES ('$fid2', '$uid')");
    $insertsub3 = $db->query("INSERT INTO ".TABLE_PREFIX."forumsubscriptions (`fid`, `uid`) VALUES ('$fid3', '$uid')");
    $insertsub4 = $db->query("INSERT INTO ".TABLE_PREFIX."forumsubscriptions (`fid`, `uid`) VALUES ('$fid4', '$uid')");
}
Ok thanks, I'll just try that. Give me a few....
Works fine now thanks a lot Smile