MyBB Community Forums

Full Version: Custom Plugin Help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am trying to make a showgroups.php like they have on leakforums. However when I go to my page (http://www.mysite.com/usergroups.php) it just shows the last group created that a user can join.

My Pluginpage (usergroups.php)
<?php 



define("IN_MYBB", 1);

$templatelist = "usergroups, error";
require_once "./global.php";

if ($mybb->user['uid'] == 0){ 

error_no_permission();
 }



$query = $db->simple_select("usergroups", "*", "(type='3' OR type='4')", array('order_by' => 'title'));
	while($usergroup = $db->fetch_array($query))

{
$name = $usergroup ['title'];

$description1= $usergroup['description'];

$userbar= $usergroup ['image'];

$number = $gid;

$joinlink = "<a href=\"usercp.php?action=usergroups&amp;joingroup={$usergroup ['gid']}&amp;my_post_key={$mybb->post_code}\">Join Group</a>";





 



   
	add_breadcrumb("Custom Groups");
	$title = "Custom Groups";
	eval("\$usergroups = \"".$templates->get("usergroups")."\";");
	output_page($usergroups);

   
?>


The template for usergroups.php:
<html>
<html>
<head>
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="expires" content="0" />
<title>{$mybb->settings[bbname]} - Usergroups</title>
{$headerinclude}
</head>
<body>
{$header}
<br />

<table border="0" cellspacing="{$theme[borderwidth]}" cellpadding="{$theme[tablespace]}" class="tborder">
<tr>
<td class="thead"><strong>Custom User Groups</strong></td>
</tr>
<tr>
<td class="trow1" width="100%">

<div style="width: 48%; min-height:120px; border: 1px #0B61A4 solid; margin: 4px; padding: 2px; border-radius: 5px; float:left;">
<table width="100%" cellspacing="0" cellpadding="5" border="0">
<tr class="trow1">
<td width="75%" border="0">
<span><b>{$name}</b></span><br/>
<span class="smalltext">
{$joinlink}<br/><br/>{$description}</span>
</td>
<td width="25%" align="right" valign="middle">
<img src="{$userbar}"/><br/>
</td>
</tr>
</table>
</div>

<div style="width: 48%; min-height:120px; border: 1px #0B61A4 solid; margin: 4px; padding: 2px; border-radius: 5px; float:right;">
<table width="100%" cellspacing="0" cellpadding="5" border="0">
<tr class="trow1">
<td width="75%" border="0">
<span><b>{$name}</b></span><br/>
<span class="smalltext">
{$joinlink}<br/><br/>{$description}</span>
</td>
<td width="25%" align="right" valign="middle">
<img src="{$userbar}"/><br/>
</td>
</tr>
</table>
</div>


<div style="width: 48%; min-height:120px; border: 1px #0B61A4 solid; margin: 4px; padding: 2px; border-radius: 5px; float:left;">
<table width="100%" cellspacing="0" cellpadding="5" border="0">
<tr class="trow1">
<td width="75%" border="0">
<span><b>{$name}</b></span><br/>
<span class="smalltext">
{$joinlink}<br/><br/>{$description}</span>
</td>
<td width="25%" align="right" valign="middle">
<img src="{$userbar}"/><br/>
</td>
</tr>
</table>
</div>







 
</td>
</tr>
<tr>
</table>
</form>
{$footer}
</body>
</html>

I know I am doing something wrong but can't figure out what.
(2013-08-21, 04:00 PM)thezlehman Wrote: [ -> ]I am trying to make a showgroups.php like they have on leakforums. However when I go to my page (http://www.mysite.com/usergroups.php) it just shows the last group created that a user can join.

My Pluginpage (usergroups.php)
<?php 



define("IN_MYBB", 1);

$templatelist = "usergroups, error";
require_once "./global.php";

if ($mybb->user['uid'] == 0){ 

error_no_permission();
 }



$query = $db->simple_select("usergroups", "*", "(type='3' OR type='4')", array('order_by' => 'title'));
	while($usergroup = $db->fetch_array($query))

{
$name = $usergroup ['title'];

$description1= $usergroup['description'];

$userbar= $usergroup ['image'];

$number = $gid;

$joinlink = "<a href=\"usercp.php?action=usergroups&amp;joingroup={$usergroup ['gid']}&amp;my_post_key={$mybb->post_code}\">Join Group</a>";





 



   
	add_breadcrumb("Custom Groups");
	$title = "Custom Groups";
	eval("\$usergroups = \"".$templates->get("usergroups")."\";");
	output_page($usergroups);

   
?>


The template for usergroups.php:
<html>
<html>
<head>
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="expires" content="0" />
<title>{$mybb->settings[bbname]} - Usergroups</title>
{$headerinclude}
</head>
<body>
{$header}
<br />

<table border="0" cellspacing="{$theme[borderwidth]}" cellpadding="{$theme[tablespace]}" class="tborder">
<tr>
<td class="thead"><strong>Custom User Groups</strong></td>
</tr>
<tr>
<td class="trow1" width="100%">

<div style="width: 48%; min-height:120px; border: 1px #0B61A4 solid; margin: 4px; padding: 2px; border-radius: 5px; float:left;">
<table width="100%" cellspacing="0" cellpadding="5" border="0">
<tr class="trow1">
<td width="75%" border="0">
<span><b>{$name}</b></span><br/>
<span class="smalltext">
{$joinlink}<br/><br/>{$description}</span>
</td>
<td width="25%" align="right" valign="middle">
<img src="{$userbar}"/><br/>
</td>
</tr>
</table>
</div>

<div style="width: 48%; min-height:120px; border: 1px #0B61A4 solid; margin: 4px; padding: 2px; border-radius: 5px; float:right;">
<table width="100%" cellspacing="0" cellpadding="5" border="0">
<tr class="trow1">
<td width="75%" border="0">
<span><b>{$name}</b></span><br/>
<span class="smalltext">
{$joinlink}<br/><br/>{$description}</span>
</td>
<td width="25%" align="right" valign="middle">
<img src="{$userbar}"/><br/>
</td>
</tr>
</table>
</div>


<div style="width: 48%; min-height:120px; border: 1px #0B61A4 solid; margin: 4px; padding: 2px; border-radius: 5px; float:left;">
<table width="100%" cellspacing="0" cellpadding="5" border="0">
<tr class="trow1">
<td width="75%" border="0">
<span><b>{$name}</b></span><br/>
<span class="smalltext">
{$joinlink}<br/><br/>{$description}</span>
</td>
<td width="25%" align="right" valign="middle">
<img src="{$userbar}"/><br/>
</td>
</tr>
</table>
</div>







 
</td>
</tr>
<tr>
</table>
</form>
{$footer}
</body>
</html>

I know I am doing something wrong but can't figure out what.

Hi,

The issue here is pretty simple.

basically your code overwrites the variable rather than adding to it

$joinlink = "<a href=\"usercp.php?action=usergroups&amp;joingroup={$usergroup ['gid']}&amp;my_post_key={$mybb->post_code}\">Join Group</a>";

which would be fine if you were immediately calling the template and placing it in there.

However you call the usergroups template (by what looks like inside the loop) but this is an entire page template, which is going to just leave you with the template being generated for only the last group.

What you need to do is to create a template called something like
"usergroup_rows" which would look like

<tr>
<td class="thead"><strong>Custom User Groups</strong></td>
</tr>
<tr>
<td class="trow1" width="100%">

<div style="width: 48%; min-height:120px; border: 1px #0B61A4 solid; margin: 4px; padding: 2px; border-radius: 5px; float:left;">
<table width="100%" cellspacing="0" cellpadding="5" border="0">
<tr class="trow1">
<td width="75%" border="0">
<span><b>{$name}</b></span><br/>
<span class="smalltext">
{$joinlink}<br/><br/>{$description}</span>
</td>
<td width="25%" align="right" valign="middle">
<img src="{$userbar}"/><br/>
</td>
</tr>
</table>
</div>

<div style="width: 48%; min-height:120px; border: 1px #0B61A4 solid; margin: 4px; padding: 2px; border-radius: 5px; float:right;">
<table width="100%" cellspacing="0" cellpadding="5" border="0">
<tr class="trow1">
<td width="75%" border="0">
<span><b>{$name}</b></span><br/>
<span class="smalltext">
{$joinlink}<br/><br/>{$description}</span>
</td>
<td width="25%" align="right" valign="middle">
<img src="{$userbar}"/><br/>
</td>
</tr>
</table>
</div>
[code]

Then call that inside your while loop with the following

[code]
$query = $db->simple_select("usergroups", "*", "(type='3' OR type='4')", array('order_by' => 'title'));
    while($usergroup = $db->fetch_array($query))

{
$name = $usergroup ['title'];

$description1= $usergroup['description'];

$userbar= $usergroup ['image'];

$number = $gid;

$joinlink = "<a href=\"usercp.php?action=usergroups&amp;joingroup={$usergroup ['gid']}&amp;my_post_key={$mybb->post_code}\">Join Group</a>";
   
    add_breadcrumb("Custom Groups");
    $title = "Custom Groups";
    eval("\$usergroup_rows .= \"".$templates->get("usergroup_rows")."\";");
    output_page($usergroups);
}

 


notice the .= on the template call, because we want to build the variable to contain all of the rows.

Then outside your while loop at the end call the usergroups template that should look something like

<html>
<html>
<head>
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="expires" content="0" />
<title>{$mybb->settings[bbname]} - Usergroups</title>
{$headerinclude}
</head>
<body>
{$header}
<br />

<table border="0" cellspacing="{$theme[borderwidth]}" cellpadding="{$theme[tablespace]}" class="tborder">
{$usergroup_rows}
</table>
</form>
{$footer}
</body>
</html>


By the way I would totally get rid of nested tables (tables inside tables) you should be able to do this with either divs inside the table cells or just have table cells do the work.

I have just done the basics above you may have to adjust the HTML but the basic concept would look like this


$query = blah
while() {
//build rows of HTML table in loop
}

//Put rows into a table table.

Hope this helps
Dan
(2013-08-22, 12:55 AM)Dannymh Wrote: [ -> ]
(2013-08-21, 04:00 PM)thezlehman Wrote: [ -> ]I am trying to make a showgroups.php like they have on leakforums. However when I go to my page (http://www.mysite.com/usergroups.php) it just shows the last group created that a user can join.

My Pluginpage (usergroups.php)
<?php 



define("IN_MYBB", 1);

$templatelist = "usergroups, error";
require_once "./global.php";

if ($mybb->user['uid'] == 0){ 

error_no_permission();
 }



$query = $db->simple_select("usergroups", "*", "(type='3' OR type='4')", array('order_by' => 'title'));
	while($usergroup = $db->fetch_array($query))

{
$name = $usergroup ['title'];

$description1= $usergroup['description'];

$userbar= $usergroup ['image'];

$number = $gid;

$joinlink = "<a href=\"usercp.php?action=usergroups&amp;joingroup={$usergroup ['gid']}&amp;my_post_key={$mybb->post_code}\">Join Group</a>";





 



   
	add_breadcrumb("Custom Groups");
	$title = "Custom Groups";
	eval("\$usergroups = \"".$templates->get("usergroups")."\";");
	output_page($usergroups);

   
?>


The template for usergroups.php:
<html>
<html>
<head>
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="expires" content="0" />
<title>{$mybb->settings[bbname]} - Usergroups</title>
{$headerinclude}
</head>
<body>
{$header}
<br />

<table border="0" cellspacing="{$theme[borderwidth]}" cellpadding="{$theme[tablespace]}" class="tborder">
<tr>
<td class="thead"><strong>Custom User Groups</strong></td>
</tr>
<tr>
<td class="trow1" width="100%">

<div style="width: 48%; min-height:120px; border: 1px #0B61A4 solid; margin: 4px; padding: 2px; border-radius: 5px; float:left;">
<table width="100%" cellspacing="0" cellpadding="5" border="0">
<tr class="trow1">
<td width="75%" border="0">
<span><b>{$name}</b></span><br/>
<span class="smalltext">
{$joinlink}<br/><br/>{$description}</span>
</td>
<td width="25%" align="right" valign="middle">
<img src="{$userbar}"/><br/>
</td>
</tr>
</table>
</div>

<div style="width: 48%; min-height:120px; border: 1px #0B61A4 solid; margin: 4px; padding: 2px; border-radius: 5px; float:right;">
<table width="100%" cellspacing="0" cellpadding="5" border="0">
<tr class="trow1">
<td width="75%" border="0">
<span><b>{$name}</b></span><br/>
<span class="smalltext">
{$joinlink}<br/><br/>{$description}</span>
</td>
<td width="25%" align="right" valign="middle">
<img src="{$userbar}"/><br/>
</td>
</tr>
</table>
</div>


<div style="width: 48%; min-height:120px; border: 1px #0B61A4 solid; margin: 4px; padding: 2px; border-radius: 5px; float:left;">
<table width="100%" cellspacing="0" cellpadding="5" border="0">
<tr class="trow1">
<td width="75%" border="0">
<span><b>{$name}</b></span><br/>
<span class="smalltext">
{$joinlink}<br/><br/>{$description}</span>
</td>
<td width="25%" align="right" valign="middle">
<img src="{$userbar}"/><br/>
</td>
</tr>
</table>
</div>







 
</td>
</tr>
<tr>
</table>
</form>
{$footer}
</body>
</html>

I know I am doing something wrong but can't figure out what.

Hi,

The issue here is pretty simple.

basically your code overwrites the variable rather than adding to it

$joinlink = "<a href=\"usercp.php?action=usergroups&amp;joingroup={$usergroup ['gid']}&amp;my_post_key={$mybb->post_code}\">Join Group</a>";

which would be fine if you were immediately calling the template and placing it in there.

However you call the usergroups template (by what looks like inside the loop) but this is an entire page template, which is going to just leave you with the template being generated for only the last group.

What you need to do is to create a template called something like
"usergroup_rows" which would look like

<tr>
<td class="thead"><strong>Custom User Groups</strong></td>
</tr>
<tr>
<td class="trow1" width="100%">

<div style="width: 48%; min-height:120px; border: 1px #0B61A4 solid; margin: 4px; padding: 2px; border-radius: 5px; float:left;">
<table width="100%" cellspacing="0" cellpadding="5" border="0">
<tr class="trow1">
<td width="75%" border="0">
<span><b>{$name}</b></span><br/>
<span class="smalltext">
{$joinlink}<br/><br/>{$description}</span>
</td>
<td width="25%" align="right" valign="middle">
<img src="{$userbar}"/><br/>
</td>
</tr>
</table>
</div>

<div style="width: 48%; min-height:120px; border: 1px #0B61A4 solid; margin: 4px; padding: 2px; border-radius: 5px; float:right;">
<table width="100%" cellspacing="0" cellpadding="5" border="0">
<tr class="trow1">
<td width="75%" border="0">
<span><b>{$name}</b></span><br/>
<span class="smalltext">
{$joinlink}<br/><br/>{$description}</span>
</td>
<td width="25%" align="right" valign="middle">
<img src="{$userbar}"/><br/>
</td>
</tr>
</table>
</div>
[code]

Then call that inside your while loop with the following

[code]
$query = $db->simple_select("usergroups", "*", "(type='3' OR type='4')", array('order_by' => 'title'));
    while($usergroup = $db->fetch_array($query))

{
$name = $usergroup ['title'];

$description1= $usergroup['description'];

$userbar= $usergroup ['image'];

$number = $gid;

$joinlink = "<a href=\"usercp.php?action=usergroups&amp;joingroup={$usergroup ['gid']}&amp;my_post_key={$mybb->post_code}\">Join Group</a>";
   
    add_breadcrumb("Custom Groups");
    $title = "Custom Groups";
    eval("\$usergroup_rows .= \"".$templates->get("usergroup_rows")."\";");
    output_page($usergroups);
}

 


notice the .= on the template call, because we want to build the variable to contain all of the rows.

Then outside your while loop at the end call the usergroups template that should look something like

<html>
<html>
<head>
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="expires" content="0" />
<title>{$mybb->settings[bbname]} - Usergroups</title>
{$headerinclude}
</head>
<body>
{$header}
<br />

<table border="0" cellspacing="{$theme[borderwidth]}" cellpadding="{$theme[tablespace]}" class="tborder">
{$usergroup_rows}
</table>
</form>
{$footer}
</body>
</html>


By the way I would totally get rid of nested tables (tables inside tables) you should be able to do this with either divs inside the table cells or just have table cells do the work.

I have just done the basics above you may have to adjust the HTML but the basic concept would look like this


$query = blah
while() {
//build rows of HTML table in loop
}

//Put rows into a table table.

Hope this helps
Dan

Thanks it does help. However now I am experiencing another problem. As you can see here: http://www.moneymethodsmania.com/usergroups.php the usergroup fills both sets of boxes on the left and the right. I know I am most likely over looking something again but I can't see to find a solution.
hmm need to see the whole code. Its just a matter of getting all your HTML and loops right.

The styling you are using can be achieved with a single table rather than nested tables. Can you please post your whole code, attachments would be best then I can view them straight in edit plus
All the files are in the attached zip folder. Templates are in .txt format. Thanks for the help.
ok will take a look shortly. Sorry for the delay work became a bit busy

You issue is that you call the same thing twice in your row template.

Your row template currently looks like

<tr>

<td class="trow1" width="100%">



<div style="width: 48%; min-height:120px; border: 1px #0B61A4 solid; 
margin: 4px; padding: 2px; border-radius: 5px; float:left;">

<table width="100%" cellspacing="0" cellpadding="5" border="0">

<tr class="trow1">

<td width="75%" border="0">

<span><b>{$name}</b></span><br/>

<span class="smalltext">

{$joinlink}<br/><br/>{$description}</span>

</td>

<td width="25%" align="right" valign="middle">

<img src="{$userbar}"/><br/>

</td>

</tr>

</table>

</div>



<div style="width: 48%; min-height:120px; border: 1px #0B61A4 solid; 
margin: 4px; padding: 2px; border-radius: 5px; float:right;">

<table width="100%" cellspacing="0" cellpadding="5" border="0">

<tr class="trow1">

<td width="75%" border="0">

<span><b>{$name}</b></span><br/>

<span class="smalltext">

{$joinlink}<br/><br/>{$description}</span>

</td>

<td width="25%" align="right" valign="middle">

<img src="{$userbar}"/><br/>

</td>

</tr>

</table>

</div>

Whereas it should look like

<tr>
	<td class="trow1" width="100%">
		<div style="width: 48%; min-height:120px; border: 1px #0B61A4 solid; margin: 4px; padding: 2px; border-radius: 5px; float:left;">
			<table width="100%" cellspacing="0" cellpadding="5" border="0">
				<tr class="trow1">
					<td width="75%" border="0">
						<span><b>{$name}</b></span><br/>
						<span class="smalltext">
							{$joinlink}
							<br/><br/>
							{$description}
						</span>
					</td>
					<td width="25%" align="right" valign="middle">
						<img src="{$userbar}"/><br/>
					</td>
				</tr>
			</table>
		</div>
	</td>
</tr>

That will put everything in single rows per group you have. If you wanted to do two per row, you would need to change the loop PHP side and the HTML to match, I assume the above will work as needed. If you wanted the exact layout you had, i.e. a table with two columns, let me know and I will put something basic together for you.

By the way I have used tab formatting on that so it makes it easier to read. It just makes figuring out where elements lay a little easier to read at a glance.

Call that in your loop and it should be closer to what you want.

Dan
The person I am doing this plugin for actually wanted the two rows which is the only reason I had it. If it is too much trouble I can convince him that this is the best way to do it.

Edit: I got it to alternate sides however each block is on its own line. The attached files contain the edits I made.

Edit2: Fixed it myself.
Oh it's not too much trouble to do, it's just a different methodology.

There are several ways to do it. You could do a count in your loop, and do something like

template cell 1
<tr>
<td>
info
</td>

template cell 2
<td>
info
</td>
</tr>

Then in your loop

counter = 1;
while(){
if($counter==1){
call template 1;
++$count;
} else {
call template 2;
$count =1;
}

That is not a very elegant way to do it. But basically what it does is as follows

Template 1 is your first cell of the row so it opens you <tr>
template 2 is the last cell so it closes the </tr>

Wrap that in you table template outside the loop and it will do what you need.

Now one word of warning, if you finish on a 1, i.e. have odd groups, you will need to make sure that you add the last </tr>

To do this before you call your table. You will need to do the following

if($counter==2){
//add a <tr> to the temlate variable
}

The reason it is counter=2 is that after calling the first template the counter will update to , if it is at 1 then you are good to go.

Again it really is not an elegant way to do things, but it will work and should give you the idea.

You need to think of calling templates inside a loop as basically filling in chunks of HTML.

If you map your application out as if you were filling in a variable to build your table, you need to remember that you would be adding to the variable ".=" instead of "=" then pull the HTML back out to a template you will get a better idea.

When you call a template you are not printing that out to the page, you are storing it in a variable and then outputting the variable at the very end though the $page->output
I did edit the previous post to say I managed to get it by myself. What I did was in the templates the box was either "floating" right or left. So I created a variable called $side and one just called $number during my while statement I put an if/else statement which would either make $side equal left or side equal right. I placed $side in place of the floating parameter (Im guessing thats what its called) and it worked.


You have been a huge help in this. I can't thank you enough.
Thats a much more elegant way of doing it. Just check IE7 with it as well but glad you have it working now.

Dan