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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    Regular Coder ttttt's Avatar
    Join Date
    Oct 2005
    Posts
    372
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Displaying Last 5 Records

    Hi,
    I have a database which contains the articles on the front page in it. Well, to save clogging up the front page, I would like to display only the latest 5 articles in the table. The articles have an id field which could be ordered descending, but from then on, I have no idea what to do.
    I would appreciate any help,
    Thanks,
    ttttt
    http://openoffice.org/- Microsoft Office without the Microsoft.
    [home page][forum]
    Versatile, free software

  • #2
    New Coder
    Join Date
    Nov 2006
    Location
    UK
    Posts
    52
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation

    SELECT * FROM <%YOUR_TABLE%> LIMIT 5 ORDER BY desc

    Should do the trick

  • #3
    Regular Coder Tyree's Avatar
    Join Date
    Sep 2003
    Posts
    254
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Oh well, beat me to it.

  • #4
    Regular Coder ttttt's Avatar
    Join Date
    Oct 2005
    Posts
    372
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Wow! Thanks, could you also tell me how I list the items using this query?
    Thanks again,
    ttttt
    http://openoffice.org/- Microsoft Office without the Microsoft.
    [home page][forum]
    Versatile, free software

  • #5
    Regular Coder Tyree's Avatar
    Join Date
    Sep 2003
    Posts
    254
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    $query "SELECT * FROM <%YOUR_TABLE%> LIMIT 5 ORDER BY desc";
          
          while (
    $row mysql_fetch_assoc($query))
          {
              
    $lastFiveRecords[] = $row;
          } 
    PHP Code:
    foreach($lastFiveRecords AS $record) {
         print 
    $record['data'];

    'data' being the name of a column in your db table
    Last edited by Tyree; 11-29-2006 at 07:39 PM.

  • #6
    Regular Coder ttttt's Avatar
    Join Date
    Oct 2005
    Posts
    372
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That doesn't work for me. Can you help? (I get this error:mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource )
    Thanks,
    ttttt
    http://openoffice.org/- Microsoft Office without the Microsoft.
    [home page][forum]
    Versatile, free software

  • #7
    Regular Coder Tyree's Avatar
    Join Date
    Sep 2003
    Posts
    254
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry, I use a function to perform my queries. I forgot a line...
    PHP Code:
    $query "SELECT * FROM <%YOUR_TABLE%> LIMIT 5 ORDER BY desc"
           
          
    $result mysql_query($query);

    while (
    $row mysql_fetch_assoc($result)) 
          { 
              
    $lastFiveRecords[] = $row
          } 

  • #8
    Regular Coder ttttt's Avatar
    Join Date
    Oct 2005
    Posts
    372
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I now get the same error and :
    Invalid argument supplied for foreach()
    My full code, if it helps is:
    PHP Code:
    <?php
    //compression

    //include settings
    include('settings.php');
    //set variables
    $id $_REQUEST['id'];
    $action $_REQUEST['action'];
    //db connect
    mysql_connect($db_host$db_user$db_pass);
    mysql_select_db($db_name);
    //db queries
    //id query
    //check if id is numeric
    if(isset($id))
    {
        if(
    is_numeric($id))
        {
            
    $select_id mysql_query("SELECT * FROM front_page WHERE id=$id ORDER BY id");
            
    $echo_id mysql_fetch_array($select_id); 
        }
        
    //is not numeric, do NOTHING
    }
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html lang="eng-GB">
    <head>
    <!-- site title and page name -->
    <title>Versa-Soft - Home Page</title>
    <!-- meta tags -->
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
    <meta name="keywords" content="software,website,versatile,open-source,free,forums,forum,blog,site,versa-soft,download"/>
    <meta name="description" content="Free/Open source software, designed to make the web a cleaner and more versatile place."/>
    <meta name="ROBOTS" content="ALL"/>
    <!-- include styles and favicon -->
    <link href="style.css" rel="stylesheet" type="text/css"/>
    <link rel="shortcut icon" href="images/favicon.ico"/>
    </head>
    <body>
    <div class="margin">
      <div class="container">
        <div class="header">
          <div class="headerimg"> <img class="header" src="images/header.jpg" alt="Versa-Soft.com" height="150px" width="100%"/> </div>
          <div class="top_menu">
            <!-- Top menu could go here -->
          </div>
        </div>
        <div class="left_column">
          <div class="main_menu">
            <h4 class="navigation">Navigation</h4>
            <ul class="main_menu">
              <li class="main_menu"><a href="/index.php">Home</a></li>
              <li class="main_menu"><a href="/forum">Forum</a></li>
              <li class="main_menu"><a href="/news">News</a></li>
              <li class="main_menu"><a href="/software">Software Home</a></li>
              <li class="main_menu"><a href="/downloads.php">Downloads</a></li>
              <li class="main_menu"><a href="/docs.php">Documentation</a></li>
              <li class="main_menu"><a href="/support.php">Support</a></li>
              <li class="main_menu"><a href="/rss.php">RSS</a></li>
              <li class="main_menu"><a href="/info.php">Information</a></li>
              <li class="main_menu"><a href="/contact.php">Contact Us</a></li>
              <li class="main_menu"><a href="/about.php">About</a></li>
            </ul>
          </div>
          <div class="login">
            <h4 class="login">Login</h4>
            <p>Coming Soon...</p>
          </div>
          <div class="syndication">
            <h4 class="syndication">Syndication</h4>
            <ul class="syndication">
              <li class="syndication"><a href="/xml/rss.xml">RSS</a></li>
              <li class="syndication"><a href="/xml/rss.xml">XML</a></li>
            </ul>
          </div>
          <div class="newsbox">
            <h4 class="newsbox">Newsbox</h4>
            <ol class="newsitems">
              <li class="newsitems"></li>
              <li class="newsitems"></li>
              <li class="newsitems"></li>
              <li class="newsitems"></li>
              <li class="newsitems"></li>
            </ol>
          </div>
        </div>
        <div class="right_column">
          <div class="title">
            <h1 class="title">
            <?php 
            
    //if site is under construction, display notice
            
    if($under_construction == '1')
            {
                echo 
    $construction_title;
            }
            
    //check if id is set
            
    else if(isset($id))
            {
                
    //check if id is numeric
                
    if(is_numeric($id))
                {
                    echo 
    $echo_id['title'];
                }
                
    //is not numeric, display not found
                
    else
                {
                    echo(
    'Record Not Found');
                }
            }
            
    //otherwise, display the front page    
            
    ?>
            </h1>
          </div>
          <div class="content">
            <p class="content">        
            <?php 
            
    //if site is under construction, display notice
            
    if($under_construction == '1')
            {
                echo 
    $construction_notice;
            }
            
    //check if id is set
            
    else if(isset($id))
            {
                
    //check if id is numeric
                
    if(is_numeric($id))
                {
                    echo 
    $echo_id['body'];
                }
                
    //is not numeric, display not found
                
    else
                {
                    echo(
    'The id you have entered (');echo $id; echo(') could not be found. This could be because it is not numeric, or because of a dodgy link, or the record just not existing.');
                }
            }
            
    //check if action is set
            
    else if(isset($action))
            {
                
    //discover what kind of action
                
    echo('action');
            }
            
    //otherwise, display the front page
            
    else
            {
                
    $query "SELECT * FROM front_page LIMIT 5 ORDER BY id desc";      
                
    $result mysql_query($query);
                while (
    $row mysql_fetch_assoc($result)) 
                { 
                  
    $lastFiveRecords[] = $row
                }
                foreach(
    $lastFiveRecords AS $record) {
                 echo 
    $record['title'];
                } 
            }        
            
    ?></p>
            <p class="notices"></p>
          </div>
        </div>
        <div class="clear"></div>
        <div class="footer">
          <div class="copyright">
            <p class="copyright"><a href="copyright.php"><?php echo $copyright_notice?></a></p>
          </div>
          <div class="badges"> </div>
        </div>
      </div>
    </div>
    </body>
    </html>
    Thanks,
    ttttt
    http://openoffice.org/- Microsoft Office without the Microsoft.
    [home page][forum]
    Versatile, free software

  • #9
    Regular Coder Tyree's Avatar
    Join Date
    Sep 2003
    Posts
    254
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I see you must have your database login into in that settings file, are you successfully logging in to your server?

    No queries are going to work if you're not connected to your database to begin with.

    Secondly, the foreach() I gave you was just simply to show how you could get the info out of the array. You'll probably want to do some formatting of the data you spew out with the foreach(). Right now, all it's going to do is list the data out across the page. I'm assuming you want to put it in a table or something.

    Here's what I did and it's working on mine with my database:
    PHP Code:
    $query "SELECT * FROM News ORDER BY ID DESC LIMIT 5";       
                
    $result mysql_query($query); 
                while (
    $row mysql_fetch_assoc($result))  
                {  
                  
    $lastFiveRecords[] = $row;  
                } 
                foreach(
    $lastFiveRecords AS $record) { 
                 print 
    $record['ID']."<br />"
                } 
    Last edited by Tyree; 11-29-2006 at 09:26 PM.

  • #10
    Regular Coder
    Join Date
    Aug 2006
    Location
    UK, London, Dartford
    Posts
    221
    Thanks
    3
    Thanked 14 Times in 14 Posts
    Quick hint dont have to double echo only silly but hey:
    PHP Code:
    echo('The id you have entered ('.$id.') could not be found. This could be because it is not numeric, or because of a dodgy link, or the record just not existing.'); 

  • #11
    Regular Coder ttttt's Avatar
    Join Date
    Oct 2005
    Posts
    372
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well, I now get a really strange error, displayed here:
    http://versa-soft.com/
    Thanks,
    ttttt
    http://openoffice.org/- Microsoft Office without the Microsoft.
    [home page][forum]
    Versatile, free software

  • #12
    Regular Coder Tyree's Avatar
    Join Date
    Sep 2003
    Posts
    254
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I don't see an error. Are you talking about the "12UT A" going down the page? Is that where your "last 5" should be showing?

    Post your code again so I can see what you changed.

  • #13
    Regular Coder ttttt's Avatar
    Join Date
    Oct 2005
    Posts
    372
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Tyree View Post
    Are you talking about the "12UT A" going down the page?
    Yes, exactly.
    PHP Code:
    <?php
    //compression

    //include settings
    include('settings.php');
    //set variables
    $id $_REQUEST['id'];
    $action $_REQUEST['action'];
    //db connect
    mysql_connect($db_host$db_user$db_pass);
    mysql_select_db($db_name);
    //db queries
    //id query
    //check if id is numeric
    if(isset($id))
    {
        if(
    is_numeric($id))
        {
            
    //$select_id = mysql_query("SELECT * FROM front_page WHERE id=$id ORDER BY id");
            //$echo_id = mysql_fetch_array($select_id); 
        
    }
        
    //is not numeric, do NOTHING
    }
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html lang="eng-GB">
    <head>
    <!-- site title and page name -->
    <title>Versa-Soft - Home Page</title>
    <!-- meta tags -->
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
    <meta name="keywords" content="software,website,versatile,open-source,free,forums,forum,blog,site,versa-soft,download"/>
    <meta name="description" content="Free/Open source software, designed to make the web a cleaner and more versatile place."/>
    <meta name="ROBOTS" content="ALL"/>
    <!-- include styles and favicon -->
    <link href="style.css" rel="stylesheet" type="text/css"/>
    <link rel="shortcut icon" href="images/favicon.ico"/>
    </head>
    <body>
    <div class="margin">
      <div class="container">
        <div class="header">
          <div class="headerimg"> <img class="header" src="images/header.jpg" alt="Versa-Soft.com" height="150px" width="100%"/> </div>
          <div class="top_menu">
            <!-- Top menu could go here -->
          </div>
        </div>
        <div class="left_column">
          <div class="main_menu">
            <h4 class="navigation">Navigation</h4>
            <ul class="main_menu">
              <li class="main_menu"><a href="/index.php">Home</a></li>
              <li class="main_menu"><a href="/forum">Forum</a></li>
              <li class="main_menu"><a href="/news">News</a></li>
              <li class="main_menu"><a href="/software">Software Home</a></li>
              <li class="main_menu"><a href="/downloads.php">Downloads</a></li>
              <li class="main_menu"><a href="/docs.php">Documentation</a></li>
              <li class="main_menu"><a href="/support.php">Support</a></li>
              <li class="main_menu"><a href="/rss.php">RSS</a></li>
              <li class="main_menu"><a href="/info.php">Information</a></li>
              <li class="main_menu"><a href="/contact.php">Contact Us</a></li>
              <li class="main_menu"><a href="/about.php">About</a></li>
            </ul>
          </div>
          <div class="login">
            <h4 class="login">Login</h4>
            <p>Coming Soon...</p>
          </div>
          <div class="syndication">
            <h4 class="syndication">Syndication</h4>
            <ul class="syndication">
              <li class="syndication"><a href="/xml/rss.xml">RSS</a></li>
              <li class="syndication"><a href="/xml/rss.xml">XML</a></li>
            </ul>
          </div>
          <div class="newsbox">
            <h4 class="newsbox">Newsbox</h4>
            <ol class="newsitems">
              <li class="newsitems"></li>
              <li class="newsitems"></li>
              <li class="newsitems"></li>
              <li class="newsitems"></li>
              <li class="newsitems"></li>
            </ol>
          </div>
        </div>
        <div class="right_column">
          <div class="title">
            <h1 class="title">
            <?php 
            
    //if site is under construction, display notice
            
    if($under_construction == '1')
            {
                echo 
    $construction_title;
            }
            
    //check if id is set
            
    else if(isset($id))
            {
                
    //check if id is numeric
                
    if(is_numeric($id))
                {
                    echo 
    $echo_id['title'];
                }
                
    //is not numeric, display not found
                
    else
                {
                    echo(
    'Record Not Found');
                }
            }
            
    //otherwise, display the front page    
            
    ?>
            </h1>
          </div>
          <div class="content">
            <p class="content">        
            <?php 
            
    //if site is under construction, display notice
            
    if($under_construction == '1')
            {
                echo 
    $construction_notice;
            }
            
    //check if id is set
            
    else if(isset($id))
            {
                
    //check if id is numeric
                
    if(is_numeric($id))
                {
                    echo 
    $echo_id['body'];
                }
                
    //is not numeric, display not found
                
    else
                {
                    echo(
    'The id you have entered (');echo $id; echo(') could not be found. This could be because it is not numeric, or because of a dodgy link, or the record just not existing.');
                }
            }
            
    //check if action is set
            
    else if(isset($action))
            {
                
    //discover what kind of action
                
    echo('action');
            }
            
    //otherwise, display the front page
            

    // problems here !!!!!!!



    else
            {
                
    $query "SELECT * FROM front_page ORDER BY ID DESC LIMIT 5";       
                
    $result mysql_query($query); 
                while (
    $row mysql_fetch_row($result))  
                {  
                  
    $records $row;  
                } 
                foreach (
    $records AS $record) { 
                 echo 
    $record[title]."<br />"
                }  
            }        
            
    ?></p>
            <p class="notices"></p>
          </div>
        </div>
        <div class="clear"></div>
        <div class="footer">
          <div class="copyright">
            <p class="copyright"><a href="copyright.php"><?php echo $copyright_notice?></a></p>
          </div>
          <div class="badges"> </div>
        </div>
      </div>
    </div>
    </body>
    </html>
    ttttt
    http://openoffice.org/- Microsoft Office without the Microsoft.
    [home page][forum]
    Versatile, free software

  • #14
    New Coder
    Join Date
    Nov 2006
    Location
    UK
    Posts
    52
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Is that the first letter(s) to your database row title(s)?
    - Mark

  • #15
    Regular Coder Tyree's Avatar
    Join Date
    Sep 2003
    Posts
    254
    Thanks
    0
    Thanked 0 Times in 0 Posts
    First thing I see is this:
    PHP Code:
    $records $row
    Should be...
    PHP Code:
    $records[] = $row
    That makes $records an array.

    Still, you're not getting an error. You're just not getting what you expected. You're VERY close!


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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