[Question] Getting 5 Most Recent Forum Posts on Home Page

  • (2 Pages)
  • +
  • 1
  • 2

29 Replies - 1720 Views - Last Post: 12 May 2015 - 06:11 PM Rate Topic: -----

#1 Tyr4el  Icon User is offline

  • D.I.C Addict

Reputation: 23
  • View blog
  • Posts: 507
  • Joined: 24-October 14

[Question] Getting 5 Most Recent Forum Posts on Home Page

Posted 30 April 2015 - 12:26 PM

I'm looking into PHP for building my site recently. We've implemented PHPBB3 for our forums and are using a free hosting site. The site has PHP and SQL with FTP though so it's quite a catch. I just don't think it's that well known. But anyway, on to my question.

I'm trying to build the home page. It's currently only in concept right now as I'm working on the layout but I think it'd be cool for the home page to get the most recent 5 posts from the forums. I know this would require PHP and SQL together (they often go hand in hand, no?).

Our host has a control panel of sorts that allows us to see the database and relevant tables. I found the table within the database and I know it's the right table because it has all the fields I'd want (username, text, forum, etc.). However, beyond knowing this information, I'm lost as to how to GET it onto my home page. I know the home page will need a .php extension, but other than that I'm lost.

I don't want a direct code answer but any kind of direction would be useful. I've tried Googling to no avail. Can PHP understand SQL database and field names in its code? For example, can you write something in PHP and call a SQL table and field and display it. That's really what I need...I think.

Any help would be appreciated!

Is This A Good Question/Topic? 0
  • +

Replies To: [Question] Getting 5 Most Recent Forum Posts on Home Page

#2 andrewsw  Icon User is online

  • the case is sol-ved
  • member icon

Reputation: 6379
  • View blog
  • Posts: 25,770
  • Joined: 12-December 12

Re: [Question] Getting 5 Most Recent Forum Posts on Home Page

Posted 30 April 2015 - 01:55 PM

Quote

Can PHP understand SQL database and field names in its code?

Every half-decent tutorial or book about PHP will start to talk about MySQL, and how PHP interacts with it, at a very early stage. Interacting with, and retrieving data from, a database is one of the main purposes of a server-side language like PHP.

What book are you studying?
Was This Post Helpful? 0
  • +
  • -

#3 Tyr4el  Icon User is offline

  • D.I.C Addict

Reputation: 23
  • View blog
  • Posts: 507
  • Joined: 24-October 14

Re: [Question] Getting 5 Most Recent Forum Posts on Home Page

Posted 30 April 2015 - 01:57 PM

Not a book per se, but a bunch of online resources such as Code Academy and Tizag. I haven't gone into too much depth. The PHP thing I was leaving to my friend to work with but this challenge got me interested in at least learning something about PHP and SQL so I can use it on the home page. So to be honest, I haven't read much. Just bits and pieces. I need to start with Code Academy on PHP from Step 1 and go from there. That will give a good foundation. Then I can go to some other free resources or even pay at Code School for the PHP track.

And actually, after posting this, I read some more and found that PHP does interact with SQL and I just didn't edit my post :P. So now it's all about learning and getting what I need. I need to get the data formatted the way I want on the home page (probably in a div in a table formatted with CSS?).

The most I've gotten is I need to connect to the SQL database in the PHP script :P. That's as far as I got.

This post has been edited by Tyr4el: 30 April 2015 - 01:59 PM

Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6245
  • View blog
  • Posts: 24,013
  • Joined: 23-August 08

Re: [Question] Getting 5 Most Recent Forum Posts on Home Page

Posted 30 April 2015 - 02:07 PM

MySQL has the ORDER BY clause and LIMIT clause. Documentation for SELECT: https://dev.mysql.co.../en/select.html (includes both ORDER BY and LIMIT)
Was This Post Helpful? 1
  • +
  • -

#5 Tyr4el  Icon User is offline

  • D.I.C Addict

Reputation: 23
  • View blog
  • Posts: 507
  • Joined: 24-October 14

Re: [Question] Getting 5 Most Recent Forum Posts on Home Page

Posted 30 April 2015 - 02:18 PM

The most I have is SELECT post_text FROM phpbb_posts. But that's only the query. I don't know how to format the results into my Html at all. I don't even know if that will return the text itself or something else.


Sent from my iPhone using Tapatalk
Was This Post Helpful? 0
  • +
  • -

#6 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 879
  • View blog
  • Posts: 1,893
  • Joined: 30-January 09

Re: [Question] Getting 5 Most Recent Forum Posts on Home Page

Posted 30 April 2015 - 06:07 PM

While it's important to learn how PHP and MySQL work, it's also important to know that there are resources available where you can self-help. phpBB is a very popular forum software that has a lot of support from it's creators and community. A quick search for "phpbb latest posts" reveals the following links:

With problems like this, where it's likely that lots of other people need an answer, there's usually established solutions floating about. This is not to say copying and pasting other people's code is necessarily a good idea - there's a lot of bad code and coders out there. But in this case, we have solutions provided by the phpBB team and community themselves, who are pretty authoritative sources :)/> Just make sure that when you use other people's code, you understand what it is doing. Read through every line and figure out why each line exists and what it does. This will make it easier when you integrate it into your code, and if you need to support / maintain / bugfix the code.

This post has been edited by e_i_pi: 30 April 2015 - 06:08 PM
Reason for edit:: Grammar nazi-ing my own post

Was This Post Helpful? 2
  • +
  • -

#7 Tyr4el  Icon User is offline

  • D.I.C Addict

Reputation: 23
  • View blog
  • Posts: 507
  • Joined: 24-October 14

Re: [Question] Getting 5 Most Recent Forum Posts on Home Page

Posted 30 April 2015 - 06:26 PM

You're a life saver. That's what I did Google and I didn't find anything. I mean, my search was "PHP latest forum posts" so your search differed a little but I figured I should have found something. I'll take a look at that first link. Browsing through all of them real quick, I think that provides the most information and what I need. Now I just need to learn PHP to see what the hell it's saying :P.

Thank you for your help! I appreciate it. This will be of much use on our site.

Question though - how do I test PHP while I'm writing it? I have Brackets...which I think can write in PHP but I'm not on it at the moment so I'm not sure. I assume since PHP can be written within HTML, if you save the file as .php, it will still recognize HTML tags but also will recognize PHP. But then again, it won't have access to the database it needs, so I still don't know how to test it :P (yay for typing out thoughts!).

Thanks!
Was This Post Helpful? 0
  • +
  • -

#8 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 879
  • View blog
  • Posts: 1,893
  • Joined: 30-January 09

Re: [Question] Getting 5 Most Recent Forum Posts on Home Page

Posted 30 April 2015 - 07:14 PM

View PostTyr4el, on 01 May 2015 - 12:26 PM, said:

Question though - how do I test PHP while I'm writing it? I have Brackets...which I think can write in PHP but I'm not on it at the moment so I'm not sure.

Adobe Brackets should be able to handle PHP markup. If not, you can always use the coders fallback of Notepad++, which isn't an IDE but it does do markup.

Quote

I assume since PHP can be written within HTML, if you save the file as .php, it will still recognize HTML tags but also will recognize PHP.

Yes, but when you embed PHP in HTML, you have to use the tags <?php ?>. Also, your web server has to execute .php files using the PHP engine. Your server will either be Apache or IIS, and they should be automatically configured to recognise .php files as required PHP execution, but only if PHP is installed on the server. An easy way to check this is to create a file called test.php, and edit the contents to be <?php echo 'Hello World' ?>. If, when you navigate to the page, the words "Hello World" are displayed, then PHP is working.

Quote

But then again, it won't have access to the database it needs, so I still don't know how to test it :P/>/> (yay for typing out thoughts!).

You'll need a development environment to do your testing. I strongly advise against using production for testing. For a development environment, you'll need to set up a web server, database server, and PHP. Do a search for "LAMP stack" if you have a Linux box, or "WAMP stack" if you have a Windows box (for your development environment). LAMP stands for Linux Apache MySQL PHP, WAMP stands for Windows Apache MySQL PHP. These are shrink-wrapped packages that contain everything you need to set up a dev environment, though sometimes configuration can be a little tricky. LAMP/WAMP stack producers usually have forums where you can ask for help during setup. Once you've got your stack set up, you'll need to export your prod database to a file, download it to your dev environment, and import it into your dev database.

This post has been edited by e_i_pi: 30 April 2015 - 07:16 PM

Was This Post Helpful? 1
  • +
  • -

#9 Tyr4el  Icon User is offline

  • D.I.C Addict

Reputation: 23
  • View blog
  • Posts: 507
  • Joined: 24-October 14

Re: [Question] Getting 5 Most Recent Forum Posts on Home Page

Posted 05 May 2015 - 07:42 PM

So in the first link you gave me above, would I include in my PHP file, the Header, Functions and Example 1 only? I only want the first five recent announcements. I may just make it unlimited but are those the only parts I need?

Also, I don't see anything in there about creating the HTML to output the query to. I assume that's something I'd have to do on my own, yes?

Thanks a lot for your help!
Was This Post Helpful? 0
  • +
  • -

#10 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 879
  • View blog
  • Posts: 1,893
  • Joined: 30-January 09

Re: [Question] Getting 5 Most Recent Forum Posts on Home Page

Posted 07 May 2015 - 05:11 AM

So in the first link, there is a section titled "Example 2: Display first post from the last five topics". That's the one you want. You will have to assign the value 5 to the variable $search_limit, and in the while block is where you would have your HTML - note the part of the while block that starts with $template->assign_block_vars. You would replace that function with your output HTML, and use the variables that that function call uses, something like this:
echo 'TITLE: ' . censor_text($topic_title) . '<br>';
echo 'AUTHOR: ' . $topic_author . '<br>';
echo censor_text($post_text) . '<br>';


Was This Post Helpful? 1
  • +
  • -

#11 Tyr4el  Icon User is offline

  • D.I.C Addict

Reputation: 23
  • View blog
  • Posts: 507
  • Joined: 24-October 14

Re: [Question] Getting 5 Most Recent Forum Posts on Home Page

Posted 08 May 2015 - 11:27 AM

Thanks for your help! What do the => and -> symbols mean? I thought they were just ways to draw arrows without having an arrow symbol in text lol. Do I need to replace any code in there?

And just out of curiosity, why are we replacing that part in the while block? What does it do otherwise? I see the example that's linked on that page too and it looks exactly like I want but where do I put something like a div element in that PHP code? I want to wrap it in a div so I can easily style it in CSS later.

This post has been edited by Tyr4el: 08 May 2015 - 11:37 AM

Was This Post Helpful? 0
  • +
  • -

#12 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3714
  • View blog
  • Posts: 13,472
  • Joined: 08-August 08

Re: [Question] Getting 5 Most Recent Forum Posts on Home Page

Posted 08 May 2015 - 04:41 PM

View PostTyr4el, on 08 May 2015 - 02:27 PM, said:

And just out of curiosity, why...

The secret to programming is to write and test. When you do that, you don't need to ask why.
Was This Post Helpful? 0
  • +
  • -

#13 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 879
  • View blog
  • Posts: 1,893
  • Joined: 30-January 09

Re: [Question] Getting 5 Most Recent Forum Posts on Home Page

Posted 08 May 2015 - 05:08 PM

View PostTyr4el, on 09 May 2015 - 05:27 AM, said:

Thanks for your help! What do the => and -> symbols mean? I thought they were just ways to draw arrows without having an arrow symbol in text lol. Do I need to replace any code in there?

=> is assignment of a value to an array key
-> is the calling of a method on an object

Quote

And just out of curiosity, why are we replacing that part in the while block? What does it do otherwise? I see the example that's linked on that page too and it looks exactly like I want but where do I put something like a div element in that PHP code? I want to wrap it in a div so I can easily style it in CSS later.

That part of the while block is building an object. You don't want to build an object necessarily, you want to output HTML (I say necessarily, because you can get an object to output HTML, but only if that's the way the object is built in code).

You can wrap it in a div element, that's fine, however you want to build your HTML output.
Was This Post Helpful? 0
  • +
  • -

#14 Tyr4el  Icon User is offline

  • D.I.C Addict

Reputation: 23
  • View blog
  • Posts: 507
  • Joined: 24-October 14

Re: [Question] Getting 5 Most Recent Forum Posts on Home Page

Posted 11 May 2015 - 05:35 PM

I'm getting the following error:

Fatal error: Call to a member function sql_build_query() on a non-object in /home/vhosts/ascendedkhaos.orgfree.com/index3.php on line 56

But I've looked through the code and can't figure out what it means. I found the function sql_build_query() but don't know what non-object it's calling anywhere.

Here's the php portion of the site:
<?php
        $posts_ary = array(
        'SELECT' => 'p.*, t.*',
    
        'FROM' => array(
            POSTS_TABLE => 'p',
        ),
    
        'LEFT_JOIN' => array(
            array(
                'FROM' => array(TOPICS_TABLE => 't'),
                'ON' => 't.topic_first_post_id = p.post_id'
            )
        ),
    
        'WHERE' => str_replace( array('WHERE ', 'forum_id'), array('', 't.forum_id'), $forum_id_where) . '
                        AND t.topic_status <> ' . ITEM_MOVED . '
                        AND t.topic_approved = 1',
    
        'ORDER_BY' => 'p.post_id DESC',
    );
    
    $posts = $db->sql_build_query('SELECT', $posts_ary);

   $posts_result = $db->sql_query_limit($posts, $search_limit);

      while( $posts_row = $db->sql_fetchrow($posts_result) )
      {
         $topic_title       = $posts_row['topic_title'];
         $topic_author       = get_username_string('full', $posts_row['topic_poster'], $posts_row['topic_first_poster_name'], $posts_row['topic_first_poster_colour']);
         $topic_date       = $user->format_date($posts_row['topic_time']);
         $topic_link       = append_sid("{$phpbb_root_path}viewtopic.$phpEx", 'f=' . $posts_row['forum_id'] . '&amp;t=' . $posts_row['topic_id']);

         $post_text = nl2br($posts_row['post_text']);

         $bbcode = new bbcode(base64_encode($bbcode_bitfield));         
         $bbcode->bbcode_second_pass($post_text, $posts_row['bbcode_uid'], $posts_row['bbcode_bitfield']);

         $post_text = smiley_text($post_text);

         echo 'TITLE: ' . censor_text($topic_title) . '<br>';
         echo 'AUTHOR: ' . $topic_author . '<br>';
         echo censor_text($post_text) . '<br>';
      }
        ?>

Can anyone tell me what I'm doing wrong? I can fix the HTML output later but for now I think it'd be great if I just got this working.
Was This Post Helpful? 0
  • +
  • -

#15 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3714
  • View blog
  • Posts: 13,472
  • Joined: 08-August 08

Re: [Question] Getting 5 Most Recent Forum Posts on Home Page

Posted 11 May 2015 - 09:16 PM

Where have you initialized $db?
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2