SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2002
    Location
    UK (The only place ot live)
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    mysql alphabetical query?

    PHP]a<?php
    session_start();

    $host="localhost";
    $login = "";
    $password = "";
    $db = "";
    $connection = mysql_connect($host, $login, $password);
    $database = mysql_select_db($db, $connection);

    if ($system) $argument = "WHERE cheats.system = '$system'";
    $games = mysql_query("SELECT DISTINCT cheats.game FROM cheats INNER JOIN games ON cheats.game = games.game $argument ORDER BY cheats.game");
    ?>

    Add your default headers here

    <?php
    // <!-- THIS LOOP PRINTS THE GAMES. CHANGE THE TEXT AS YOU LIKE... -->
    while($game = mysql_fetch_array($games)) {
    print "Show all cheats for <A HREF=\"showcheats.php?game=$game[0]\">$game[0]</A> ... <BR>";
    }
    ?>

    <!-- THIS FORM IS FOR YOUR CONVENIENCE -->
    Change the system for which to show the games:
    <FORM ACTION="listing.php">
    <SELECT NAME="system">
    <OPTION VALUE=""> (all systems) </OPTION>
    <?php
    $systems = mysql_query("SELECT DISTINCT system FROM cheats");
    while ($system = mysql_fetch_array($systems))
    print "<OPTION VALUE=\"$system[0]\">$system[0]</OPTION>\n";
    ?>
    <INPUT TYPE=submit VALUE="Show!">
    </FORM>[/PHP]

    I need this code to alphabeticaly get cheats from my mysql table but only for the specified conole, eg I click on a link to view all game for the PS2 begginging with the letter A how would i do this, can i edit my scirpt i already done?

  2. #2
    .NET inside archigamer's Avatar
    Join Date
    Jan 2002
    Location
    Strongsville OH
    Posts
    1,534
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    use 1 percent sign in front of the variable for example

    Code:
    select * from mytable where mycolumn = %myargument
    in previous example it will select all columns beggining with m cause the first letter in myargument is m
    Web Finesse Studios
    Professional, business oriented web hosting and development.

  3. #3
    Prolific Blogger silver trophy Technosailor's Avatar
    Join Date
    Jun 2001
    Location
    Before These Crowded Streets
    Posts
    9,446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Moved to the *sigh* MySQL forum...
    Aaron Brazell
    Technosailor



  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,350
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    as archigamer said, you need to use the % wildcard, but here's the correct syntax for the sql (i don't know php so please forgive if i make an error)

    change

    if ($system) $argument = "WHERE cheats.system = '$system'";

    to

    if ($system) $argument = "WHERE cheats.system LIKE '$system%'";

    in other words, the $system variable would pass in the letter A and you have to concatenate the percent sign after it, so that the sql resolves to LIKE 'A%'
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


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
  •