Simple Machines Community Forum

Customizing SMF => SMF Coding Discussion => Aiheen aloitti: bobdole2281 - heinäkuu 17, 2013, 03:30:57 AP

Otsikko: Latest comments section
Kirjoitti: bobdole2281 - heinäkuu 17, 2013, 03:30:57 AP
I'm using a custom html page with a couple smf interactions on it. I'm looking for a way to see the latest comment of a specific thread. Is that possible? Can anyone point me in the right direction.


Let's say for example, The thread I want is http://marveldcforum.com/index.php?topic=980.0. Is there any way to show the last comment on it? Thanks!
Otsikko: Re: Latest comments section
Kirjoitti: kat - heinäkuu 17, 2013, 05:59:41 AP
Not the answer you're looking for... But, something that might point you in the right direction?

Simple Portal has a preconfigured block, for doing exactly what you want to do. I think it's called "Board News".

If you get that mod, maybe you could "borrow" the code, for that, from there?
Otsikko: Re: Latest comments section
Kirjoitti: kat - heinäkuu 17, 2013, 06:03:06 AP
Ha! I just saw this:

http://www.simplemachines.org/community/index.php?topic=507513.msg3574955#msg3574955

Is that anything like what you're trying to do?
Otsikko: Re: Latest comments section
Kirjoitti: bobdole2281 - heinäkuu 17, 2013, 02:36:00 IP
I read through the linked thread. It was similar, but they actually didn't offer any help. He basically only said that his friend "fixed" it, and someone said not to use iframes.

I looked through my simple portal and that actually won't work either. I need comments from a specific thread, not a group's top thread (if that makes any sense). I basically just need the body of the comment somehow, I'd like to do the formatting myself. Thanks for your help so far though!
Otsikko: Re: Latest comments section
Kirjoitti: Kindred - heinäkuu 18, 2013, 02:16:42 IP
I believe you would be looking for this

// Fetch a post with a particular ID. By default will only show if you have permission to the see the board in question - this can be overriden.
function ssi_fetchPosts($post_ids = array(), $override_permissions = false, $output_method = 'echo')


call the function into and array and output_method='block' (or 'array')

then display out only the last item of the array....
Otsikko: Re: Latest comments section
Kirjoitti: bobdole2281 - elokuu 06, 2013, 04:36:46 IP
I have been messing with this for days.

$arrayZ = ssi_fetchPosts($post_ids = array(), $override_permissions = false, $output_method = 'echo');
   
print_r($arrayZ);


This is just displaying blank. I know it's wrong because there must be somewhere I need to actually put the thread ID. Also, do I need to initialize the array before hand or will it just know that the variable $arrayZ is an array?

Otsikko: Re: Latest comments section
Kirjoitti: margarett - elokuu 06, 2013, 04:41:41 IP
For that to work, you need to change 2 things:
- you need to pass the id of the topic in the first argument
- you need to change the output type from echo

I'm not in the computer now but I can try to check this later...
Otsikko: Re: Latest comments section
Kirjoitti: bobdole2281 - elokuu 06, 2013, 04:57:15 IP
$arrayZ = ssi_fetchPosts(1309.0, false, 'block');
   

while (list(, $value) = each($arrayZ))
{
   echo "Value: $value<br />\n";
}


I'm not sure if I placed the ID correctly. I'm also not sure if 'block' is supposed to have quotes around it. Either way here is what displays:

LainaaValue: Array
Otsikko: Re: Latest comments section
Kirjoitti: Kindred - elokuu 06, 2013, 05:09:04 IP
1309... not 1309.0
Otsikko: Re: Latest comments section
Kirjoitti: bobdole2281 - elokuu 06, 2013, 05:26:07 IP
Thank you, that fixes the ID part. I can't figure out why my loop isn't printing correctly though.

I tested the loop code. That part is fine. But for some reason the value
$arrayZ = ssi_fetchPosts(1309, false, block);

Is just being saved as
LainaaArray

I need it to print out the actual reply.
Otsikko: Re: Latest comments section
Kirjoitti: vbgamer45 - elokuu 06, 2013, 05:38:10 IP
Do
$arrayZ = ssi_fetchPosts(1309, false);
Otsikko: Re: Latest comments section
Kirjoitti: Kindred - elokuu 06, 2013, 05:50:32 IP
ot this...
$arrayZ = ssi_fetchPosts(1309, false, 'block');
Otsikko: Re: Latest comments section
Kirjoitti: bobdole2281 - elokuu 06, 2013, 06:31:34 IP
Alright, that fixed a few things.

I cannot for the life of me figure out how to loop this array. I keep getting this error.

Warning: Invalid argument supplied for foreach() in...

I know it should be simple code but it isn't working.

The first part is this:

$arrayZ = ssi_fetchPosts(1241, false);

//Some type of foreach loop


Now how do I grab the last comment?
Otsikko: Re: Latest comments section
Kirjoitti: margarett - elokuu 06, 2013, 06:54:27 IP
Isn't the foreach error inside the function, instead of yout code after? Because it requires an array as first argument and we told you to just give the topic id...

If it is in your code, you need to first print_r the result and then build the correct foreach. I didn't test that yet (bed time) but I assume the resulting array contains some structures...
Otsikko: Re: Latest comments section
Kirjoitti: bobdole2281 - elokuu 06, 2013, 07:14:05 IP
Lainaus käyttäjältä: margarett - elokuu 06, 2013, 06:54:27 IP
Isn't the foreach error inside the function, instead of yout code after? Because it requires an array as first argument and we told you to just give the topic id...

If it is in your code, you need to first print_r the result and then build the correct foreach. I didn't test that yet (bed time) but I assume the resulting array contains some structures...

I'm not sure what you mean by this.
Otsikko: Re: Latest comments section
Kirjoitti: margarett - elokuu 07, 2013, 02:15:28 AP
I'm sorry, later today I will try this and explain better...
Otsikko: Re: Latest comments section
Kirjoitti: margarett - elokuu 07, 2013, 06:04:46 IP
OK, now I see the behaviour. And I'm sorry, it will not serve you...

Here is my "test.php"

<?php

// Include the SSI file.
require(dirname(__FILE__) . '/SSI.php');

$Zarray=ssi_fetchPosts(1,false,'array');
echo 
'<pre>';
print_r($Zarray);
echo 
'</pre>';

?>


(didn't even care about html/body tags :P )

And here is the result:

Array
(
    [0] => Array
        (
            [id] => 1
            [board] => Array
                (
                    [id] => 1
                    [name] => General Discussion
                    [href] => http://localhost/smf2/index.php?board=1.0
                    [link] => General Discussion
                )

            [topic] => 1
            [poster] => Array
                (
                    [id] => 0
                    [name] => Simple Machines
                    [href] =>
                    [link] => Simple Machines
                )

            [subject] => Welcome to SMF!
            [short_subject] => Welcome to SMF!
            [preview] => Welcome to Simple Machines Forum!We hope you enjoy using your forum.  If you have any problems, please feel free to ask us for a...
            [body] => Welcome to Simple Machines Forum!

We hope you enjoy using your forum.  If you have any problems, please feel free to ask us for assistance.

Thanks!
Simple Machines
            [time] => July 30, 2013, 11:38:46 pm
            [timestamp] => 1375223926
            [href] => http://localhost/smf2/index.php?topic=1.msg1;topicseen#new
            [link] => Welcome to SMF!
            [new] => 1
            [is_new] =>
            [new_from] => 4
        )

)

As you can see, it fetch you a lot of details from the post, but not what you need as it will NOT fetch the topic replies (therefore, not the lastest one...)

This is because the function actually expects the ID of a POST, not a topic. If you know the ID of the last post (which you probably don't, as it's dynamic...) then you can use it. If not, then I think you need an "extra" function.
It's basically just knowing the IDs of the posts related to a topic and dump them into the array that this function needs...
Otsikko: Re: Latest comments section
Kirjoitti: Kindred - elokuu 07, 2013, 07:14:39 IP
Are you sure margaret?

You used 1 which is both post AND topic 1

does topic 1 have more than one post in it?
Otsikko: Re: Latest comments section
Kirjoitti: margarett - elokuu 07, 2013, 07:24:11 IP
I am positive, Kindred.

I was playing with it for a while... That function expects POST id (or an array of them) and shows all the datas I put above. Of course, if you can have yourself that array of posts, then you can get your "topic", full.
If you only have 1 post ("Welcome to Simplemachines") then your topic is complete. But if you reply to this post, that function will not fetch the reply...

Since I got myself some time before I go to bed, I put up a solution according to my very basic skills (trial and error, basically :P )
WARNING: this needs a new function to be created in SSI.php, and I know this is something most peopled don't like a lot...

So, in SSI.php, add the following function somewhere (I added it before ssi_fetchPosts)

//Get all IDs of post related to a certain topic
function ssi_getPostIDfromTopic($topic)
{
global $settings, $smcFunc, $db_prefix;
$retcode = array();
if (empty($topic))
return $retcode;

$request = $smcFunc['db_query']('', '
SELECT id_msg
FROM {db_prefix}messages
WHERE id_topic = '.$topic.'
ORDER BY id_msg '
);

$temp = array();
    // fill the array
    while ($temp2 = $smcFunc['db_fetch_assoc']($request))
        $temp[] = $temp2;

$smcFunc['db_free_result']($request);

if (!empty($temp))
$retcode = $temp;
return $retcode;
}

This will return just an array of IDs (post IDs related to a topic ID, passed to the function)

Then this "test.php"

<?php

// Include the SSI file.
require(dirname(__FILE__) . '/SSI.php');

$Zarray=ssi_getPostIDfromTopic(1);

if (!empty(
$Zarray))
{
$num_records=count($Zarray);
//echo $num_records.'<br>';
$last_id $Zarray[$num_records-1]['id_msg'];
//echo $last_id.'<br>';
$postinfo ssi_fetchPosts($last_id,false,'array');
$postbody $postinfo['0']['body'];
//echo $postbody.'<br>';
}
?>


With this, you have the body of the LAST post of a certain topic. This is, as far as I understand, the goal of the user.
From here, it's also quite easy to get the full topic data, as we can build an array of this IDs and pass it to the function ssi_fetchPosts, thus gathering all the information I put above from ALL post is that topic.
Otsikko: Re: Latest comments section
Kirjoitti: bobdole2281 - elokuu 08, 2013, 01:46:28 AP
Man, you are a genius. This worked perfectly! I was a little worried when you said to change SSI, but it worked great. Is there a variable I can call to see what user posted the last comment?

Like this...

Latest reply by ______
...

(The ... works great! Thank you for all your help :))
Otsikko: Re: Latest comments section
Kirjoitti: margarett - elokuu 08, 2013, 02:22:27 AP
Yes, there is.

Try to print_r($postinfo); You will see information like the one I posted above.

So, a line like
$postauthor = $postinfo[0]['poster']['name'];
Should do the trick ;)