A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Searching a db

  1. #1
    Senior Member
    Join Date
    Jul 2001
    Posts
    2,467

    Searching a db

    Hi:

    I finally have some free time to learn PHP, but I need a push in the right direction for something I'm working on...

    I've set up a MySQL db using PHPmyAdmin; it has 4 columns (year, make, model, url). It's a table of used car inventory, so I want to be able to search by 'year,' 'make,' or model, and produce an HTML page containing the results, including the url to the page for each row returned in the search. I will be using dropdown lists with an onchange event handler to restrict searches to only the above 3 categories, but I would like to return results in descending order, either numerically, when searching by "year," or alphabetically, when searching by "make" or "model."

    Here's a page containing examples of how I'd like the results returned:

    http://www.ekigroup.com/table.html

    A link to a good tutorial, or better yet, some sample code I can modify for my purposes will be greatly appreciated.


    Thanks,


    -james
    "God so loved the world that he gave his only begotten son, so that whosoever believed in him would not perish, but have everlasting life."

    Image Popup Scripting Engine | Thumb PopUp Script | HTML Anchors w/Flash | Popup Script Generator | Seq. Img Swap | Img Swap | Browser Shake | Rand. Img Swap | Inline Img Swap | Screen Res. PopUp | Screen Resolution Popup Script


  2. #2
    Registered User
    Join Date
    Feb 2001
    Posts
    13,039
    Hi,

    this is not exactly what you are looking for, but users will be familiar with this way to show results

    Code:
    $query = "select * from cars where ";
    $conds = array();
    if(!empty($_POST["year"]))
      $conds[] = "year = '{$_POST["year"]}'";
    if(!empty($_POST["make"]))
      $conds[] = "make = '{$_POST["make"]}'";
    if(!empty($_POST["model"]))
      $conds[] = "model = '{$_POST["model"]}'";
    if(!count($conds))
      die("please enter some search terms");
    $query .= implode(" and ", $conds);
    $query .= " order by year, make, model";
    Next step in scripting probably should be clickable column headings that change the sort order

    Musicman

  3. #3
    Senior Member
    Join Date
    Jul 2001
    Posts
    2,467
    Hi Wolfgang:

    Thanks for the help. Unfortunately, I haven't been successful at incorporating your array scenario. It can't seem to populate it. with the table fields/column values.

    What I came up with is...


    Code:
    <?php
    $db = mysql_connect ('localhost', 'myusername', 'mypassword')
    or die ("Unable to connect to Database Server");
    mysql_select_db ('mydbname', $db)
    or die ("Could not select database");
    $frmval = $HTTP_GET_VARS["year"];
    $result = mysql_query("SELECT * FROM mytablename WHERE year = '$frmval' ORDER BY year, make, model")
    or die("Query failed : " . mysql_error());
    while ($col = mysql_fetch_assoc($result)){
    echo $col["year"] . $col["make"] . $col["model"] . $col["link"];
    }
    ?>
    This works, but I'm unsure of the best way to format the resultant
    (HTML) page.


    Thanks, again, for your help!

    -james
    Last edited by jamescover; 05-24-2004 at 01:12 AM.
    "God so loved the world that he gave his only begotten son, so that whosoever believed in him would not perish, but have everlasting life."

    Image Popup Scripting Engine | Thumb PopUp Script | HTML Anchors w/Flash | Popup Script Generator | Seq. Img Swap | Img Swap | Browser Shake | Rand. Img Swap | Inline Img Swap | Screen Res. PopUp | Screen Resolution Popup Script


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center