MyBB Community Forums

Full Version: PHP / page manager
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Dear all, I am a PHP beginner and have no clue how to solve this problem:

I want to create -with page manager- a page that has a simle input form and then takes that input (a user ID) and makes the value of that input available as a variable for displaying sth like user info from the mybb_users table on that page.



Try this:

<?php

global $headerinclude, $header, $theme, $footer, $lang;

my_page_lang();

$link = my_page_url($pages['url']);
$title = $pages['name'];

if($mybb->input['action'] == 'do_submit' && $mybb->request_method == 'post')
{
	verify_post_check($mybb->input['my_post_key']);

	if($mybb->input['uid'] == '')
	{
		$errors[] = $lang->page_input_error_empty;
	}
	else
	{
		$query = $db->simple_select('users', '*' , 'uid = ' . $mybb->input['uid']);

		if($db->num_rows($query) != 1)
		{
			$errors[] = $lang->page_input_error_select;
		}
	}

	if(count($errors) > 0)
	{
		$errors = inline_error($errors);
	}
	else
	{
		$result = $db->fetch_array($query);
		$userinfo_title = $lang->sprintf($lang->page_userinfo, $result['username']);
		eval("\$userinfo = \"" . my_page_template('userinfo') . "\";");
	}
}
elseif($mybb->input['uid'] && $mybb->request_method == 'get')
{
	error($lang->sprintf($lang->page_input_error_get, $link));
}

add_breadcrumb($title);

eval("\$page = \"" . my_page_template('page') . "\";");

output_page($page);

function my_page_url($url, $parameters = false)
{
	if(is_array($parameters))
	{
		foreach($parameters as $key => $val)
		{
			$query .= '&' . $key . '=' . $val;
		}
	}

	$basename = basename(getenv('REQUEST_URI'));

	if(my_strpos($basename, 'page=' . $url))
	{
		$url_string = 'misc.php?page=' . $url . $query;
	}
	else
	{
		$url_string = my_substr($basename, 0, my_strpos($basename, '?'));

		if(isset($query))
		{
			$url_string .= '?' . my_substr($query, 1);
		}
	}

	$url_string = htmlspecialchars_uni($url_string);
	return $url_string;
}

function my_page_lang()
{
	global $lang;
	$l['page_input_uid'] = 'ID des Benutzers eingeben:';
	$l['page_input_submit'] = 'Benutzerinformationen anzeigen';
	$l['page_input_error_empty'] = 'Keine ID eingegeben.';
	$l['page_input_error_select'] = 'Die eingegebene ID gehört zu keinem Benutzer.';
	$l['page_input_error_get'] = 'Die ID des Benutzers darf nicht per Adresszeile übergeben werden. Bitte das <a href="{1}">Formular</a> dafür verwenden!';
	$l['page_userinfo'] = 'Benutzerinformationen von {1}';
	$l['page_userinfo_value'] = 'Benutzerinformation:';

	foreach($l as $key => $val)
	{
		if(!$lang->$key)
		{
			$lang->$key = $val;
		}
	}
}

function my_page_template($template)
{
	$templates = array
	(
		'page' => '<html>
<head>
<title>{$title}</title>
<meta name="description" content="{$description}" />
<link rel="canonical" href="{$mybb->settings[bburl]}/{$link}" />
{$headerinclude}
</head>
<body>
{$header}
{$errors}
{$userinfo}
<form action="{$link}" method="post">
<input type="hidden" name="my_post_key" value="{$mybb->post_code}" />
<input type="hidden" name="action" value="do_submit" />
<table border="0" cellspacing="{$theme[borderwidth]}" cellpadding="{$theme[tablespace]}" class="tborder">
<thead>
<tr>
<td class="thead" colspan="2"><strong>{$title}</strong></td>
</tr>
</thead>
<tbody>
<tr>
<td class="trow1" width="20%" valign="top"><strong>{$lang->page_input_uid}</strong></td>
<td class="trow1"><input type="text" class="textbox" name="uid" size="40" maxlength="10" tabindex="1" /></td>
</tr>
</tbody>
</table>
<br />
<div style="text-align: center;"><input type="submit" class="button" name="submit" value="{$lang->page_input_submit}" tabindex="2" /></div>
</form>
{$footer}
</body>
</html>',
		'userinfo' => '<table border="0" cellspacing="{$theme[borderwidth]}" cellpadding="{$theme[tablespace]}" class="tborder">
<thead>
<tr>
<td class="thead" colspan="2"><strong>{$userinfo_title}</strong></td>
</tr>
</thead>
<tbody>
<tr>
<td class="trow1" width="20%" valign="top"><strong>{$lang->page_userinfo_value}</strong></td>
<td class="trow1">{$result[\'email\']}</td>
</tr>
</tbody>
</table>
<br />'
	);
	$templates[$template] = str_replace("\'", "'", addslashes($templates[$template]));
	return $templates[$template];
}

?>

I outsourced the template and language strings into functions. Feel free to store them in a language file and/or a MyBB template.

The important part is the template userinfo: At the moment it shows the email address of the user. Try changing {$result[\'email\']} (line 146) to something different (for example: {$result[\'regip\']}).

If you want to work with the info before output it, add your code between line 35 and 36 (directly before eval).

Schöne Grüße aus Leipzig! Cool
Wow. Thanks a lot.
(edit) - works like a charm - even with a whole table of values.

Liebe Grüße vom Bayrischen Platz. Wink