SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast daverozelle's Avatar
    Join Date
    Mar 2003
    Location
    Western, PA - USA
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help me with some PHP - mySQL code please!!!!

    I am trying to create a menu page that displays information stored in a mySQL db. Its a list of names, addresses, etc. One of the fields in my table is 'Position' and I'd like to create a form on my menu page so that I can select a position from a drop down and only show the records that match that position.

    Problem is, the values stored in the Position column are not predefined. So I am thinking I need to somehow query the db to return a list of values used in the table, then make an array with those values, and populate a drop down form field with those values as options.

    Am I doing this the hard way? How can I do this?

    daverozelle

  2. #2
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try a query like this to get the different positions available and used in the table.
    Code:
    SELECT DISTINCT position FROM table

  3. #3
    ********* Wizard silver trophy Cam's Avatar
    Join Date
    Aug 2002
    Location
    Burpengary, Australia
    Posts
    4,495
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    Easy
    PHP Code:
    $sql "select distinct
                position
            from
                tablename" 
    ;
    $rs mysql_query($sql) or die( mysql_error() );

    echo( 
    '<select name="postition" onChange="some_js_code_or_whatever">' );
    while( 
    $row mysql_fetch_object($rs) ) {
        echo( 
    '<option value="' $row->postition '">' $row->postition '</option>' );
    }
    echo( 
    '</select' ); 
    There's your drop-down menu, and just check the form submit there in the PHP to create a where clause to add to your SQL to only select from a certain position

  4. #4
    ********* Wizard silver trophy Cam's Avatar
    Join Date
    Aug 2002
    Location
    Burpengary, Australia
    Posts
    4,495
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    Beat me

  5. #5
    SitePoint Enthusiast daverozelle's Avatar
    Join Date
    Mar 2003
    Location
    Western, PA - USA
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DJ P@CkMaN
    Easy
    PHP Code:
    $sql "select distinct
                position
            from
                tablename" 
    ;
    $rs mysql_query($sql) or die( mysql_error() );
     
    echo( 
    '<select name="postition" onChange="some_js_code_or_whatever">' );
    while( 
    $row mysql_fetch_object($rs) ) {
        echo( 
    '<option value="' $row->postition '">' $row->postition '</option>' );
    }
    echo( 
    '</select' ); 
    There's your drop-down menu, and just check the form submit there in the PHP to create a where clause to add to your SQL to only select from a certain position
    Awesome. That worked great! It might have been easy for you, but I am still working my way through my first PHP book learning it, and having to use it for work at the same time.

    Thanks a ton again.


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
  •