Uutiset:

Bored?  Looking to kill some time?  Want to chat with other SMF users?  Join us in IRC chat or Discord

Main Menu
Advertisement:

Fetch body of post?

Aloittaja cddude, kesäkuu 02, 2014, 10:03:37 AP

« edellinen - seuraava »

cddude

Hey all.

I am making my own little plugin, but I need some help.

I would like to get the body of the first post from a given topic_ID. I have looked in the functions database, but I don't know which one to use.
My guess is that I would first need to somehow find which post_ID is the first one to link the given topic_ID? and once I have the post_ID I'll call the appropriate fetch_function to get the body?

Does someone know how to do this? All I need is a direction of which functions I need to use - I'll hadle it from there on.

Thanks!

margarett

With the topic ID you can get the ID of the first message from "smf_topics" table. Then with the first message ID you can fetch the body from "smf_messages" table.
This can be done in single query.
In order to correctly display it, you need to process it before printing it, pretty much like SMF does in Display.php

// Do the censor thang.
censorText($message['body']);

// Run BBC interpreter on the message.
$message['body'] = parse_bbc($message['body'], $message['smileys_enabled'], $message['id_msg']);
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

cddude

Thanks margarett!

I managed to pull the messages that I wanted. My issue now is with the parsing.
The code you included seems to work with the array $message[] - from my db query I only have a String variable ($body) containing the contents from the Body column in the database and the related id_msg integer.

So this is what I tried:

// Do the censor thang.
censorText($body);

// Run BBC interpreter on the message.
$body = parse_bbc($body, 0, $id_msg);

echo $body;


From some minor past experience with SMF modding I also included SSI.php and Subs.php, which I believe I must include if this is going to work.
Nothing gets parsed however - it remains the same way as seen in the db.

Suggestions? Thanks.

Arantor

You need to include SSI.php, that will include Subs.php for you.

Please provide the full code you are currently using.
Holder of controversial views, all of which my own.


cddude

#4
Here is the code as requested:

include('/SSI.php');

$con = mysqli_connect("$host","$user","$pass","$name");
$query = "SELECT title,description,img,topic_id FROM blog_slider ORDER BY id DESC LIMIT 3";
$post = "SELECT id_msg, body FROM smf_topics INNER JOIN smf_messages WHERE id_first_msg = id_msg";
$result = mysqli_query($con, $query);
$res = mysqli_query($con, $post);

while($row = mysqli_fetch_array($result)) {
$row2 = mysqli_fetch_array($res);
echo' <div class="newsdiv">
<div class="picdiv" style="background-image: url(\'' . $row["img"] . '\');"></div>
<div class="titlediv"><a href="http://site.com/index.php?topic=' . $row["topic_id"] . '"><font size="4pt">' . $row["title"] . '</font></a></div>';
$message = $row2["body"];
censorText($message);
$message = parse_bbc($message, 0, $row2['id_msg']);

echo '<div class="textdiv"><font size="2pt">' . $row2["body"] . '</font></div>

</div>';


Cheers.

Illori

is your SSI.php in the Sources folder? by default it is not.

cddude

Lainaus käyttäjältä: Illori - kesäkuu 02, 2014, 01:06:34 IP
is your SSI.php in the Sources folder? by default it is not.
Oops! Fixed that but still doesn't work. :/

Arantor

Ugh.

SSI.php isn't in the sources file, for the start. No wonder it's not being parsed, it's not being loaded.

Let's quickly rewrite that to 1) use SMF's bootstrapping, 2) use SMF's database functionality,3) not use fetch_array which should never be used anyway, and 4) not create so many more queries than necessary in the first place. You do not need to run a query for every single post you want, especially when it is an insanely inefficient query. (Joining messages to topics without any kind of qualifier means joining the *entire table* multiple times, as you're querying it multiple times)


<?php
require_once('SSI.php'); // It is not in the Sources folder, but the top folder where this file seems to be

global $db_name;
$smcFunc['db_select_db']($db_name);

$request $smcFunc['db_query']('''
SELECT bs.title, bs.description, bs.img, bs.topic_id, m.id_msg, m.body
FROM blog_slider AS bs
INNER JOIN {db_prefix}topics AS t ON (bs.topic_id = t.id_topic)
INNER JOIN {db_prefix}messages AS m ON (t.id_first_msg = m.id_msg)
ORDER BY bs.id DESC LIMIT 3'
,
array()
);

while (
$row $smcFunc['db_fetch_assoc']($request))
{
censorText($row['body']);

echo ' <div class="newsdiv">
<div class="picdiv" style="background-image: url(\''
$row['img'], '\');"></div>
<div class="titlediv"><a href="'
$scripturl'?topic='$row['topic_id'], '"><span style="font-size: 4pt">'$row['title'], '</span></a></div>
<div class="textdiv"><font size="2pt">'
parse_bbc($row['body'], false$row['id_msg']), '</font></div>';
}
$smcFunc['db_free_result']($request);
?>

Holder of controversial views, all of which my own.


cddude

#8
Thanks Arantor,

yeah I noticed the wrong path for the SSI earlier. Also, thanks for the code.

I get this error though:
Fatal error: Function name must be a string in /home/[...]/Sources/PortalBlocks.php(3561) : eval()'d code on line 5

Perhaps I need to do something to your code before I upload this, but I cannot see anything. All variables and functions seem to be global.
And yes, this is a custom php-block that I am coding for SimplePortal.

Thanks again!

Arantor

I really do love how people forget fairly crucial details. Or misspell my username :P


<?php
require_once('SSI.php'); // It is not in the Sources folder, but the top folder where this file seems to be

global $smcFunc;

$request $smcFunc['db_query']('''
SELECT bs.title, bs.description, bs.img, bs.topic_id, m.id_msg, m.body
FROM blog_slider AS bs
INNER JOIN {db_prefix}topics AS t ON (bs.topic_id = t.id_topic)
INNER JOIN {db_prefix}messages AS m ON (t.id_first_msg = m.id_msg)
ORDER BY bs.id DESC LIMIT 3'
,
array()
);

while (
$row $smcFunc['db_fetch_assoc']($request))
{
censorText($row['body']);

echo ' <div class="newsdiv">
<div class="picdiv" style="background-image: url(\''
$row['img'], '\');"></div>
<div class="titlediv"><a href="'
$scripturl'?topic='$row['topic_id'], '"><span style="font-size: 4pt">'$row['title'], '</span></a></div>
<div class="textdiv"><font size="2pt">'
parse_bbc($row['body'], false$row['id_msg']), '</font></div>';
}
$smcFunc['db_free_result']($request);
?>
Holder of controversial views, all of which my own.


margarett

Shouldn't a PHP block inside SP have already available all SMF's content and variables, thus not needing SSI.php?
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

Arantor

In theory, yes. But in practice... not so much. Of course, having a require_once will help with that ;)
Holder of controversial views, all of which my own.


cddude

Alright it works. Thanks everyone!

Advertisement: