MyBB Community Forums

Full Version: [Tutorial] Basic Site-Forum Integration
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3 4 5 6 7
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.
<?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"])
{
?>
&nbsp;• <a href="<?php echo $rel; ?>search.php?action=getnew">View new posts</a><br>
&nbsp;• <a href="<?php echo $rel; ?>private.php">View new PM's</a><br>
&nbsp;• <a href="<?php echo $rel; ?>usercp.php">User CP</a><br>
&nbsp;&nbsp;&nbsp;&nbsp; <b>·</b> <a href="<?php echo $rel; ?>usercp.php?action=editsig">Edit Signature</a><br>
&nbsp;&nbsp;&nbsp;&nbsp; <b>·</b><a href="<?php echo $rel; ?>usercp.php?action=avatar">Edit Avatar</a><br>
&nbsp;• <a href="<?php echo $rel; ?>member.php?action=profile&uid=<?php echo $mybb->user["uid"]; ?>">My Profile</a><br>
&nbsp;• <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
Why are you opening another MySQL connection? Since you're including the global.php, why not just use the MyBB database class?
Okey, not to be that "negative" about this, great that your trying. Smile
I'm unsure why you are using chdir there, as off 1.2 you don't need to.
You can use
require $rel."global.php";
As for the querys, you should change mysql_query to $db->query and you don't have to connect to the database again when including global.php as Christian said.
mysql_fetch_array should be $db->fetch_array. or $db->fetch_field($forum_result, 'count'); for example Smile
Hope this helps you Smile
i ask once again, if my site is not on the same host/adress, can i use this code and how? thanks.
Yeah, I had a lot of issues. I also forgot a parenthesi and I didn't use define("IN_MYBB",1).
And, for D-Man, I just included an Iframe.
GREAT! now somebody makes a tutorial on integrating. when i spent a week coding to get my website to integrate with mybb, shahzam.com , if you want any scripts on there to integrate with mybb ask me.
It didn't work for me...

Does having <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> make this a problem?
no, put you should put the include on top of that.
Oh, well that's what I did. How about having the stuff on top and having the rest in a different file? Is that a problem? (I'm using SSI)
You need to use the a PHP document, SSI isn't not the same as PHP.
Pages: 1 2 3 4 5 6 7