Jump to content

[SOLVED] PHP / MSQ assistance


pznmynd

Recommended Posts

Greetings,

 

I have a news column on a site were i pull the news from a database, Currently I am using a loop to get all the news and display it, for a loop im using a while, What are my options to display only the top 15 news posts (in Descending order), Take a look at my snippet and see if you can help, Thanks in advance,

 

    <?php 
  // load the configuration file.
include("config.php");
        //load all news from the database and then OREDER them by newsid
        $result = mysql_query("SELECT * FROM news ORDER BY newsid DESC",$connect);
        //lets make a loop and get all news from the database
        while($myrow = mysql_fetch_array($result))
             {//begin of loop
               //now print the results:
               echo "<b> ";
                echo "<br><a href=\"read_more.php?newsid=$myrow[newsid]\">";
		   echo $myrow['title'];
		   echo "</a></b>";
               echo "<br><hr>";
               echo "<i>";
               echo $myrow['dtime'];
               echo "</i><hr align=left width=160>";
               echo $myrow['text1'];
		   echo "</hr> ";
              
             }//end of loop
             ?>

 

/scott

Link to comment
https://forums.phpfreaks.com/topic/140509-solved-php-msq-assistance/
Share on other sites

You can add a limit clause to your query but then you are only ever going to get that many results. With pagination you can have 100 results and limit it to 10 per page with 10 pages.

 

Limit clause example;

 

$result = mysql_query("SELECT * FROM news ORDER BY newsid DESC LIMIT 15",$connect);

You might want to look into the LIMIT clause of mysql because you can also set it up so that if the user wants to view the next 15, you could change the query to something like this:

 

$result = mysql_query("SELECT * FROM news ORDER BY newsid DESC LIMIT 16, 30",$connect);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.