Thread: News script

    #1
  1. Full Access
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jun 2000
    Location
    London, UK
    Posts
    2,019
    Rep Power
    17
    OK, I've done this news script utilising PHP and mySQL. Part of it is listed below. Basically the idea is that I want to (in this part of the script) select all the news posts ordered by the id number (which is time-based so they come out with the most recent first) and then print the two most recent posts onto the page. However this doesn't seem to work. Please can someone tell me what's wrong:

    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>

    elseif ($cmd == "default") {

    MYSQL_CONNECT(localhost,user,pass) OR DIE("Unable to connect to database");
    @mysql_select_db("db") or die("Unable to select database");

    $query = "SELECT * FROM news ORDER BY id";

    mysql_query($query);

    MYSQL_CLOSE();

    include("top.txt");

    print "<img src="news.gif" width="524" height="28"><br>n";
    print "<!---Start News--->nn";

    for ($i = 0; $i < 2; $i++) {

    print "<!---Article $row[id]--->n";
    print "<font color="#c0c0c0">$row[date]</font><br>n";
    print "<u><font size="2">$row[title]</font><br>n";
    print "<font color="#d0d0d0">$row[body]</font><br>n";
    print "<img src="line.gif" width="524" height="12"><br>n";
    print "<!---End Article $row[id]--->nn";

    }

    print "<a href="news.php3?cmd=old">-Old News-</a>n";
    print "<!---End News--->nn";

    include("bottom.txt");


    }
    ?>

    [/code]
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Location
    London/UK
    Posts
    91
    Rep Power
    15
    Hi

    you don't seem to have included anything that pulls the data from the DB, you've executed the query (seemingly correctly) but all mysql_query does is give you a result pointer, you then need to pull the data out

    ALso you want order by a date feild and make it descending...

    I'd change your code to this:

    ============

    $sql ="SELECT * FROM news ORDER BY a_date_field DESC";

    $result = mysql_query($sql);
    ^^^^^^^
    include("top.txt");
    print "<img src="news.gif" width="524" height="28"><br>n";

    print "<!---Start News--->nn";

    while ($row = mysql_fetch_array($result)) { # loop around pulling the data out into a nice to manage array

    print "<!---Article $row[id]--->n";
    print "<font color="#c0c0c0">$row[date]</font><br>n";

    print "<u><font size="2">$row[title]</font><br>n";

    print "<font color="#d0d0d0">$row[body]</font><br>n";

    print "<img src="line.gif" width="524" height="12"><br>n";

    print "<!---End Article $row[id]--->nn";

    } # END while loop

    print "<a href="news.php3?cmd=old">-Old News-</a>n";

    print "<!---End News--->nn"; include("bottom.txt");}?>

    mysql_close();

    ============
    that should sort it

    hth

    Bealers
    =========
    http://back-end.org

    [This message has been edited by Bealers (edited August 07, 2000).]
  4. #3
  5. Full Access
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jun 2000
    Location
    London, UK
    Posts
    2,019
    Rep Power
    17
    Hi and thanks,

    But the problem is I only want the loop that displays the data (ie the first TWO posts) to loop TWICE. So it only puts the two most recent posts onto the screen.

    The ID number is a date-based field (it's seconds since the epoch) so that's why I'm retrieving the data ordered by that field

    Alex
    (alex@ur-ban.com)
    (http://www.alex-greg.co.uk)
    (ICQ: 62878174)

    [This message has been edited by alexgreg (edited August 07, 2000).]
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Location
    London/UK
    Posts
    91
    Rep Power
    15
    OK cool,
    well you still had nothing that actually pulled the data out, so use just tag LIMIT 0,2 on the end of your SQL to only give you 2 results, the while loop will only loop twice then

    ------------------
    http://back-end.org
  8. #5
  9. Full Access
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jun 2000
    Location
    London, UK
    Posts
    2,019
    Rep Power
    17
    Whoa! What's this limit thing? I've a feeling this might be useful.....

    Because the other part of the news script (the "old" part) selects everything APART FROM the first two results. How can I modify the limit thing to do that?

    Thanks,

    Alex
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2000
    Posts
    669
    Rep Power
    15
    You can use the LIMIT feature to only grab a certian number of records like
    $query = "SELECT * FROM news ORDER BY id LIMIT 0,2";

    This means grab 2 records starting at row 0

    Then you can use your regular while loop to get the data
    $result = mysql_query($query);
    while ($row = mysql_fetch_array($result)) {
    print "<!---Article $row[id]--->n";
    print "<font color="#c0c0c0">$row[date]</font><br>n";
    print "<u><font size="2">$row[title]</font><br>n";
    print "<font color="#d0d0d0">$row[body]</font><br>n";
    print "<img src="line.gif" width="524" height="12"><br>n";
    print "<!---End Article $row[id]--->nn";

    }
  12. #7
  13. Full Access
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jun 2000
    Location
    London, UK
    Posts
    2,019
    Rep Power
    17
    Thanks, but how can I use this to get (this is another feature of the script which selects the "old" news) everything apart from the two most recent posts?

    Thanks,

    Alex
  14. #8
  15. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Location
    Portland, Or USA
    Posts
    3
    Rep Power
    0
    I hope this helps:

    Limit
    Used to constrain the number of rows returned by the select statement. This clause can take one or two numeric arguments. If two arguments are used, the first one will indicate the offset from the first row and the second the number of rows from the offset to return. The ofset is zero based, i.e. the first row in the resultset has an offset of 0 (zero). For examploe to return the first ten titles of documents use:
    SELECT titles, published FROM document LIMIT 10;
    and to return the next 10 rows (rows 11 - 20)
    SELECT titles, published FROM document LIMIT 10,10;

    this is taken from the book: Professional PHP Programming...seems to be a very good book...about 900 pages

    so to get the next info for your data...
    you have taken two off the top okay...
    so now use
    LIMIT 2,x
    where x is the number of entries you have
    or
    LIMIT 2,102
    to give you the next 100 after the previously "removed" 2

Similar Threads

  1. php script won't work inside "cgi-local" directory
    By Volitics in forum PHP Development
    Replies: 5
    Last Post: April 26th, 2004, 11:50 PM
  2. Replies: 2
    Last Post: February 11th, 2004, 12:03 AM
  3. How can I enable a power user to run this script?
    By Yamadatroy in forum Visual Basic Programming
    Replies: 2
    Last Post: January 19th, 2004, 10:39 PM
  4. Need Help with news script
    By Svain in forum PHP Development
    Replies: 4
    Last Post: December 28th, 2003, 02:06 PM
  5. Positioning a news script on top of background
    By jtwn2 in forum HTML Programming
    Replies: 2
    Last Post: December 21st, 2003, 04:43 PM

IMN logo majestic logo threadwatch logo seochat tools logo