MyBB Community Forums

Full Version: db functions, xmlhttp and the admin menu
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
You just do this. It works, I guarantee it

$query = $db->simple_select(TABLE_PREFIX."threads", "SUM(replies) AS sumreplies");
$sumreplies = $db->fetch_field($query, "sumreplies");
I am getting three values here. The total number of views, clicks and zone ids. If $stats[2], or the zid sum equals 0, it is treated as an inactive banner. Using fetch_row() allows a single query to get these values.

$query = $db->simple_select( TABLE_PREFIX . 'myplugin_stats', 'SUM(clicks),SUM(views),SUM(zid)', 'bid=' . $result['bid'], '' );
$stats  = fetch_row( $query );

array( [0] => #clicks, [1] => #views, [2] => #zids )

mtjo Wrote:That didn't work, however, again I created and included a myplugin_db.php file with the needed mysql functions and that works.

That should work as that's what we use in MyBB.
Example code:
$query = $db->simple_select(TABLE_PREFIX."attachments", "SUM(filesize) AS ausage", "uid='".$mybb->user['uid']."'");
		$usage = $db->fetch_array($query);
		if($usage['ausage'] > ($mybb->usergroup['attachquota']*1024) && $mybb->usergroup['attachquota'] != 0)

mtjo Wrote:I am getting three values here. The total number of views, clicks and zone ids. If $stats[2], or the zid sum equals 0, it is treated as an inactive banner. Using fetch_row() allows a single query to get these values.

$query = $db->simple_select( TABLE_PREFIX . 'myplugin_stats', 'SUM(clicks),SUM(views),SUM(zid)', 'bid=' . $result['bid'], '' );
$stats  = fetch_row( $query );

array( [0] => #clicks, [1] => #views, [2] => #zids )


You can do that with AS aswell just do:
$query = $db->simple_select( TABLE_PREFIX . 'myplugin_stats', 'SUM(clicks) AS clicks,SUM(views) AS views,SUM(zid) AS zid', 'bid=' . $result['bid'], '' );
$stats  = $db->fetch_array( $query );

array( [clicks] => #clicks, [views] => #views, [zid] => #zids )
I see this as a lot more viewable aswell as you obviously don't know what [0] in the code refers to - if you don't look at the query.
That doesn't work either. It returns the values as NULLs. I am not trying to be obtuse here, so don't take this wrong. But, are ya'll against using fetch_row() because it isn't verbose?
uhm, yeh. If you want to fetch more than one row you do this:

while($row = $db->fetch_array($query))
{
echo "<pre>";
print_r($row);
echo "</pre>";
}

We're not against using fetch_row, we're just not going to use it because there are easy functions that do the same thing but better.
You have better methods that use less than two lines of code? I'm all ears.
mtjo Wrote:You have better methods that use less than two lines of code? I'm all ears.

The number of lines does not directly correlate to the better method of code used.
Quote:That didn't work, however, again I created and included a myplugin_db.php file with the needed mysql functions and that works. I guess I have an aversion to having to modify core files and pollute core mybb dirs with custom plugin files. With over 200 plugins available, that could get very messy. I want to be able to just upload a myplugin folder with all my support files to the plugins dir w/o requiring a user to add/modify files in the admin and root dir or wherever.
You can easily do that with plugins.

Create one single file with the plugin information, hooks and any generic information you want and make sure you reference your hooks like this:
$plugins->add_hook("global_end", "my_function", MYBB_ROOT."inc/plugins/my_plugin_directory/example.php");

This will only load that specific plugin file from that directory when that hook is called. This is particularly useful if you're developing a large plugin and want to eliminate the memory overhead of having it all in memory at once - things are only loaded if they're needed.

Want to tap in to the Admin CP to add your own pages without modifying files? No problem.

$plugins->add_hook("index_start", "my_hook");

function my_hook()
{
	global $mybb;
	if($mybb->input['action'] == "my_plugin_intro")
	{
		// Code here
		exit;
	}
	else if($mybb->input['action'] == "my_plugin_start")
	{
		// Code here
		exit;
	}
}

And yes - I agree with you, plugin authors should only require files/folders to be uploaded to the plugins directory (and possibly images directory too). This eliminates unofficial files from the MyBB "core" and makes upgrading a lot easier and identifying official/unofficial files.

Quote:Lastly, I would like to see a hook to the xmlhttp.php functions w/o having to modify the file and adding a new conditional. Maybe making the xmlhttp.php a class and a dev could simply extend it.
xmlhttp.php has a plugin hook - xmlhttp.

It can be extended in a similar method to how I outlined for the Admin CP above. The same applies to any part of the MyBB front end.

xmlhttp example:
$plugins->add_hook("xmlhttp", "my_xmlhttp_hook");

function my_xmlhttp_hook()
{
	global $mybb;
	if($mybb->input['action'] == "hi")
	{
		echo "Hi";
		exit;
	}
}

Your AJAX request would then call xmlhttp.php?action=hi.
Chris, thanks for the info. That definately explains what I needed. I stand corrected on getting the SUM values via the fetch_assoc method. When getting the result array element value, you have to use the following:

//What threw me off was that "sum" is case sensitive

$query( 'select sum(clicks)..... );

$clicks = $result['sum(clicks)'];

$query( 'select SUM(clicks)..... );

$clicks = $result['SUM(clicks)'];
If you use SELECT SUM(clicks) AS my_sum you should be able to use $result['my_sum'] - much nicer.

Also - regarding your post on the Mods site about the xmlhttp.php hook - the reason you're receiving the Admin CP login page is because of your require line for admin/global.php - this shows the login screen.
Pages: 1 2 3