SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Jul 2001
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how to insert a new row before the old one

    hi,

    i have two pages for a news area where certain individuals who are logged in may update a database and everyone else just views the output on a seperate page. My problem is I use a while loop to display all the contents of the table with all the news posts in. This is fine (i want to display all the rows in that database), but it displays them in the wrong order. Whenever a new post is inserted into the database it is done so like this:

    INSERT INTO newstable SET
    messenger = '$theman',
    date = '$thedate',
    tgdnews = '$news'"

    This inserts the new news item below the last one which has been inserted so when the while loop runs it displays them:

    oldest news story
    second oldest
    Most recent news story

    Obviously I want this to be in reverse (because you want the latest news to be at the top of the webpage since this is where all browsers start), so cutting to the chase I need to know what the command is for inserting a new row at the TOP of the table.

    Thanks

  2. #2
    SitePoint Member
    Join Date
    Jul 2001
    Location
    Luxembourg
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What about displaying them with 'order by' , I suppose that you're using a mysql query to display them somewhere ?

  3. #3
    SitePoint Member
    Join Date
    Jul 2001
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    hi

    That is right yes, im using the following code to display the contents of the table on the news site:

    echo "<br><br>";
    $news = mysql_query( "SELECT * FROM mytable");
    while ( $myvariable = mysql_fetch_array($news) ) {

    echo "<p align=center><table border=1 background=http://www.mywebsite.com/myimages/mypic.gif cellpadding=3 cellspacing=1><tr>";

    echo("<td cellpadding=30 align=left valign=top><center><b>By: " . $myvariable["messenger"] . "</center></b></td>");

    echo("<td cellpadding=30 align=left valign=top><center><i>" . $myvariable["date"] . "</i></center></td>");

    echo("<td width=800 cellpadding=30 align=left><font color=DARKRED face=Lucida Blackletter size=4>" . $myvariable["thenews"] . "</font></td>");

    echo "</tr></table></p><br><br>";
    }

    ofcourse thenews, messenger and date are all column names in my table. SO what is this order by thing you are talk of?

  4. #4
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ORDER BY orders the results by a specified column in either ascending or descending order.

    PHP Code:
    $news mysql_query"SELECT * FROM mytable ORDER by date DESC"); 
    This would order the records by the date field in a descending order, or newest first.

    PHP Code:
    $news mysql_query"SELECT * FROM mytable ORDER by date ASC"); 
    or

    PHP Code:
    $news mysql_query"SELECT * FROM mytable ORDER by date"); 
    This would order the records by the date field in a ascending order, or oldest first.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  5. #5
    SitePoint Member
    Join Date
    Jul 2001
    Location
    Luxembourg
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's exactly the way to do it ( I suppose this will help you)


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
  •