SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    imagine no limitations exbabylon's Avatar
    Join Date
    Dec 2000
    Location
    Idaho, USA
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm really having a hard time trying to do this... so I've decided to see if you guys here can help me out.

    I have a database of tips, and I want to only display the last five tips entered into the database. I want these first five to be displayed with their text. But I want the rest of the tips to be "archived" at the bottom of the page, and only their title's showing.

    I have a table named tips, and these colums:

    ID
    Title
    Content
    Author
    Date

    The ID is an auto-incrament. The I have a online form to add tips. Each tip requires an input of a title, content, and an author. The data is auto.

    So there you have it. I have played with the mysql_insert_id() taking away five etc. But I have had no luck. I have been trying to use the while statement, with no luck so far. Could anyone please shed some light?

    God Bless,

    Alex
    Blamestorming: Sitting around in a group discussing why a deadline was missed or a project failed and who was responsible.

    Exbabylon- Professional Internet Services

  2. #2
    Serial Publisher silver trophy aspen's Avatar
    Join Date
    Aug 1999
    Location
    East Lansing, MI USA
    Posts
    12,937
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Without telling you code I'd tell you how I'd do it.

    first know this if you don't. The most recent records added to a DB will be the first returned IF there is no overriding ORDER BY Statement.

    The first thing that pops into my mind is limits. With a limit you can limit the number of results returned. For info on limits refer to Kevin's tutorial under advanced sql.

    However if you want at the bottom of the page to also include remaining tips, but without their text, then you'd need a second query to acheive that.

    you should try to keep down the number of queries on a page so lets choose a different approach.

    What I would do is simply use a while loop, like you said. Create a counter and while it is <= 5 display records a certain way, then once that loop has run its course start displaying them the second way.

    Chris Beasley - I publish content and ecommerce sites.
    Featured Article: Free Comprehensive SEO Guide
    My Guide to Building a Successful Website
    My Blog|My Webmaster Forums

  3. #3
    SitePoint Evangelist
    Join Date
    May 2000
    Location
    Canada
    Posts
    533
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    something like this ?

    Code:
    $numToShow = 5;
    
    $i = 1;
    
    $query = "SELECT * FROM tips ORDER BY ID DESC";
    $result = mysql_query($query);
    
    while($row = mysql_fetch_array($result))
    {
    	$title = $row['Title'];
    	$content = $row['Content'];
    	if($i > $numToShow)
    	{
    		echo $title;
    	}
    	else
    	{
    		echo $title,"<br>",$content;
    		$i++;
    	}
    
    }
    Happy Coding!
    cogito, ergo sum

  4. #4
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Or, since you have a Date column by which to identify the newest tips,

    Code:
    $query = "SELECT * FROM tips ORDER BY Date DESC";
    $result = mysql_query($query)
      or die ("Database error. Please try again later.");
    
    while($row = mysql_fetch_array($result))
    {
    	$title = $row['Title'];
    	$content = $row['Content'];
    	if($i > $numToShow)
    	{
    		echo $title;
    	}
    	else
    	{
    		echo $title,"<br>",$content;
    		$i++;
    	}
    
    }
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference

  5. #5
    imagine no limitations exbabylon's Avatar
    Join Date
    Dec 2000
    Location
    Idaho, USA
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you both! I was able to finally make the conection! I had not thought about using an IF statement in the WHILE statement. Thank you so much for the help.

    God Bless,

    Alex
    Blamestorming: Sitting around in a group discussing why a deadline was missed or a project failed and who was responsible.

    Exbabylon- Professional Internet Services


Bookmarks

Posting Permissions

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