Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jun 2005
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Limiting number of entries displayed

    OK, I admitally have very little knowledge in the PHP development area.

    I am trying to implement a script for a web site that I am using to enter news headlines on the front page... but I want to limit it 3 news items on the front page... but I can't see in the script where to limit this.

    Can someone look at it and let me know if you see how it may possible (if it is possible ?!?!).

    Thanks so much in advance for your help.

    Here's the script:

    <?php


    // first, lets get a count of how many news items are in the database
    $news_count = "SELECT count(*) as count FROM news";
    $result = mysql_query($news_count);
    $row=mysql_fetch_array($result);
    $num_news=$row['count'];

    // if there aren't any news items, then lets generate a user-friendly notice that there aren't any
    if ($num_news <1){
    print("<p class=\"warn\">There Are Currently No News Entries In The Database</p>\n");
    }

    // if there are any news items, lets go forward with the display
    else{

    // the addLinks function will place links within the news content summary
    function addLinks($summary) {
    $summary = preg_replace("/(?<!quot;|[=\"]|:\/\/)\b((\w+:\/\/|www\.).+?)(?=\W*([<>\s]|$))/i", "<a href=\"$1\">$1</a>", $summary);
    return preg_replace("/href=\"www/i", "href=\"http://www", $summary);
    }

    $action=$_REQUEST['action'];
    $nid=$_REQUEST['nid'];

    // define the query
    // if the $action variable is set, that means we're displaying a full news item and we should select the specific news entry
    if(isset($action)){
    $query = "SELECT * FROM news WHERE rec_id=$nid";
    }
    // if the $action variable isn't set, then we're displaying all of the items in summary format
    else{
    $query = "SELECT * FROM news ORDER BY rec_id DESC";
    }

    // get the results
    $result = mysql_query($query);

    // define the variables for display and start the loop
    while ( $row = mysql_fetch_array($result)){
    $rec_id = $row["rec_id"];
    $title = $row["title"];
    $subject = $row["subject"];
    $summary = $row["summary"];
    $full_text = $row["full_text"];
    $date_added = $row["date_added"];






    // shorten the ID
    $id=$rec_id;

    // these two lines convert the date from the lame-o mySQL format of 0000-00-00 and convert it
    // it will now display in an American style month-name, date, and year format such as January 10, 2003
    list($year, $month, $day)= explode("-", $date_added);
    $american_date=date("l, F d, Y", mktime(0,0,0,$month,$day,$year));

    // start the display
    // I have doubts as to whether this is actually tabular data or whether it is best placed in a DIV
    // but for now, a table it is
    print("<div class=\"news_result\">\n");
    print("<h2>$title</h2>\n");

    // if the $action variable is not set, display the summary
    if(!isset($action)){

    if ((strlen($full_text)) > 100){
    print("<div><strong>Summary:</strong> $summary</div>");
    }
    else{
    print("<div>$summary</div>\n");
    }
    }



    // if the $action variable *IS* set, display the full text
    else{
    print("<p>$full_text</p>\n");
    }

    if(!isset($action)){
    if ((strlen($full_text)) > 45){
    print("<div><a href=\"news.php?action=full&amp;nid=$id\">&gt;&gt;Full Article</a></div>\n");
    }
    }
    else{
    print("<div><a href=\"news.php\">Back To News Main</a></div>\n");
    }

    print("</div>\n");

    }
    }
    ?>

  • #2
    Regular Coder
    Join Date
    May 2005
    Posts
    563
    Thanks
    0
    Thanked 3 Times in 3 Posts
    I think you need to change this line

    PHP Code:
    $query "SELECT * FROM news ORDER BY rec_id DESC"
    to

    PHP Code:
    $query "SELECT * FROM news ORDER BY rec_id DESC LIMIT 3"

  • #3
    New Coder
    Join Date
    Jun 2005
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Right on!

    That's exactly what I needed to do.

    Thanks so much for the help.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •