SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    HI silver trophy Silverado4x4's Avatar
    Join Date
    Jan 2001
    Location
    USA
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Database Driven Sites

    I have a script that redirects the user (.php?id=1) and have it set up as "ID, address, and label".

    I want it to print on a PHP file this:

    <a href="$address">$label</a>

    but so where it is in alphabetical order (the label), and will fully update when one is added. It's sort of like a links page.

    Any suggestions?

    This is the file I have so far:

    <?
    $db_name = "DBNAME";
    $table_name = "TABLENAME";
    $pathstring = getenv("HTTP_HOST") == "localhost" ? getenv("PATH_INFO") : $PHP_SELF ;

    $id = $id;

    $connection = mysql_connect("localhost", "NAME", "DBPASSWORD")
    or die("Couldn't connect.");

    $db = mysql_select_db($db_name, $connection) or die("Couldn't select database.");

    $sql = "SELECT id, address FROM $table_name";
    $sql .= " WHERE id = " . $id . ";";
    $result = mysql_query($sql,$connection) or die("Couldn't execute query.");

    while ($row = @mysql_fetch_array($result))
    {
    $id = $row["id"];
    $address = $row["address"];
    }

    print "<HTML><HEAD>\n";
    print "<SCRIPT language=\"JavaScript1.1\">\n";
    print "<!--\n";
    print "location.replace(\"".$address."\");\n";
    print "//-->\n";
    print "</SCRIPT>\n";
    print "<NOSCRIPT>\n";
    print "<META http-equiv=\"Refresh\" content=\"0; URL=".$address."\">\n";
    print "</NOSCRIPT>\n";
    print "</HEAD>\n";
    print "<BODY>\n";
    print "Click <A href=\"".$address."\">here</A> to continue.\n";
    print "</BODY>\n";
    print "</HTML>\n";
    ?>

  2. #2
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    add "ORDER BY label ASC" to your SQL.

  3. #3
    HI silver trophy Silverado4x4's Avatar
    Join Date
    Jan 2001
    Location
    USA
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What do you mean by that?

    I have hardly any knowledge of PHP/MySQL, so please show me what you are talking about. I am trying to learn by looking at other people's scripts.

    Also, how would I get it to display more than 1 link at a time - to however many there are?

  4. #4
    Serial Publisher silver trophy aspen's Avatar
    Join Date
    Aug 1999
    Location
    East Lansing, MI USA
    Posts
    12,939
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sounds like you need to learn SQL.

    An SQL Select statement is structured like this usually.

    *SELECT
    *FROM
    WHERE
    ORDER BY
    GROUP BY

    Only the first 2 are absolutely required.

    Lets say you had a table of friends.

    SELECT Firstname FROM Friends

    Would return every record in that table, or a list of all the firstname's of your friends.

    Select Firstname FROM Friends WHERE lastname = 'Smith'

    would return a list of all the first names of your friends who's last name is 'Smith'

    So if this was your table:

    Joan Smith
    Paul Connor
    John Malkovich
    Peter White
    Connie Smith
    Karl Sandson


    The first query would return

    Joan
    Paul
    John
    Peter
    Connie
    Karl

    The second query would return

    Joan
    Connie

    Get it?

    As for order by. You can do this

    Select Firstname from Friends ORDER BY Firstname

    which would return

    Connie
    Joan
    John
    Karl
    Paul
    Peter

    Alphabetical

    If you did this.

    Select Firstname from Friends ORDER BY Firstname DESC

    It would return reverse alphabetical

    Peter
    Paul
    Karl
    John
    Joan
    Connie


    If you're having trouble learning SQL you should probably get a reference book on it. Its a very logical language and it reads almost like a spoken language.
    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

  5. #5
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    http://www.devshed.com/Server_Side/M...tro/page1.html is a great tutorial for the main stuff you'll want to do with MySQL.


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
  •