Uutiset:

Want to get involved in developing SMF, then why not lend a hand on our github!

Main Menu
Advertisement:

Latest comments section

Aloittaja bobdole2281, heinäkuu 17, 2013, 03:30:57 AP

« edellinen - seuraava »

bobdole2281

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!

kat

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?


bobdole2281

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!

Kindred

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....
Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

bobdole2281

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?


margarett

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...
Se forem conduzir, não bebam. Se forem beber... CHAMEM-ME!!!! :D

LainaaOver 90% of all computer problems can be traced back to the interface between the keyboard and the chair

bobdole2281

$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

Kindred

Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

bobdole2281

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.

vbgamer45

Do
$arrayZ = ssi_fetchPosts(1309, false);
Community Suite for SMF - Grow your forum with SMF, Gallery,Store,Classifieds,Downloads,more!

SMFHacks.com - Paid Modifications for SMF

Mods:
EzPortal - Portal System for SMF
SMF Gallery Pro
SMF Store SMF Classifieds Ad Seller Pro

Kindred

ot this...
$arrayZ = ssi_fetchPosts(1309, false, 'block');
Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

bobdole2281

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?

margarett

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...
Se forem conduzir, não bebam. Se forem beber... CHAMEM-ME!!!! :D

LainaaOver 90% of all computer problems can be traced back to the interface between the keyboard and the chair

bobdole2281

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.

margarett

I'm sorry, later today I will try this and explain better...
Se forem conduzir, não bebam. Se forem beber... CHAMEM-ME!!!! :D

LainaaOver 90% of all computer problems can be traced back to the interface between the keyboard and the chair

margarett

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...
Se forem conduzir, não bebam. Se forem beber... CHAMEM-ME!!!! :D

LainaaOver 90% of all computer problems can be traced back to the interface between the keyboard and the chair

Kindred

Are you sure margaret?

You used 1 which is both post AND topic 1

does topic 1 have more than one post in it?
Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

margarett

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.
Se forem conduzir, não bebam. Se forem beber... CHAMEM-ME!!!! :D

LainaaOver 90% of all computer problems can be traced back to the interface between the keyboard and the chair

bobdole2281

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 :))

Advertisement: