displaying a list from a database

strangedarstrangedar Senior MemberThe Royal RAM
How can I display an unordered list(<ul></ul>) with 30 entries from a database? They enteries ned to be the most recent with the 1st one being the newest I kmnow I need to use loops but am getting very confused :confused:
Cheers

PS: I have a table called gallery with the following fields id(primary), imgTitle, fileName, author,authorAddress, createDate(date)
strangedarknesssig.jpg
sebastianastill.co.uk - My Portfolio

Comments

  • FelixFelix Junior Member Shared Hoster
    mmm.. are you trying to actually learn PHP or make everyone do it for you? :p

    Anyway what you need is something similar to this. :)

    [PHP]
    <?php
    // An example of listing 30 most recent items first from a database into a list. :D
    $result = mysql_query("SELECT * FROM gallery ORDER BY createDate LIMIT 30");
    // Conditional statement to check if any data was returned via the query....
    if(mysql_num_rows($result) > 0)
    {
    // Set the starting ul tag before the main loop.
    // I AM NOT SURE WHAT DATA YOU WANT TO DISPLAY SO I WILL JUST
    // SHOW THE AUTHOR INFO. I AM SURE YOU CAN FIGURE OUT HOW
    // TO CHANGE THIS YOURSELF!!!
    echo "<ul>";
    while($r = mysql_fetch_array($result))
    {
    echo "<li>" . $r / "</li>";
    }
    // Ending ul tag
    echo "</ul>";
    }
    ?>
    [/PHP]

    That's a very basic example for you. Just simply adjust it to suit your own needs. :D

    EDIT: oops I forgot the database connection code. I am sure you already have that figured right?
  • strangedarstrangedar Senior Member The Royal RAM
    Thanks fro the respons felix. Sorry I keep asking dumb questions but i'm geting really confused here... I'm having some promblems with the code yuo gave me but am trying to figure it out.. Will have to post the pronlem soon if I cant get it...

    Thanks for your help though I really appreciate it! :D
    strangedarknesssig.jpg
    sebastianastill.co.uk - My Portfolio
  • strangedarstrangedar Senior Member The Royal RAM
    its ok... sorted it. Uisng something a bit different...
    [PHP]<?php
    $result = mysql_query("SELECT id, page, title, author, authorWebsite FROM poetry ORDER BY createDate DESC LIMIT 30");
    echo "<UL class='list'>\n";
    while (list ($id, $p, $t, $a, $aW) = mysql_fetch_row($result)) {
    echo "<li> <a href='poetry/$p' target='_blank'>$t</a> - <a href='$aW' target='_blank'>$a</a></li>\n";
    }
    echo "</UL>\n"; ?>[/PHP]

    Thanks anyway! :D ;) :cool:
    strangedarknesssig.jpg
    sebastianastill.co.uk - My Portfolio
  • FelixFelix Junior Member Shared Hoster
    strangedar wrote:
    its ok... sorted it. Uisng something a bit different...
    [PHP]<?php
    $result = mysql_query("SELECT id, page, title, author, authorWebsite FROM poetry ORDER BY createDate DESC LIMIT 30");
    echo "<UL class='list'>\n";
    while (list ($id, $p, $t, $a, $aW) = mysql_fetch_row($result)) {
    echo "<li> <a href='poetry/$p' target='_blank'>$t</a> - <a href='$aW' target='_blank'>$a</a></li>\n";
    }
    echo "</UL>\n"; ?>[/PHP]

    Thanks anyway! :D ;) :cool:

    You can just use SELECT * FROM table_name because the "*" means selecting everything. That way you don't have to type out every field name. And the reason the code I showed before didn't work was because I forgot to include the DESC (descending) statement in the query.

    Meh seems like you got it working anyway. :)
  • strangedarstrangedar Senior Member The Royal RAM
    indeed :)
    strangedarknesssig.jpg
    sebastianastill.co.uk - My Portfolio
Sign In or Register to comment.