MyBB Community Forums

Full Version: Loops
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
This is the code I'm currently using to try and loop to show the prefixes and the usergroups that can use them.

The result is: http://www.jammingames.tk/forum/prefix.php
I'm trying to show the correct usergroup for the prefixes.
Also, the forum id's stretch out as far as needed on the page, I need a way to keep them within the page so you don't have to scroll.
And I can't quite get it, so I was wondering if someone could help me, thanks.

$prefix = $db->query("SELECT * from ".TABLE_PREFIX."prefix");
while ($query = $db->fetch_array($prefix)) {
	$title = $query['title'];
	$fid = $query['fid'];
	$gid = $query['gid'];
	$format = str_replace($title, "<font color='#4BB1FF'>$title</font>", $title);
		$groups2 = $db->query("SELECT * FROM ".TABLE_PREFIX."usergroups WHERE gid = '$gid'");
		while ($groups_result2 = $db->fetch_array($groups2)) {
			$usergroup2 = $groups_result2['namestyle'];
			$usergroup3 = str_replace("{username}", $groups_result2['title'], $usergroup2);
		}
	$prefix_output = "The available prefixes are as followed (Title - Usergroups - Forum ID): <br />
			$format - $usergroup3 - $fid";
}
Do you mean to use a foreach loop rather than a while loop to iterate over your returned array?
http://php.net/manual/en/control-structures.foreach.php
Well, I used a while loop in collecting my usergroup values and then displaying them on the index, which worked fine. I thought it would work here too.
And I'm not quite sure how I would apply that here.

-- Bump --
try this. i've changed the queries to use MyBB standard coding and updated the queries/code to use the 1.6 (ver 1600) tables/schema. also simplified some code

the forum ID list is behaving weird since there is no sapce for the browser to break on. i have added a line to replace the comma with a comma + space. this should allow the browser to break the line

$query = $db->simple_select("threadprefixes",  "*", "1=1");
while ($prefix = $db->fetch_array($query)) 
{
    $title = $prefix['prefix'];
    $fid = $prefix['forums'];
    $fid = str_replace(",", ", ", $fid);
    $gid = $prefix['groups'];
    $format = "<font color='#4BB1FF'>{$title}</font>";
        $groups = $db->simple_select("usergroups", "*", "gid = '{$gid}'");
        while ($group = $db->fetch_array($groups)) 
       {
            $usergroup2 = $group['namestyle'];
            $usergroup3 = str_replace("{username}", $group['title'], $usergroup2);
        }
        $prefix_output = "The available prefixes are as followed (Title - Usergroups - Forum ID): <br />
         $format - $usergroup3 - $fid";
}
It worked, sort of.
It doesn't loop.
Btw, I had to change it a bit so it pulled the correct info.
http://www.jammingames.tk/forum/prefix.php

$query = $db->simple_select("prefix",  "*", "1=1");
while ($prefix = $db->fetch_array($query)) 
{
    $title = $prefix['title'];
    $fid = $prefix['fid'];
    $fid = str_replace(",", ", ", $fid);
    $gid = $prefix['gid'];
    $format = "<font color='#4BB1FF'>{$title}</font>";
        $groups = $db->simple_select("usergroups", "*", "gid = '{$gid}'");
        while ($group = $db->fetch_array($groups)) 
       {
            $usergroup2 = $group['namestyle'];
            $usergroup3 = str_replace("{username}", $group['title'], $usergroup2);
        }
        $prefix_output = "The available prefixes are as followed (Title - Usergroups - Forum ID): <br />
         $format - $usergroup3 - $fid";
} 
sorry, I missed the placement of the second to last }

what version of mybb are you using? 1.4 with a thread prefix plugin? my code was for 1.6 and the built in prefix support

change
        }
        $prefix_output = "The available prefixes are as followed (Title - Usergroups - Forum ID): <br />
        $format - $usergroup3 - $fid"; 
}

to
        
        $prefix_output = "The available prefixes are as followed (Title - Usergroups - Forum ID): <br />1
        $format - $usergroup3 - $fid<br />"; 
        }
}

Using MyBB 1.6.
So the table is mybb_prefix
Still doesn't work.

$query = $db->simple_select("prefix",  "*", "1=1");
while ($prefix = $db->fetch_array($query)) {
    $title = $prefix['title'];
    $fid = $prefix['fid'];
    $fid_replace = str_replace(",", ", ", $fid);
    $gid = $prefix['gid'];
    $format = "<font color='#4BB1FF'>{$title}</font>";
        $groups = $db->simple_select("usergroups", "*", "gid = '{$gid}'");
        while ($group = $db->fetch_array($groups)) {
            $usergroup2 = $group['namestyle'];
            $usergroup3 = str_replace("{username}", $group['title'], $usergroup2);
			$prefix_output = "The available prefixes are as followed (Title - Usergroups - Forum ID): <br />
			$format - $usergroup3 - $fid_replace";
		}
} 
are you using the beta version of 1.6? the current 1.6 release download uses the threadprefixes table and has no "prefix" table and the fields I used in my code are correct for it.


change
$prefix_output =
to
$prefix_output .=

(notice the dot) plus you should add the <br /> to the end of that line
No, I'm using the released 1.6.
I still have tables in there from dead plugins though, so that's what confused me.
And it's almost working now.
Check the link now, and I re-did the variables, because I moved the code.
It shows all of the usergroups, not just the ones that can use the prefix, and there is []'s at the end of the usergroups. Starting from 1 [] to 6 of them.
Thanks for your help.

$prefix = $db->simple_select("threadprefixes",  "*", "1=1");
while ($prefix_result = $db->fetch_array($prefix)) {
	$title = $prefix_result['displaystyle'];
    $fid = $prefix_result['forums'];
    $fid_replace = str_replace("-1", "All Forums", $fid);
    $gid = $prefix_result['groups'];
    $format = "<font color='#4BB1FF'>{$title}</font>";
        $groups = $db->simple_select("usergroups", "*", "gid = '{$gid}'");
        while ($groups_result = $db->fetch_array($groups)) {
            $usergroup = $groups_result['namestyle'];
            $usergroup2 = str_replace("{username}", $groups_result['title'], $usergroup);
			$usergroups .= " [".$usergroup3."] ";
			$prefix_output .= "The available prefixes are as followed (Title - Usergroups - Forum ID): <br />
			$format - $usergroups - $fid_replace<br /><br />";
		}
} 
try this one. I moved the header content to the top so it shows once only. I also put back the comma + space bit, but your All Forums replacement will still work.

i also included a where_clause set of statemetns that deals with an 'all groups" conditions


$prefix_output = "The available prefixes are as followed (Title - Usergroups - Forum ID): <br />";

$prefix = $db->simple_select("threadprefixes",  "*", "1=1");
while ($prefix_result = $db->fetch_array($prefix)) {
    $title = $prefix_result['displaystyle'];
    $fid = $prefix_result['forums'];
    $fid = str_replace(",", ", ", $fid);
    $fid_replace = str_replace("-1", "All Forums", $fid);
    $gid = $prefix_result['groups'];
    $where_clause = "1=1";
    if($gid != -1)
    {
       $where_clause = "gid in ({$gid})";
    }
    $format = "<font color='#4BB1FF'>{$title}</font>";
        $groups = $db->simple_select("usergroups", "*", $where_clause);
        while ($groups_result = $db->fetch_array($groups)) {
            $usergroup = $groups_result['namestyle'];
            $usergroup2 = str_replace("{username}", $groups_result['title'], $usergroup);
            $usergroups .= " [".$usergroup3."] ";
            $prefix_output .= "{$format} - {$usergroups} - {$fid_replace}<br /><br />";
        }
}
Pages: 1 2