How to get last 5 topics from database?

Started by VictoryAndGlory, July 22, 2016, 05:01:20 AM

Previous topic - Next topic

VictoryAndGlory

I wrote a code to fetch last five topics.
But it doesnt find any match.
What is wrong with my code?


$sonuc = mysql_query("select subject, id_msg, id_topic from smf_messages, smf_topics WHERE smf_topics.id_first_msg = smf_messages.id_msg ORDER BY id_topic DESC LIMIT 10");
if(mysql_num_rows($results)!=0)
{
    while($read = mysql_fetch_object($results))
    {
echo "<a href='../index.php?topic=" . $read->id_topic . "'>".$read->subject."</a><br>";
    }
}else{
    echo "There is no match!";
}
mysql_close($conn);



VictoryAndGlory

#1
*CODE UPDATED*
Still doesn't work :( When i tried to JOIN smf_topics into the query it shows no results match.

Also this code works to fetch last 10 messages. But i am still trying to fetch only topics.


$results = mysql_query("select subject, id_msg, id_topic from smf_messages ORDER BY id_topic DESC LIMIT 10");
if(mysql_num_rows($results)!=0)
{
    while($read = mysql_fetch_object($results))
    {
echo "<a href='../index.php?topic=" . $read->id_topic . "'>".$read->subject."</a><br>";
    }
}else{
    echo "There is no match!";
}
mysql_close($conn);




Grammy

Quote from: VictoryAndGlory on July 22, 2016, 05:56:22 AM
*CODE UPDATED*
Still doesn't work :( When i tried to JOIN smf_topics into the query it shows no results match.

Also this code works to fetch last 10 messages. But i am still trying to fetch only topics.


$results = mysql_query("select subject, id_msg, id_topic from smf_messages ORDER BY id_topic DESC LIMIT 10");
if(mysql_num_rows($results)!=0)
{
    while($read = mysql_fetch_object($results))
    {
echo "<a href='../index.php?topic=" . $read->id_topic . "'>".$read->subject."</a><br>";
    }
}else{
    echo "There is no match!";
}
mysql_close($conn);



Did you mean to mark your topic as Solved?   :)

Kindred

#3
why not


<?php
require_once('SSI.php');
ssi_recentTopics(5nullnull'echo'); 
?>



or...

<?php
global $scripturl$txt;
require_once(
'SSI.php');
$topic_list ssi_recentTopics(5nullnull'array');
foreach 
$topic_list as $post{
  echo 
'<a href="'$post['href'], '">'$post['subject'], '</a>
'
$txt['by'], ' '$post['poster']['link'], '
'
, !$post['is_new'] ? '' '<a href="' $scripturl '?topic=' $post['topic'] . '.msg' $post['new_from'] . ';topicseen#new" rel="nofollow"><img src="' $settings['lang_images_url'] . '/new.gif" alt="' $txt['new'] . '" /></a><br />';
}
 
?>

Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

Shambles

Quote from: Kindred
why not

or...

<?php
global $scripturl;
require_once(
'SSI.php');
$topic_list ssi_recentTopics(5nullnull'array');
foreach 
$topic_list as $topic {
  echo 
'<a href="'$post['href'], '">'$post['subject'], '</a>
'
$txt['by'], ' '$post['poster']['link'], '
'
, !$post['is_new'] ? '' '<a href="' $scripturl '?topic=' $post['topic'] . '.msg' $post['new_from'] . ';topicseen#new" rel="nofollow"><img src="' $settings['lang_images_url'] . '/new.gif" alt="' $txt['new'] . '" /></a><br />';
}
 
?>



Where will $post and $txt come from? I think you meant $topic and $somethingelse

Kindred

Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

VictoryAndGlory

Quote from: Kindred on July 23, 2016, 09:04:28 AM
why not


<?php
require_once('SSI.php');
ssi_recentTopics(5nullnull'echo'); 
?>



or...

<?php
global $scripturl$txt;
require_once(
'SSI.php');
$topic_list ssi_recentTopics(5nullnull'array');
foreach 
$topic_list as $post{
  echo 
'<a href="'$post['href'], '">'$post['subject'], '</a>
'
$txt['by'], ' '$post['poster']['link'], '
'
, !$post['is_new'] ? '' '<a href="' $scripturl '?topic=' $post['topic'] . '.msg' $post['new_from'] . ';topicseen#new" rel="nofollow"><img src="' $settings['lang_images_url'] . '/new.gif" alt="' $txt['new'] . '" /></a><br />';
}
 
?>


Because i dont want to use SSI. I dont know how to create custom SSI(like members avatars with last 5 topics) and it is the same thing with my question. In the basic SSI using SQL too. Thats why SSI doesnt answering my question. Also i wanna learn SQL and solving problems like that with it, is the best way to learn something.

Also topic is solved with trial and error way.

Kindred

So, in other words, you want to reinvent the wheel and do it in an insecure manner?
Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

VictoryAndGlory

Quote from: Kindred on July 24, 2016, 09:16:07 PM
So, in other words, you want to reinvent the wheel and do it in an insecure manner?

Why do you think it is insecure?
I did it and it is pretty secure i think.

Kindred

nope... it is distinctly insecure. You are making direct database calls without cleaning the input.  very hazardous.
Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

Advertisement: