PHP

 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me



Go Back   Tutorialized ForumsWeb Design & DevelopmentPHP

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Tutorialized Forums Sponsor:
  #1  
Old January 30th, 2004, 12:30 AM
kurt kurt is offline
Contributing User
Tutorialized Novice (500 - 999 posts)
 
Join Date: Jan 2004
Posts: 507 kurt New User: is a brand new recruit and a unknown entity at this point. 
Time spent in forums: 5 m 48 sec
Reputation Power: 0
[PHP] Show the latest forum posts on your website.

Having a website is cool, having a forum is fun.. but it's really cool and fun when people post to your forum, and it shows up on your website.. See how to do that here.

For this example I am going to show you how to add the latest posts to your website from a PHPBB forum, like we do on Tutorialized.com.

What you need:
PHP, MySQL, PHPBB, a Website.

I'll assume you already have PHPBB installed and running.

1) Okay, so first you need to setup your database connection in PHP so it can see the PHPBB tables:

Code:
$server = 'localhost';  // server location
$user = 'db_user';     // db username
$password = 'db_password';   // db password
$db = 'forum_db';  // db name

$link = mysql_connect("$server", "$user", "$password");
mysql_select_db("$db");


Now when we run a mysql query, PHP will know where to look for the tables.

2) Next, we create the query that will read from the PHPBB database the last 10 posts, by title, and sort them from newest to oldest, and then execute it:

Code:
$query = "select distinct t.topic_id, t.topic_title
        from posts p, topics t
        where p.topic_id = t.topic_id
        order by p.post_time desc
        limit 10";
$result = mysql_query($query);


So what did we do there, well I'll break it down.

We're selecting the topic_id, which is the unique identifier that each topic in PHPBB receives, and the name of the topic, topic_title. Both are located in the "topics" table (may be phpbb_topics in your database).

Next, we join the topic_id in the topics table with the topic_id in the posts table. The posts table contains the unique ids for each post that occurs inside a given topic. In the beginning of the query I also used the term "distinct", this will limit the results to show a topic only once, even if there are 3 new posts under that topic.

Then we tell mysql to sort the results from newest to oldest based on the time the posts took place (post_time), in the posts table.

And finally we tell mysql to only return the first 10 results.

3) So now that we have our results from mysql, we need to display them:

I like to display the results in an ordered list:

Code:
print "<ul>";
while ($row = mysql_fetch_assoc($result)) {
        $topic_id = $row['topic_id'];
        $topic_title = $row['topic_title'];
        print "[*]<a href=\"/forum/viewtopic.php?t=$topic_id\">$topic_title</a>";
}
print "[/list]";


Wow, so what the heck did I just do? It's actually really simple!

First, I told php to grab the results from the query we did in step 2 and as long as the results were returning a row of data, to assign those results to the $row variable. The mysql_fetch_assoc function tells php to use the same column names that mysql uses, so topic_id in mysql is put into our $row variable as $row['topic_id']. Ta Da.

Then, for each row of data that mysql passes down to php, I print out the information.. only I do it in a slightly prettier format then just plain text.

Before the while statement, I started a list, and then for each row of data we're printing I put that row of data as an item in the list.

Also, rather then just printing out the title and only listing the last 10 topics.. I make it so the title of each topic has a link that goes to the forum and directly to the topic. This is done by setting your href to point to the "viewtopic.php" script in PHPBB and adding the topic_id to the passed url.

So, when you're all done and you've prettied up the text and added it to your website. Everytime someone posts to a topic in your forum, that topic will show up as the most recent topic and people from your website can just click on the link to be taken to your forum. Fun stuff with just a little code.

Reply With Quote
  #2  
Old February 15th, 2004, 11:54 AM
Blade Blade is offline
Contributing User
Tutorialized Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 20 Blade New User: is a brand new recruit and a unknown entity at this point. 
Time spent in forums: < 1 sec
Reputation Power: 0
Wow kurt nice tutorial man.

Reply With Quote
  #3  
Old February 23rd, 2004, 12:48 AM
Blade Blade is offline
Contributing User
Tutorialized Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 20 Blade New User: is a brand new recruit and a unknown entity at this point. 
Time spent in forums: < 1 sec
Reputation Power: 0
I tried following the script and got a error.

Quote:
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home2/xdinc/public_html/forumtopics.php on line 19

Reply With Quote
  #4  
Old February 26th, 2004, 07:30 PM
AnthraX AnthraX is offline
Contributing User
Tutorialized Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Location: Anchorage, Alaska
Posts: 3 AnthraX New User: is a brand new recruit and a unknown entity at this point. 
Time spent in forums: < 1 sec
Reputation Power: 0
Man, Ive been looking at how to do this forever. Is it all right if I make a tutorial on my site on how to do this? I'll give u full credit.
__________________

Reply With Quote
  #5  
Old February 26th, 2004, 10:55 PM
kurt kurt is offline
Contributing User
Tutorialized Novice (500 - 999 posts)
 
Join Date: Jan 2004
Posts: 507 kurt New User: is a brand new recruit and a unknown entity at this point. 
Time spent in forums: 5 m 48 sec
Reputation Power: 0
That's fine with me.. Just link to the site.

Thanks for asking,

Kurt

Reply With Quote
  #6  
Old February 29th, 2004, 08:34 PM
Anonymous Anonymous is offline
Contributing User
Tutorialized Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 19 Anonymous New User: is a brand new recruit and a unknown entity at this point. 
Time spent in forums: < 1 sec
Reputation Power: 0
Kurt, can u give the code how to put a QUICK REPLY at the bottom of this forum ?? I realy2 need it..

B'coz lots of people are to lazy to load a new page just to post/reply a topic. Thx

Reply With Quote
  #7  
Old February 29th, 2004, 10:58 PM
Anonymous Anonymous is offline
Contributing User
Tutorialized Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 19 Anonymous New User: is a brand new recruit and a unknown entity at this point. 
Time spent in forums: < 1 sec
Reputation Power: 0
What if i wanna to display not just the topics but also the last user who post/reply a topic..
Like this :

Need help, PHP problem [last posted by Tom-182 :: Sun Feb 29, 2004 8:56 PM]

And what if i have a anncouncement or sticky in my forum ?? I don't want sticky & announcement appear at the website (only general topics appear on the site)

And as for Blade, you should change
Code:
 $query = "select distinct t.topic_id, t.topic_title 
    from posts p, topics t 
    where p.topic_id = t.topic_id 
    order by p.post_time desc 
    limit 10"; 
$result = mysql_query($query);


into

Code:
$query = "select distinct t.topic_id, t.topic_title 
    from phpbb_posts p, phpbb_topics t 
    where p.topic_id = t.topic_id 
    order by p.post_time desc 
    limit 10"; 
$result = mysql_query($query);


in order to make the scripts works...just take a good look at your phpbb database & see the list of table in there...

Reply With Quote
  #8  
Old June 5th, 2004, 01:46 AM
Jynxx Jynxx is offline
Contributing User
Tutorialized Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 16 Jynxx New User: is a brand new recruit and a unknown entity at this point. 
Time spent in forums: < 1 sec
Reputation Power: 0
This is EXACTLY what I have been looking for.... but you'll have to forgive me. I'm a total newb when it comes to PHP and SQL. I got my phpbb up and running and tweaked everything the way that I want it... I am now building my home page, and this is exactly what I need.
Now, my questions are... could you explain it more for me? Where do I put that code? Do I make my index page as a php page or an html page?
Also, is there a way to display the top 10 of a specific catagory? Like if I had News, Sports, Weather... is it possible to only display the top 10 posts in News? That would be a HUGE help for me.

Sorry if this bugs you guys, but I am really at my wits end in trying to figure out how to get this to work

Reply With Quote
  #9  
Old June 5th, 2004, 12:32 PM
kurt kurt is offline
Contributing User
Tutorialized Novice (500 - 999 posts)
 
Join Date: Jan 2004
Posts: 507 kurt New User: is a brand new recruit and a unknown entity at this point. 
Time spent in forums: 5 m 48 sec
Reputation Power: 0
Hey Jynxx, welcome to the forum.

You'll want to add this code to your index page, and that page should be a php page.

You can limit it to display only certain categories, you would do that by editing the SQL to only look at the posts in a certain forum/group.

So if I wanted to only show the last 10 posts for the "General Chat" forum here.. I know that it's forum_id is "1", so I would change the code like so:

Code:
$query = "select distinct t.topic_id, t.topic_title 
        from posts p, topics t
        where p.topic_id = t.topic_id 
        and t.forum_id = 1
        order by p.post_time desc 
        limit 10"; 
$result = mysql_query($query); 

Reply With Quote
  #10  
Old June 5th, 2004, 12:42 PM
Jynxx Jynxx is offline
Contributing User
Tutorialized Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 16 Jynxx New User: is a brand new recruit and a unknown entity at this point. 
Time spent in forums: < 1 sec
Reputation Power: 0
Ok, so all I basically have to do is figure out what table I want it to show and add ONLY that code?
That seems too easy to be true... I've been bustin my head tryin to do this lol

Reply With Quote
  #11  
Old June 5th, 2004, 03:34 PM
Jynxx Jynxx is offline
Contributing User
Tutorialized Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 16 Jynxx New User: is a brand new recruit and a unknown entity at this point. 
Time spent in forums: < 1 sec
Reputation Power: 0
I still dont get it... I tried putting that code everywhere on a page... in the body, before the body, after the body.... nothing comes up, or it shows the actual code on the page.
Man, I must sound REALLY stupid about all of this ;)

Reply With Quote
  #12  
Old June 5th, 2004, 11:40 PM
kurt kurt is offline
Contributing User
Tutorialized Novice (500 - 999 posts)
 
Join Date: Jan 2004
Posts: 507 kurt New User: is a brand new recruit and a unknown entity at this point. 
Time spent in forums: 5 m 48 sec
Reputation Power: 0
Hey Jynxx-

What web server are you running?

Do you have PHP installed?

Did you configure the webserver to use PHP to parse the files with a .PHP extension?

Reply With Quote
  #13  
Old June 6th, 2004, 10:02 AM
Jynxx Jynxx is offline
Contributing User
Tutorialized Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 16 Jynxx New User: is a brand new recruit and a unknown entity at this point. 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally Posted by Kurt
Hey Jynxx-

What web server are you running?

Apache

Quote:
Do you have PHP installed?

Yep, PHP 4
Quote:
Did you configure the webserver to use PHP to parse the files with a .PHP extension?

Dont know what you mean, but my serever runs PHP files fine. I can use the phpBB with no problems and have installed about 3 mods on it so far.

Reply With Quote
  #14  
Old June 7th, 2004, 11:32 AM
Jynxx Jynxx is offline
Contributing User
Tutorialized Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 16 Jynxx New User: is a brand new recruit and a unknown entity at this point. 
Time spent in forums: < 1 sec
Reputation Power: 0
Ok, maybe I am doing this wrong...
1) Like I said, I know nothing about php... do I have to put each of those codes inside a <?php tag, or can all of them go in the same tag?
2) I am assuming I put the script in the table that I want it to be in... right?
3) do I copy that code verbatim, or is there anything I have to change to reflect my specific db name, and if so... what?

Sorry to be such a bother, but this seems like such a great place to get info!!

Reply With Quote
  #15  
Old June 7th, 2004, 12:53 PM
Jynxx Jynxx is offline
Contributing User
Tutorialized Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 16 Jynxx New User: is a brand new recruit and a unknown entity at this point. 
Time spent in forums: < 1 sec
Reputation Power: 0
Ok, last thing.... I just made this script in my page and it worked fine, so I know I could run the code, so I dont know what I am doing wrong.

Code:
<?php

$myvar = "This is a test!!!";

echo $myvar;

?>

Reply With Quote
Reply

Viewing: Tutorialized ForumsWeb Design & DevelopmentPHP > [PHP] Show the latest forum posts on your website.


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 

Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.

© 2003-2014 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap