TP-Docs
HTML5 Icon HTML5 Icon HTML5 Icon
TP on Social Media

Recent

Welcome to TinyPortal. Please login or sign up.

Members
  • Total Members: 3,963
  • Latest: BiZaJe
Stats
  • Total Posts: 195,917
  • Total Topics: 21,308
  • Online today: 629
  • Online ever: 8,223 (February 19, 2025, 04:35:35 AM)
Users Online
  • Users: 0
  • Guests: 466
  • Total: 466

Random post from specific topic

Started by morskisrle, November 11, 2007, 05:55:35 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

morskisrle

Do you know the code to pull just a random post from one specific topic.
Nothing fancy, want to use it for dispaying thoughts of great people witch are posted by the members. It would be some 300 or so characters long...
If it's not a lot of extra coding, would it be possible to display the author's name underneath it?


JPDeni

This is the basic code for what you want:


global $db_prefix, $scripturl;

$topic_number = 2; // fill in the number of the topic you want to pull your info from

$query = db_query(
      "SELECT *
       FROM {$db_prefix}messages
       WHERE `ID_TOPIC` = $topic_number
       ORDER BY RAND( )
       LIMIT 1", __FILE__, __LINE__);
$row = mysql_fetch_assoc($query);
$row['body'] = parse_bbc($row['body'], $row['smileysEnabled'], $row['ID_MSG']);

// set up your printout
// variables:
//   $row['ID_MEMBER']
//   $row['posterName']
//   $row['body']


I'm assuming that by "author's name" you mean the member's name that posted it. The original author's name would have to be within the post.

At this point, the code doesn't print anything out. It just sets up the variables so that you can format the results however you want. Also, be aware that it will display a different quote each time the page is refreshed.

morskisrle

Thanks...could you explain what you mean with setting up the variables. I really have no clue with any scripting, just can find my way most of the time.
What do i need to set up in your code to just pull random posts/reply from topic 2 for example.

JPDeni

I'll need you to give me an example of how you want your quote to be displayed. Invariably, when I guess at the format someone wants, I get it wrong and have to redo it.

morskisrle

OK..I made this little foto to show it. The box should have a fixed sixe of some 6 lines with possibility for scrolling if bigger.

You see the text (the murphy is just the part of the post itself) and underneath it the username witch is clickable(or not if it's to much work). On the right side of the username you see the "more" text witch would lead you to the end of the topic this post is being pulled from.

The text size should be just small as normally it is in the boxes (i also forgot to modufy it in the pic)
The whole thing (text, username, and more) should be centered (i forgot to modify it in the pic too )

Hope it's not to much coding...

JPDeni

I don't know how to do the "The box should have a fixed sixe of some 6 lines with possibility for scrolling if bigger" thing. I know it's a css function, but somehow I can never get it to work. Maybe someone else can tell you how to do that. I'm also not sure how to get to the bottom of a topic. I can get to the first post of a topic or a specific post in the middle of the topic, but I don't know about the end without a whole lot more coding. The code below will get you to the first post.

Use the code I posted above and then add this below it:

echo '<div style="text-align: center;" >' . $row['body'] . '<br />';
echo '<a href="' . $scripturl . '?action=profile;u=' . $row['ID_MEMBER'] . '">' . $row['posterName'] . '</a>  ';
echo '<a href="' . $scripturl . '?topic=' . $row['ID_TOPIC'] . '">More</a></div>';


If you want different colors, you can either set them in the .css file or inline within the a href statement, like


echo '<a style="color:red;" href="' . $scripturl . '?topic=' . $row['ID_TOPIC'] . '">More</a></div>';

morskisrle

#6
Wow thanks..this should definately go in the index of tips and tricks. It looks good. For the box size i added this in the div tag and works perfectly.
width: 150px; height: 200px; overflow: auto;

Ok to put all together for the ones that want the fixed box size with a scroll possibility if the text ecseeds the hight, and the colored link to the topic:

global $db_prefix, $scripturl;

$topic_number = 2; // fill in the number of the topic you want to pull your info from

$query = db_query(
      "SELECT *
       FROM {$db_prefix}messages
       WHERE `ID_TOPIC` = $topic_number
       ORDER BY RAND( )
       LIMIT 1", __FILE__, __LINE__);
$row = mysql_fetch_assoc($query);
$row['body'] = parse_bbc($row['body'], $row['smileysEnabled'], $row['ID_MSG']);

// set up your printout
// variables:
//   $row['ID_MEMBER']
//   $row['posterName']
//   $row['body']

echo '<div style="text-align: center; width: 150px; height: 200px; overflow: auto;" >' . $row['body'] . '<br />';
echo '<a href="' . $scripturl . '?action=profile;u=' . $row['ID_MEMBER'] . '">' . $row['posterName'] . '</a>  ';
echo '<a style="color:red;" href="' . $scripturl . '?topic=' . $row['ID_TOPIC'] . '">More</a></div>';


Thanks again man...  ;)




morskisrle

Oh...didn't know :D. There are not much women around coding so...

Thanks again, woman :P

JPDeni

 ;D You're welcome. Glad I could help.

Also glad you got the scroll thing to work. I used exactly the same code as you did and it didn't work for me. I think css just doesn't like me sometimes.

Anyway, I'm happy you have it the way you want it.

This website is proudly hosted on Crocweb Cloud Website Hosting.