2006-10-31, 01:34 AM
This tutorial is by me, but the code used is mostly modified from code by other user tutorials, so don't think all the PHP is mine.
Creating a Login Form/Basic Userbox
Modify the code and then insert it on your page. My forum is on a separate subdomain, so I have put the login/userbox in a separate file on my subdomain, then put it on my site in an iframe.
Insert this code at the very top of your Web page code, before all other PHP or HTML.
Creating a Recent Posts Box
Edit the code accordingly. If you have already added the chdir() because you added the Userbox, do not add it again, nor the $rel line.
Forum Statistics
None of this code was by me, but I shortened it. Modify it accordingly. Do not include the configuration lines and connection lines (from $host to $sel) lines if you already inserted them earlier in the same file.
That concludes my tutorial.
EDIT (11.04.2006 @ 15:17 GMT-7 by lupus2k5): Fixed basic errors (lack of parenthesi and lack of script end--?>)./EDIT
Creating a Login Form/Basic Userbox
Modify the code and then insert it on your page. My forum is on a separate subdomain, so I have put the login/userbox in a separate file on my subdomain, then put it on my site in an iframe.
Insert this code at the very top of your Web page code, before all other PHP or HTML.
<?php
$rel = "forum/"; // The directory to your forum--relative to this file's location; include ending slash
chdir($rel);
require("./global.php");
?>
Include this in your head tag:<script>
function submitted()
{
setTimeout("refresh()",6000);
}
function refresh()
{
var sURL = unescape(window.location.pathname);
window.location.href = sURL;
}
</script>
Then you can insert this code where you want the form to appear.<?php
if($mybb->user["uid"])
{
?>
• <a href="<?php echo $rel; ?>search.php?action=getnew">View new posts</a><br>
• <a href="<?php echo $rel; ?>private.php">View new PM's</a><br>
• <a href="<?php echo $rel; ?>usercp.php">User CP</a><br>
<b>·</b> <a href="<?php echo $rel; ?>usercp.php?action=editsig">Edit Signature</a><br>
<b>·</b><a href="<?php echo $rel; ?>usercp.php?action=avatar">Edit Avatar</a><br>
• <a href="<?php echo $rel; ?>member.php?action=profile&uid=<?php echo $mybb->user["uid"]; ?>">My Profile</a><br>
• <a href="<?php echo $rel; ?>search.php?action=finduser&uid=<?php echo $mybb->user["uid"]; ?>">My Posts</a>
<?php
}
else
{
?>
<table>
<form method="POST" action="<?php echo $rel; ?>member.php" onsubmit="return submitted();">
<input type="hidden" name="action" value="do_login">
<input type="hidden" name="url" value="">
<tr>
<td>Username</td>
<td><input type="text" name="username" size="15"></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="password" size="15"></td>
</tr>
<tr>
<td colspan="2"><center><input type="submit" value="Login" name="submit"></center></td>
</tr>
<tr>
<td colspan="2"><center><a href="<?php echo $rel; ?>member.php?action=register">Not a member? Register now!</a></center></td>
</form>
</table>
<?php
}
?>
I didn't create a list, I just used bullet characters, as they do not do the <p> break as a <ul> would.Creating a Recent Posts Box
Edit the code accordingly. If you have already added the chdir() because you added the Userbox, do not add it again, nor the $rel line.
<?php
$rel = "forum/"; // The directory to your forum--relative to this file's location; include ending slash
chdir($rel);
$host = "localhost"; // Your database host
$user = "root"; // Your database username
$pass = "password"; // Your database password
$data = "mybb"; // The name of your database
$prfx = "mybb_"; // The table prefix of your database
$col1 = "#FFFFFF"; // The color of the first row--alternating row colours
$col2 = "#FCFCFC"; // The color of the second row
// NOTE: I didn't include ./global.php because I wasn't sure what the variables look like.
$num = 5; // The number of recent posts to show
$author = TRUE; // Should we display the post author? (TRUE or FALSE)
$conn = mysql_connect($host,$user,$pass) or die("<b>Error:</b> Database connection failed.");
$sel = mysql_select_db($data,$conn) or die("<b>Error:</b> Database connection failed.");
if($author == TRUE)
{
?>
<table>
<?php
}
$i = 0;
$q = mysql_query("SELECT * FROM ".$prfx."posts ORDER BY dateline DESC LIMIT ".$num);
while($row = mysql_fetch_array($q))
{
if($i % 2 == 0)
{
$bgcolor = $col1;
}
else
{
$bgcolor = $col2;
}
$i++;
$q2 = mysql_query("SELECT * FROM ".$prfx."forums WHERE fid = '".$row["fid"]."';");
$row = mysql_fetch_array($q2);
?>
<tr>
<td width="50%" bgcolor="<?php echo $bgcolor; ?>">
<a title="In forum: <?php echo $row["name"]; ?>" href="<?php echo $rel; ?>showthread.php?tid=<?php echo $row["tid"]; ?>&action=lastpost">
<b><?php echo $row["subject"]; ?></b></a></p></td>
<?php
if($author == TRUE)
{
?>
<td width="50%" bgcolor="<?php echo $bgcolor; ?>">by
<b><a title="<?php echo $row["username"]; ?>'s Profile" href="<?php echo $rel; ?>member.php?action=profile&uid=<?php echo $row["uid"]; ?>">
<?php echo $row["username"]; ?></b></a></td>
<?php
}
?>
</tr>
<?php
}
?>
</table>
Forum Statistics
None of this code was by me, but I shortened it. Modify it accordingly. Do not include the configuration lines and connection lines (from $host to $sel) lines if you already inserted them earlier in the same file.
<?php
$host = "localhost"; // Your database host
$user = "root"; // Your database username
$pass = "password"; // Your database password
$data = "mybb"; // The name of your database
$prfx = "mybb_"; // The table prefix of your database
$conn = mysql_connect($host,$user,$pass) or die("<b>Error:</b> Database connection failed.");
$sel = mysql_select_db($data,$conn) or die("<b>Error:</b> Database connection failed.");
$topic_result = mysql_query("SELECT COUNT(*) as count FROM ".$prfx."threads");
$topic_count = mysql_fetch_array($topic_result);
$topic_count = $topic_count["count"];
$post_result = mysql_query("SELECT COUNT(*) as count FROM ".$prfx."posts");
$post_count = mysql_fetch_array($post_result);
$post_count = $post_count["count"];
$user_result = mysql_query("SELECT COUNT(*) as count FROM ".$prfx."users");
$user_count = mysql_fetch_array($user_result);
$user_count = $user_count["count"];
$forum_result = mysql_query("SELECT COUNT(*) as count FROM ".$prfx."forums");
$forum_count = mysql_fetch_array($forum_result);
$forum_count = $forum_count["count"];
?>
<table>
<tr>
<td><b><?php echo $topic_count; ?></b></td>
<td>Threads</td>
</tr>
<tr>
<td><b><?php echo $post_count; ?></b></td>
<td>Posts</td>
</tr>
<tr>
<td><b><?php echo $user_count; ?></b></td>
</tr>
<tr>
<td><b><?php echo $forum_count; ?></b></td>
<td>Forums</td>
</tr>
</table>
That concludes my tutorial.
EDIT (11.04.2006 @ 15:17 GMT-7 by lupus2k5): Fixed basic errors (lack of parenthesi and lack of script end--?>)./EDIT