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

Recent

Welcome to TinyPortal. Please login or sign up.

Members
Stats
  • Total Posts: 196,004
  • Total Topics: 21,330
  • Online today: 468
  • Online ever: 8,223 (February 19, 2025, 04:35:35 AM)
Users Online
  • Users: 0
  • Guests: 430
  • Total: 430

Random article in Frontpage block

Started by sanax, February 02, 2007, 08:52:11 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

sanax

Is it possible to add a random article in a frontpage block?

sanax




JPDeni


sanax

I figure this out:

Quote// Make a MySQL Connection
$query = "SELECT * FROM smf2_tp_articles ORDER BY rand() DESC LIMIT 1";
   
$result = mysql_query($query) or die(mysql_error());


while($row = mysql_fetch_array($result))
{
   echo $row['subject']. " - ". $row['intro'];
   echo "
";
}

This shows a random subject with the intro but I can't figure out how to link it to the actual article!... and how to show the images in the intro because I have images in some intro's...

JPDeni

First, this doesn't have anything to do with themes, so I'll move the topic to block code snippets after I post. That could very well be why you didn't get a response for several days. I don't look at theme stuff very often because I don't know much about theme stuff.

You just want a link to the random article?


global $db_prefix, $scripturl;

$query = db_query(
    "SELECT id, intro, subject
     FROM {$db_prefix}tp_articles
     ORDER BY rand()
     LIMIT 1", __FILE__, __LINE__);

$row = mysql_fetch_assoc($query);

echo $row['subject'] , ' - ' , $row['intro'] , '<br />';
echo '<a href="' , $scripturl , '?page=' , $row['id'] , '">Read full article</a>';


This uses the built-in functions that SMF uses to access the database. Might as well make use of their code. :)

I'm not sure about the image. There should be an


<img src


html tag for the images, right? If so, they should just show up. If not, I don't know what to tell you. I don't have any intros to work with to test it out.

sanax

Thank you for moving it Deni... your code works great! I mostly use remote images in the intro. Don't know how It'll show up?

sanax

Any idea how I can show the random article up like this:

QuoteAnti-crime campaign: No word from Mbeki

The Presidency could not say on Thursday whether thousands of e-mails detailing South Africans' experiences of crime had reached President Thabo Mbeki. "I've not seen any of those letters. I'm not even sure whether they've arrived," said spokesperson for the Presidency Mukoni...

..instead of this:

QuoteAnti-crime campaign: No word from Mbeki - The Presidency could not say on Thursday whether thousands of e-mails detailing South Africans' experiences of crime had reached President Thabo Mbeki. "I've not seen any of those letters. I'm not even sure whether they've arrived," said spokesperson for the Presidency Mukoni...

To make the heading bold and seperate the intro from the subject?

JPDeni

It's easy enough to format the text. I just used the same formatting that you already had.

Instead of


echo $row['subject'] , ' - ' , $row['intro'] , '<br />';


use


echo '<b>' , $row['subject'] , '</b><br /><br />';
echo $row['intro'] , '<br />';


QuoteI mostly use remote images in the intro.

I don't know what that means. Could you post an example, using a [ code ] ... [ /code ] around the text so I can see the html?

This website is proudly hosted on Crocweb Cloud Website Hosting.