News:

SMF 2.1.6 has been released! Take it for a spin! Read more.

Main Menu

SSI - showing an entire 'thread' with posts [PHP] ?

Started by Beaverton, November 29, 2011, 04:44:33 PM

Previous topic - Next topic

Beaverton

I got the part where I can show a single post, but how does one show an entire board with posts? (an active board for that matter)

I'm using PHP. Any help is appreciated. I don't want to load the entire forum - just pull the posts from the board and display all of them.

OK - this is what I'm using right now, the fetchPosts:

$post_info = current(ssi_fetchPosts(123456, false, 'array'));

How does one display the the other posts? If they are new posts, I wouldn't know their postID, how do I make it show all without specifying a postID?

EDIT: I keep saying board - I mean THREAD. How do I show a specific THREAD with all posts?

Beaverton

Bump.. will this require a specific function to be created?

Currently, at least to my knowledge, SSI for 2.0 only supports showing a single post or recent posts. How can I show an entire thread?

Help??

Kays

Hi, since that first parameter can be an array of message ids. Why not run a query of the messages table sorted by id_topic to create an array of all message ids for that topic to use for that parameter.

If at first you don't succeed, use a bigger hammer. If that fails, read the manual.
My Mods

Beaverton

Quote from: Kays on December 07, 2011, 11:00:45 AM
Hi, since that first parameter can be an array of message ids. Why not run a query of the messages table sorted by id_topic to create an array of all message ids for that topic to use for that parameter.

Yes, but how safe is this? I prefer to continue using SSI since it seems to be cleaned and conditioned.

Kays

In the call to ssi_fetchPosts(), the first parameter where you entered the message id. That can also accept an array of message ids to show more than one post.

Either create this array manually ($posts = array(1234, 54237,76468); ) Or since you are looking for all posts in a topic. You can create this array using a database query.

If at first you don't succeed, use a bigger hammer. If that fails, read the manual.
My Mods

Beaverton

#5
Alright.. finally finished and working. For those who are curious on how to display a single topic:

<?php

//Make a MySQL Connection
include("SSI.php");
mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("databaseName") or die(mysql_error());

//Get all the data
$result = mysql_query("SELECT * FROM `smf_messages` WHERE `id_topic` = 1234")
or die(
mysql_error());  

//keeps getting the next row
while($row = mysql_fetch_array( $result )) {


//display with SSI function and BBC support
$post_info = current(ssi_fetchPosts($row['id_msg'], false, 'array'));
                echo
'<h3>', $post_info['link'], '</h3>
                <hr style="width: 65%; margin-left: 0;" />
                <p>'
, $post_info['body'], '</p>';

}

?>


$row['id_msg'] is the key, after it has loaded from topic "1234".

And $row['poster_name'] is how you get the poster's name instead of  $post_info['poster_name'].

EDIT: Can anyone comment on how safe the above code is to use on a production site?

Beaver

Kays

That's sort of what I meant but not the way to do it. current() only returns one element of the array so you're making multiple calls to ssi_fetchPosts(). You should build the array first, then use that as the parameter in ssi_fetchPosts() as such:

$post_info = ssi_fetchPosts($post_id_array, false, 'array');

However, simpler yet, the following is a re-write of the ssi_fetchPosts() function which will return all posts in a topic. Add this to the end of SSI.php before the closing ?>.


function ssi_fetchTopic($topic_id, $override_permissions = false, $output_method = 'echo')
{
global $user_info, $modSettings;

$query_where = '
m.id_topic = {int:topic}' . ($override_permissions ? '' : '
AND {query_wanna_see_board}') . ($modSettings['postmod_active'] ? '
AND m.approved = {int:is_approved}' : '');
$query_where_params = array(
'topic' => $topic_id,
'is_approved' => 1,
);

// Then make the query and dump the data.
return ssi_queryPosts($query_where, $query_where_params, '', 'm.id_msg ASC', $output_method);
}


Call it like so. Since it's an array you need to loop though each of the elements. Do change the first parameter to the id of your topic.


$posts = ssi_fetchTopic(13, false, 'array');

foreach ($posts as $post_info)
echo '<h3>', $post_info['link'], '</h3>
<hr style="width: 65%; margin-left: 0;" />
<p>', $post_info['body'], '</p>';


Notice that I didn't use current(). For more info on outputting as an array. The second FAQ covers that. ssi_boardNews() is used as an example but the principle is the same.

[README] SSI FAQ's

If at first you don't succeed, use a bigger hammer. If that fails, read the manual.
My Mods

Beaverton


petb

Quote from: Kays on December 08, 2011, 11:18:47 AM
That's sort of what I meant but not the way to do it. current() only returns one element of the array so you're making multiple calls to ssi_fetchPosts(). You should build the array first, then use that as the parameter in ssi_fetchPosts() as such:

$post_info = ssi_fetchPosts($post_id_array, false, 'array');

However, simpler yet, the following is a re-write of the ssi_fetchPosts() function which will return all posts in a topic. Add this to the end of SSI.php before the closing ?>.


function ssi_fetchTopic($topic_id, $override_permissions = false, $output_method = 'echo')
{
global $user_info, $modSettings;

$query_where = '
m.id_topic = {int:topic}' . ($override_permissions ? '' : '
AND {query_wanna_see_board}') . ($modSettings['postmod_active'] ? '
AND m.approved = {int:is_approved}' : '');
$query_where_params = array(
'topic' => $topic_id,
'is_approved' => 1,
);

// Then make the query and dump the data.
return ssi_queryPosts($query_where, $query_where_params, '', 'm.id_msg ASC', $output_method);
}


Call it like so. Since it's an array you need to loop though each of the elements. Do change the first parameter to the id of your topic.


$posts = ssi_fetchTopic(13, false, 'array');

foreach ($posts as $post_info)
echo '<h3>', $post_info['link'], '</h3>
<hr style="width: 65%; margin-left: 0;" />
<p>', $post_info['body'], '</p>';


Notice that I didn't use current(). For more info on outputting as an array. The second FAQ covers that. ssi_boardNews() is used as an example but the principle is the same.

[README] SSI FAQ's

Sorry for bringing up this old Post.

But my Question belongs to it:

How to limit the amount of post to show ?
How to limit for example, to the last 3 Posts in the specified topic ?

Where to place Statments like   "order by posting_time" "limit=3"
to get the 3 latest Post in this Topic ?

Hopefully that someone can help me with that ?
Thank You.

Advertisement: