Is it possible to add a random article in a frontpage block?
anyone has an idea?
bumb :)
bump
Probably. Most things are possible.
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...
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.
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?
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?
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?
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!! :)
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.
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']);
Thank you. That displayed the articles at least but all articles were displayed on the front page one after the other
Post your code.
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'];
Your code is correct and will not show all of your articles. It's something else that's doing that -- some other setting.
What kind of block this code goes to? ssi block?
PHP Front Block
It goes in a php block.
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>';
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>';
Not sure what you're talking about. Care to post a link so we can see what is happening?
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
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/>';
Nope, not working...
Any hint how to solve this issue?
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.
Doing more research....
Try
echo html_entity_decode(substr($row['body'],0,300)) ,'....', '<br/>';
Wonderful - this worked. Thank you very much and especially because this was a php question and not a TP question :)