MyBB Community Forums

Full Version: Query Question
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
*sigh* I feel bad that I have to keep coming and bothering you all for help. Anyways, I cannot get this query to run no matter what I do. Although I guess that isn't true. It's running, it's just not returning anything. (this is run directly from the index.php):
$i = 0;
	// First, load the stats cache.
	$stats = $cache->read("stats");
	
	$query = "SELECT usergroup FROM mybb_users WHERE uid=";

	$qResult = $db->query($query.$stats['lastuid']);
	
	if($qResult){
		while($qResult['usergroup']==7){
			$i++;
			$qResult = $db->query($query.($stats['lastuid']-$i));
		}
		
		$username = $db->query("SELECT * FROM mybb_users WHERE uid=".($stats['lastuid']-$i));
		$newestmember = build_profile_link($username['username'], ($stats['lastuid']-$i));
	}

And if I replace the if($qResult) with if($qResult['usergroup']), the if statement evaluates false and skips over it... I've tried everything I can think of, I just can't get it to run.

By the way, what it's SUPPOSED to do, is get the newest non-banned user and set the newest user on the index page.

Also, while I've got your attention, I'd like to point you to this thread.
First, you shouldn't use the mybb_ prefix there. Do ".TABLE_PREFIX."users
Have you checked if lastuid is a valid number?
Don't you have to $row = $db->fetch_array($query) before you can access $qREsult['usergroup']? Likewise with $username.

If you want the lastuid that's not in usergroup 7, you should query the users table directly, instead of looping backwards through the stats with one query per id... especially if there are ids missing that's getting you nowhere.
Well, I got it working. And you were right, I forgot to put the fetch_array function in there. But now how do I turn it into a plugin? I creating a plugin for it, declaring "global $cache, $db, $lang;", and "hooked" that function to index_end, but I couldn't get it to work.

Here's the function, by the way:

function dsbu_run(){
global $cache, $db, $lang, $templates;	

		$i = 0;
	// First, load the stats cache.
	$stats = $cache->read("stats");
	
	$query = "SELECT usergroup FROM mybb_users WHERE uid=";

	$qResult = $db->query($query.$stats['lastuid']);
	$row = $db->fetch_array($qResult);
	
	if($row){
		while($row['usergroup']==7){
			$i++;
			$row = $db->fetch_array($db->query($query.($stats['lastuid']-$i)));
			while(!$row['usergroup']){
				$i++;
				$row = $db->fetch_array($db->query($query.($stats['lastuid']-$i)));
			}
		}
		
		$query = $db->query("SELECT * FROM mybb_users WHERE uid=".($stats['lastuid']-$i));
		$username = $db->fetch_array($query);
		
		$newestmember = build_profile_link($username['username'], ($stats['lastuid']-$i));
	}
	
	$lang->stats_newestuser = $lang->sprintf($lang->stats_newestuser, $newestmember);
	
	eval("\$forumstats = \"".$templates->get("index_stats")."\";");
}
What do you mean by "How do I turn it into a plugin?". Don't you have the activate and deactivate functions already?
By the way, use table prefix instead of mybb_
Look at the default plugins that come with mybb. I think one of them is called hello.php.
No, no. What I mean, is that function doesn't work when it's made into a plugin for some reason. Here's the plugin in full:

<?php

if(!defined("IN_MYBB")){
	die("This file cannot be opened directly.  Please access it through your admin control panel.");
}

$plugins->add_hook("index_end","dsbu_run");


function dsbu_info()
{
	return array(
		"name"			=> "DSBU in newest member bit",
		"description"	=> "Stops the newest user bit from showing banned users.",
		"website"		=> "http://www.thedrp.org/",
		"author"		=> "Nicholas Roge",
		"authorsite"	=> "http://www.thedrp.org/",
		"version"		=> "1.0",
		"guid" 			=> "",
		"compatibility" => "*"
	);
}



function dsbu_run(){
	global $cache, $db, $lang, $templates;	

	$i = 0;
	
	$stats = $cache->read("stats");
	
	$query = "SELECT usergroup FROM ".TABLE_PREFIX."users WHERE uid=";

	$qResult = $db->query($query.$stats['lastuid']);
	$row = $db->fetch_array($qResult);
	
	if($row){
		while($row['usergroup']==7){
			$i++;
			$row = $db->fetch_array($db->query($query.($stats['lastuid']-$i)));
			while(!$row['usergroup']){
				$i++;
				$row = $db->fetch_array($db->query($query.($stats['lastuid']-$i)));
			}
		}
		
		$query = $db->query("SELECT * FROM ".TABLE_PREFIX."users WHERE uid=".($stats['lastuid']-$i));
		$username = $db->fetch_array($query);
		
		$newestmember = build_profile_link($username['username'], ($stats['lastuid']-$i));
	}
	
	$lang->stats_newestuser = $lang->sprintf($lang->stats_newestuser, $newestmember);
	
	eval("\$forumstats = \"".$templates->get("index_stats")."\";");
}
I don't see the activate and deactivate hooks being used. For the plugin to work you need to use the activate and deactivate hooks to allow people to turn the plugin on and off.
@skywalker, do you mean functions? Not hooks.
@Nicholas_Roga, before dsbu_run() is executed, you must activate the plugin
(2009-07-17, 07:31 PM)Pirata Nervo Wrote: [ -> ]@skywalker, do you mean functions? Not hooks.
It my eyes they are hooks as well because you are hooking into mybb to activate the plug in, but I guess that could be debated. Also, in the end they are all functions anyways.
Pages: 1 2