MyBB Community Forums

Full Version: [SOLVED]How to show result of query as a list ?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hy.
I need a little help. I'm begginer but I try to make a simple plugin.
To be more explicit, I will give an example about what I try to do:

I have 2 tables in database: "one" and "two".
Structure of table "one" is: name, surname.
Structure of table "two" is: name, location.
Field "name" have the same value in both tables.

I have also a custom template, called "my_template":
<div>{$results_list}</div>

So... now I want to build a query like:
$query = $db->query("
	SELECT t.location AS my_location
	FROM ".TABLE_PREFIX."two t
	LEFT JOIN ".TABLE_PREFIX."one o ON (o.name=t.name)
");
$results = $db->fetch_array($query);
$results_list = $results['my_location'];

Now, in "my_template", with {$results_list} I want to show all value of "location" fields (from table two), so I add next line:
eval("\$my_template = \"".$templates->get("my_template")."\";");

In this example, {$results_list} will show only the FIRST value of "location" field from table two.

Question:
Please, if you want to teach me, how to display all value from "location" as a list, separated by a comma ?

Huh

I think... I must to use "while" condition ? Or "foreach" ? And how to do this, please ?
Thank you.
foreach ($results as $result) {
$results_list .= $result['my_location'];
}

That should work I believe Smile
(2011-06-24, 09:42 AM)euantor Wrote: [ -> ]
foreach ($results as $result) {
$results_list .= $result['my_location'];
}

That should work I believe Smile

In this way it's showed only FIRST LETTER of value of first row from field "location".
I want to show all values of rows from "location" column.

Of course, I always make that mistake xD. Try this then:

while($result = $db->fetch_array($query))
{
   $results_list .= $result['my_location'];
}
First of all, thank you for your very quikly reply, but the result it's not displayed, and I don't understand why.

So... better... I will display the entire code of the plugin that I try to create for my forum:
<?php
/**
 * Sections Moderated Plugin for MyBB 1.6
 *
 * Website: http://community.mybb.com
 * Author: http://community.mybb.com/user-39767.html
 *
 * File: MYBB_ROOT/inc/plugins/sections_moderated.php
 */

// SECURITY
if(!defined("IN_MYBB"))
{
	die("Direct initialization of this file is not allowed.<br /><br />Please make sure IN_MYBB is defined.");
}

// HOOKS
$plugins->add_hook("member_profile_end", "sections_moderated_in_profile");

// INFO
function sections_moderated_info()
{
	global $lang;
	$lang->load("forum_sections_moderated", false, true);
	return array(
		"name" => $lang->sections_moderated,
		"description" => $lang->sections_moderated_desc,
		"website" => "http://community.mybb.com",
		"author" => "Flavius Popa",
		"authorsite" => "http://community.mybb.com/user-39767.html",
		"version" => "0.1",
		"compatibility" => "16*",
		"guid" => ""
	);
}

// ACTIVATE
function sections_moderated_activate()
{
	global $db, $mybb, $lang;
	// load language
	$lang->load("forum_sections_moderated", false, true);
	// gid of settings group
	$query = $db->simple_select("settinggroups", "MAX(gid) as gid");
	$gid = $db->fetch_field($query, "gid");
	// disporder of settings group
	$query = $db->simple_select("settinggroups", "MAX(disporder) as disporder");
	$disporder = $db->fetch_field($query, "disporder");
	// insert settings group
	$insert = array(
		"gid" => $gid+1,
		"name" => "sections_moderated",
		"title" => $lang->sections_moderated_settings,
		"description" => $lang->sections_moderated_settings_desc,
		"disporder" => $disporder+1,
		"isdefault" => 0
	);
	$db->insert_query("settinggroups", $insert);
	// sid of settings	
	$query = $db->simple_select("settings", "MAX(sid) as sid");
	$sid = $db->fetch_field($query, "sid");
	// insert settings
	$settings = array(
		"onoff" => array(
			$lang->sections_moderated_onoff,
			$lang->sections_moderated_onoff_desc,
			"onoff",
			"1"
		)
	);
	$disporder = 1;
	foreach($settings as $settingsname => $settingsinfo)
	{
		$insert = array(
			"sid" => $sid+1,
			"name" => "sections_moderated"."_".$settingsname,
			"title" => $db->escape_string($settingsinfo[0]),
			"description" => $db->escape_string($settingsinfo[1]),
			"optionscode" => $db->escape_string($settingsinfo[2]),
			"value" => $db->escape_string($settingsinfo[3]),
			"disporder" => $disporder,
			"gid" => $gid+1,
			"isdefault" => 0
		);
		$db->insert_query("settings", $insert);
		$sid++; $disporder++;
	}
	// tid of templates
	$query = $db->simple_select("templates", "MAX(tid) as tid");
	$tid = $db->fetch_field($query, "tid");
	// insert templates
	$templates = array(
		"in_profile" => array(
'<!-- START: Sections Moderated in Profile -->
	<tr>
		<td class="trow1" align="center">
			<strong>{$lang->sections_moderated}</strong>
			<br />
			{$sections_moderated}
		</td>
	</tr>
<!-- END: Sections Moderated in Profile -->'
		)
	);
	foreach($templates as $templatestitle => $templatesinfo)
	{
		$insert = array(
			"tid" => $tid+1,
			"title" => "sections_moderated"."_".$templatestitle,
			"template" => $db->escape_string($templatesinfo[0]),
			"sid" => "-1",
			"version" => "1600",
			"status" => "",
			"dateline" => TIME_NOW
		);
		$db->insert_query("templates", $insert);
		$tid++;
	}
	// replace templates
	require MYBB_ROOT."/inc/adminfunctions_templates.php";
	find_replace_templatesets("member_profile", "#".preg_quote('{$awaybit}')."#i", '{\$awaybit}{\$sections_moderated_in_profile}');
	// rebuild settings
	rebuild_settings();
}

// DEACTIVATE
function sections_moderated_deactivate()
{
	global $db, $mybb;
	// restore templates
	include MYBB_ROOT."/inc/adminfunctions_templates.php";
	find_replace_templatesets("member_profile", "#".preg_quote('{$sections_moderated_in_profile}')."#i", '', 0);
	// delete templates
	$db->write_query("DELETE FROM ".TABLE_PREFIX."templates WHERE title IN(
		'sections_moderated_in_profile'
	)");
	// gid of settings group
	$query = $db->simple_select("settinggroups", "gid" , "name='sections_moderated'");
	$gid = (int)$db->fetch_field($query, "gid");
	// delete settings
	$db->delete_query("settings", "gid={$gid}");
	// delete settings group
	$db->delete_query("settinggroups", "gid={$gid}");
	// rebuild settings
	rebuild_settings();
}

// RUN
function sections_moderated_in_profile()
{
	global $db, $mybb, $lang, $memprofile, $templates, $sections_moderated_in_profile;
	// load language
	$lang->load("forum_sections_moderated", false, true);
	// if plugin is enabled
	if($mybb->settings['sections_moderated_onoff'] == "1")
	{
		
		$query = $db->query("
		SELECT f.name AS results_sections
		FROM ".TABLE_PREFIX."forums f
		LEFT JOIN ".TABLE_PREFIX."moderators m ON (m.fid=f.fid)
		WHERE m.id='".$memprofile['uid']."'
		");
		while($results_sections = $db->fetch_array($query))
		{
			$sections_moderated .= $results_sections['my_location'];
			eval("\$sections_moderated_in_profile = \"".$templates->get("sections_moderated_in_profile")."\";");
		} 
	}
}
?>


And this is the language file:
<?php
/**
 * Sections Moderated Plugin for MyBB 1.6
 *
 * Website: http://community.mybb.com
 * Author: http://community.mybb.com/user-39767.html
 *
 * File: MYBB_ROOT/inc/languages/english/admin/forum_sections_moderated.php
 */

// INFO
$l['sections_moderated'] = "Sections Moderated";
$l['sections_moderated_desc'] = "A plugin that show in moderator profile, the sections of the forum moderated by him.";

// SETTINGS GROUP
$l['sections_moderated_settings'] = "Sections Moderated Settings";
$l['sections_moderated_settings_desc'] = "This section allows you to manage settings used by Sections Moderated plugin.";

// SETTINGS
$l['sections_moderated_onoff'] = "Turn On/Off";
$l['sections_moderated_onoff_desc'] = "This section allows you to turn On/Off Sections Moderated plugin.";
?>
Please, help me to understand how to display in profile all forums names as a list, where a current viewed profile user is moderator.

You need to add $sections_moderated to the gloablled list Smile

<?php
/**
 * Sections Moderated Plugin for MyBB 1.6
 *
 * Website: http://community.mybb.com
 * Author: http://community.mybb.com/user-39767.html
 *
 * File: MYBB_ROOT/inc/plugins/sections_moderated.php
 */

// SECURITY
if(!defined("IN_MYBB"))
{
    die("Direct initialization of this file is not allowed.<br /><br />Please make sure IN_MYBB is defined.");
}

// HOOKS
$plugins->add_hook("member_profile_end", "sections_moderated_in_profile");

// INFO
function sections_moderated_info()
{
    global $lang;
    $lang->load("forum_sections_moderated", false, true);
    return array(
        "name"          => $lang->sections_moderated,
        "description"   => $lang->sections_moderated_desc,
        "website"       => "http://community.mybb.com",
        "author"        => "Flavius Popa",
        "authorsite"    => "http://community.mybb.com/user-39767.html",
        "version"       => "0.1",
        "compatibility" => "16*",
        "guid"          => ""
    );
}

// ACTIVATE
function sections_moderated_activate()
{
    global $db, $mybb, $lang;
    // load language
    $lang->load("forum_sections_moderated", false, true);
    // gid of settings group
    $query = $db->simple_select("settinggroups", "MAX(gid) as gid");
    $gid = $db->fetch_field($query, "gid");
    // disporder of settings group
    $query = $db->simple_select("settinggroups", "MAX(disporder) as disporder");
    $disporder = $db->fetch_field($query, "disporder");
    // insert settings group
    $insert = array(
        "gid"            =>    $gid+1,
        "name"            =>    "sections_moderated",
        "title"            =>    $lang->sections_moderated_settings,
        "description"    =>    $lang->sections_moderated_settings_desc,
        "disporder"        =>    $disporder+1,
        "isdefault"        =>    0
    );
    $db->insert_query("settinggroups", $insert);
    // sid of settings    
    $query = $db->simple_select("settings", "MAX(sid) as sid");
    $sid = $db->fetch_field($query, "sid");
    // insert settings
    $settings = array(
        "onoff" => array(
            $lang->sections_moderated_onoff,
            $lang->sections_moderated_onoff_desc,
            "onoff",
            "1"
        )
    );
    $disporder = 1;
    foreach($settings as $settingsname => $settingsinfo)
    {
        $insert = array(
            "sid"         => $sid+1,
            "name"        => "sections_moderated"."_".$settingsname,
            "title"       => $db->escape_string($settingsinfo[0]),
            "description" => $db->escape_string($settingsinfo[1]),
            "optionscode" => $db->escape_string($settingsinfo[2]),
            "value"       => $db->escape_string($settingsinfo[3]),
            "disporder"   => $disporder,
            "gid"         => $gid+1,
            "isdefault"   => 0
        );
        $db->insert_query("settings", $insert);
        $sid++; $disporder++;
    }
    // tid of templates
    $query = $db->simple_select("templates", "MAX(tid) as tid");
    $tid = $db->fetch_field($query, "tid");
    // insert templates
    $templates = array(
        "in_profile" => array(
'<!-- START: Sections Moderated in Profile -->
    <tr>
        <td class="trow1" align="center">
            <strong>{$lang->sections_moderated}</strong>
            <br />
            {$sections_moderated}
        </td>
    </tr>
<!-- END: Sections Moderated in Profile -->'
        )
    );
    foreach($templates as $templatestitle => $templatesinfo)
    {
        $insert = array(
            "tid"       => $tid+1,
            "title"     => "sections_moderated"."_".$templatestitle,
            "template"  => $db->escape_string($templatesinfo[0]),
            "sid"       => "-1",
            "version"   => "1600",
            "status"    => "",
            "dateline"  => TIME_NOW
        );
        $db->insert_query("templates", $insert);
        $tid++;
    }
    // replace templates
    require MYBB_ROOT."/inc/adminfunctions_templates.php";
    find_replace_templatesets("member_profile", "#".preg_quote('{$awaybit}')."#i", '{\$awaybit}{\$sections_moderated_in_profile}');
    // rebuild settings
    rebuild_settings();
}

// DEACTIVATE
function sections_moderated_deactivate()
{
    global $db, $mybb;
    // restore templates
    include MYBB_ROOT."/inc/adminfunctions_templates.php";
    find_replace_templatesets("member_profile", "#".preg_quote('{$sections_moderated_in_profile}')."#i", '', 0);
    // delete templates
    $db->write_query("DELETE FROM ".TABLE_PREFIX."templates WHERE title IN(
        'sections_moderated_in_profile'
    )");
    // gid of settings group
    $query = $db->simple_select("settinggroups", "gid" , "name='sections_moderated'");
    $gid = (int)$db->fetch_field($query, "gid");
    // delete settings
    $db->delete_query("settings", "gid={$gid}");
    // delete settings group
    $db->delete_query("settinggroups", "gid={$gid}");
    // rebuild settings
    rebuild_settings();
}

// RUN
function sections_moderated_in_profile()
{
    global $db, $mybb, $lang, $memprofile, $templates, $sections_moderated_in_profile, $sections_moderated;
    // load language
    $lang->load("sections_moderated", false, true);
    // if plugin is enabled
    if($mybb->settings['sections_moderated_onoff'] == "1")
    {
        
        $query = $db->query("
        SELECT f.name AS results_sections
        FROM ".TABLE_PREFIX."forums f
        LEFT JOIN ".TABLE_PREFIX."moderators m ON (m.fid=f.fid)
        WHERE m.id='".$memprofile['uid']."'
        ");
        while($results_sections = $db->fetch_array($query))
        {
            $sections_moderated .= $results_sections['my_location'];
            eval("\$sections_moderated_in_profile = \"".$templates->get("sections_moderated_in_profile")."\";");
        } 
    }
}
?>
Still not working Undecided
[attachment=23152]

Please, can you (or someone else with experience) make this plugin to work ?
I just want to learn how to do this. That's all.
I really need a plugin like this, so this is the archive:
Later: or just teach me how to show result of query as a list ?
Bump, please!?
Uhhh... I found the mistake.
I was really blind Smile
            $sections_moderated .= $results_sections['my_location'];
'my location' must to be 'results_sections'
Blush
Thank you again euantor for all your support !
This thread can be closed.