TinyPortal

Development => Block Codes => Topic started by: sanax on February 02, 2007, 08:52:11 AM

Title: Random article in Frontpage block
Post by: sanax on February 02, 2007, 08:52:11 AM
Is it possible to add a random article in a frontpage block?
Title: Re: Random article in Frontpage block
Post by: sanax on February 04, 2007, 03:05:04 AM
anyone has an idea?
Title: Re: Random article in Frontpage block
Post by: sanax on February 06, 2007, 03:05:50 PM
bumb :)
Title: Re: Random article in Frontpage block
Post by: sanax on February 09, 2007, 04:39:00 PM
bump
Title: Re: Random article in Frontpage block
Post by: JPDeni on February 09, 2007, 04:46:45 PM
Probably. Most things are possible.
Title: Re: Random article in Frontpage block
Post by: sanax on February 09, 2007, 06:11:11 PM
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...
Title: Re: Random article in Frontpage block
Post by: JPDeni on February 09, 2007, 06:35:14 PM
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.
Title: Re: Random article in Frontpage block
Post by: sanax on February 09, 2007, 07:32:26 PM
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?
Title: Re: Random article in Frontpage block
Post by: sanax on February 09, 2007, 07:36:16 PM
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?
Title: Re: Random article in Frontpage block
Post by: JPDeni on February 09, 2007, 08:18:11 PM
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?
Title: Re: Random article in Frontpage block
Post by: sanax on February 09, 2007, 08:36:49 PM
Thanks Deni, works great

Remote images by using an img URL... although I see images located on my server do show up in the intro. I'll check it out!

Thanks for the help!! :)
Title: Re: Random article in Frontpage block
Post by: Rafferty on October 13, 2007, 11:53:54 AM
Tried this and seems to only display the heading of the article with a link. What if the full article is wanting to be displayed as the feature article does.
Title: Re: Random article in Frontpage block
Post by: JPDeni on October 13, 2007, 12:40:16 PM
Quote
What if the full article is wanting to be displayed

Then you display the full article. Use the code above, but change


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


to


echo html_entity_decode($row['body']);
Title: Re: Random article in Frontpage block
Post by: Rafferty on October 13, 2007, 03:11:30 PM
Thank you. That displayed the articles at least but all articles were displayed on the front page one after the other
Title: Re: Random article in Frontpage block
Post by: JPDeni on October 13, 2007, 08:42:16 PM
Post your code.
Title: Re: Random article in Frontpage block
Post by: Rafferty on October 14, 2007, 01:14:34 AM
Quoteglobal $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'] , '
';
echo $row['body'];
Title: Re: Random article in Frontpage block
Post by: JPDeni on October 14, 2007, 02:19:45 AM
Your code is correct and will not show all of your articles. It's something else that's doing that -- some other setting.
Title: Re: Random article in Frontpage block
Post by: Mick on February 23, 2008, 02:17:02 PM
What kind of block this code goes to?   ssi block?
Title: Re: Random article in Frontpage block
Post by: Rafferty on February 23, 2008, 02:57:52 PM
PHP Front Block
Title: Re: Random article in Frontpage block
Post by: JPDeni on February 23, 2008, 04:45:17 PM
It goes in a php block.
Title: Re: Random article in Frontpage block
Post by: RogerLewin on January 03, 2009, 12:26:38 PM
How do i show only the first 30 characters of the body?

I tried this


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 '<b>' , $row['subject'] , '</b><br />';
echo substr($row['body'],1,30);
echo '<a href="' , $scripturl , '?page=' , $row['id'] , '">Den vollen Artikel lesen...</a>';
Title: Re: Random article in Frontpage block
Post by: RogerLewin on January 03, 2009, 12:58:45 PM
OK, i got it, but the following code shows also the html tags. How do i get the text shown after being interpreted?


global $db_prefix, $scripturl;

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

$row = mysql_fetch_assoc($query);

echo '<b>' , $row['subject'] , '</b><br />';
echo substr($row['body'],1,300) ,'....', '<br/>';
echo '<a href="' , $scripturl , '?page=' , $row['id'] , '">Alles lesen...</a>';
Title: Re: Random article in Frontpage block
Post by: IchBin on January 04, 2009, 07:48:34 PM
Not sure what you're talking about. Care to post a link so we can see what is happening?
Title: Re: Random article in Frontpage block
Post by: RogerLewin on January 05, 2009, 11:36:17 AM
Yes, of course. The Box 'Wissen' is showing the html tags also, not the text after being interpreted by the webserver

http://portal.dasaugederhorde.de/ in the upper left corner
Title: Re: Random article in Frontpage block
Post by: JPDeni on January 05, 2009, 12:20:57 PM
This code


echo substr($row['body'],1,300) ,'....', '<br/>';


is cutting off the first character and probably causing the problem. Try


echo substr($row['body'],0,300) ,'....', '<br/>';
Title: Re: Random article in Frontpage block
Post by: RogerLewin on January 05, 2009, 12:24:56 PM
Nope, not working...
Title: Re: Random article in Frontpage block
Post by: RogerLewin on January 09, 2009, 07:15:26 PM
Any hint how to solve this issue?
Title: Re: Random article in Frontpage block
Post by: JPDeni on January 09, 2009, 07:39:03 PM
I'm pretty sure, now that I think more about it, that it has to do with the fact that you have open html tags, but you're cutting off the text before the closing tags are printed. I know that you don't want it this way all the time, but for testing purposes, try printing out the whole article. Use


echo $row['body'] , '<br/>';


instead of


echo substr($row['body'],0,300) ,'....', '<br/>';


The result of that will give us a clue as to what's going on.
Title: Re: Random article in Frontpage block
Post by: JPDeni on January 09, 2009, 08:52:45 PM
Doing more research....

Try


echo html_entity_decode(substr($row['body'],0,300)) ,'....', '<br/>';
Title: Re: Random article in Frontpage block
Post by: RogerLewin on January 11, 2009, 02:36:10 PM
Wonderful - this worked. Thank you very much and especially because this was a php question and not a TP question :)