SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 28

Hybrid View

  1. #1
    SitePoint Zealot trigger8's Avatar
    Join Date
    Nov 2000
    Location
    Dallas, TX
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i have two drop down menu one contain the state name, second contian some cities of that state. I have to select the state in first drop down all the corresponding cities should come in the second drop down automatically corresponding to the state selected. I've the data in MySQL. How do I proceed?
    Trigger8

    **************************
    I killed a 6-pack just to watch it die!

  2. #2
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    query the database for DISTINCT states.
    $result = mysql_query("SELECT DISTINCT statefieldname from tablename ORDER by statefieldname ASC");

    build the drop down list

    print '<select name="state">';
    while ($row = mysql_fetch_array($result)) {
    printf('<option value="%s">%s', $row["statefieldname"], $row["statefieldname"]);
    }
    print '</select>';


    Submit the form

    check for $state

    if ($state) {
    //Query the db for the cities for that state
    $result = mysql_query("SELECT citiesfieldname from tablename WHERE statefieldname = '$state' ORDER by citiesfieldname ASC");

    //build drop down
    print '<select name="city">';
    while ($row = mysql_fetch_array($result)) {
    printf('<option value="%s">%s', $row["citiesfieldname"], $row["citiesfieldname"]);
    }
    print '</select>';

    }
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  3. #3
    SitePoint Zealot trigger8's Avatar
    Join Date
    Nov 2000
    Location
    Dallas, TX
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The page only shows an empty drop down menu. Here's what I got from viewing the source:
    <select name="state"><br>
    <b>Warning</b>: Supplied argument is not a valid MySQL result resource in <b>/path/to/testphp.php</b> on line <b>16</b><br>
    </select>
    Trigger8

    **************************
    I killed a 6-pack just to watch it die!

  4. #4
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you show us a little more of the code maybe like the whole part that queries the db?
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  5. #5
    SitePoint Zealot trigger8's Avatar
    Join Date
    Nov 2000
    Location
    Dallas, TX
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <?

    $SQLServer = "localhost";
    $DBUser = "--";
    $DBPass = "--";
    $DBName = "--";

    mysql_connect($SQLServer, $DBUser, $DBPass);

    //query the database for DISTINCT states.
    $result = mysql_query("SELECT DISTINCT state from State ORDER by state ASC");

    //build the drop down list
    print '<select name="state">';
    while ($row = mysql_fetch_array($result)) {
    printf('<option value="%s">%s', $row["state"], $row["state"]);
    }
    print '</select>';


    //Submit the form
    //check for $state
    if ($state) {
    //Query the db for the cities for that state
    $result = mysql_query("SELECT city from City WHERE state = '$state' ORDER by city ASC");

    //build drop down
    print '<select name="city">';
    while ($row = mysql_fetch_array($result)) {
    printf('<option value="%s">%s', $row["city"], $row["city"]);
    }
    print '</select>';

    }
    ?>
    Trigger8

    **************************
    I killed a 6-pack just to watch it die!

  6. #6
    SitePoint Zealot trigger8's Avatar
    Join Date
    Nov 2000
    Location
    Dallas, TX
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is the MySQL design:

    BD table name:
    State
    City

    State:
    State_ID
    State_Name

    City:
    City_ID
    City_Name
    FK_STATE_ID (foreign key)

    Any idea?

    Thanks,
    Trigger8

    **************************
    I killed a 6-pack just to watch it die!

  7. #7
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Study your PHP and MySQL

    $SQLServer = "localhost";
    $DBUser = "--";
    $DBPass = "--";
    $DBName = "--";

    mysql_connect($SQLServer, $DBUser, $DBPass);

    //query the database for DISTINCT states.
    $result = mysql_query("SELECT * from State ORDER by State_Name ASC");

    //build the drop down list
    print '<select name="state">';
    while ($row = mysql_fetch_array($result)) {
    printf('<option value="%s">%s', $row["State_ID"], $row["State_Name"]);
    }
    print '</select>';


    //Submit the form
    //check for $state
    if ($state) {
    //Query the db for the cities for that state
    $result = mysql_query("SELECT * from City WHERE FK_STATE_ID = '$state' ORDER by City_Name ASC");

    //build drop down
    print '<select name="city">';
    while ($row = mysql_fetch_array($result)) {
    printf('<option value="%s">%s', $row["City_ID"], $row["City_Name"]);
    }
    print '</select>';

    }
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  8. #8
    SitePoint Zealot trigger8's Avatar
    Join Date
    Nov 2000
    Location
    Dallas, TX
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm still getting the same error. What's wrong in line 16??
    Trigger8

    **************************
    I killed a 6-pack just to watch it die!

  9. #9
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    YOu did change your code to reflect what I have given you didn't have the right field names in your queries or in your printf() statement I changed them to hte correct ones, but make sure that all of the fieldnames and tables names that are used in the code are the same as what they are in the database. Also if you are going to refer to a line number please show us which line that is
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  10. #10
    SitePoint Zealot trigger8's Avatar
    Join Date
    Nov 2000
    Location
    Dallas, TX
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've already posted the whole scripts above.
    Trigger8

    **************************
    I killed a 6-pack just to watch it die!

  11. #11
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You see your code above, correct? You do see my code right under it? Correct? Did you change the stuff to make it look like my code? Also we don't know where your code starts on the page so we don't know where line 16 is, maybe if you made a notation to where the line is that is the error, it would help. I suspect if you change your code to look like mine above your problem will be solved.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  12. #12
    SitePoint Zealot trigger8's Avatar
    Join Date
    Nov 2000
    Location
    Dallas, TX
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, I do see your code and used exactly the code that you've written. I checked with the table and fieldname in the database but everything is correct.
    Below is exactly the code the I used.
    ------------------------------------------
    <?

    $SQLServer = "localhost";
    $DBUser = "--";
    $DBPass = "--";
    $DBName = "--";

    mysql_connect($SQLServer, $DBUser, $DBPass);

    //query the database for DISTINCT states.
    $result = mysql_query("SELECT * from State ORDER by State_Name ASC");

    //build the drop down list
    print '<select name="state">';
    while ($row = mysql_fetch_array($result)) {
    printf('<option value="%s">%s', $row["State_ID"], $row["State_Name"]);
    }
    print '</select>';


    //Submit the form
    //check for $state
    if ($state) {
    //Query the db for the cities for that state
    $result = mysql_query("SELECT * from City WHERE FK_STATE_ID = '$state' ORDER by City_Name ASC");

    //build drop down
    print '<select name="city">';
    while ($row = mysql_fetch_array($result)) {
    printf('<option value="%s">%s', $row["City_ID"], $row["City_Name"]);
    }
    print '</select>';

    }
    ?>
    -----------------------------------------------
    Trigger8

    **************************
    I killed a 6-pack just to watch it die!

  13. #13
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Looks like you forgot to to select your db

    $SQLServer = "localhost";
    $DBUser = "--";
    $DBPass = "--";
    $DBName = "--";

    $db = mysql_connect($SQLServer, $DBUser, $DBPass);
    mysql_select_db($DBName, $db);
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  14. #14
    SitePoint Zealot trigger8's Avatar
    Join Date
    Nov 2000
    Location
    Dallas, TX
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It really was the DBName! Thanks!
    But you know what, only the drop down menu for state is being shown.
    Trigger8

    **************************
    I killed a 6-pack just to watch it die!

  15. #15
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You cannot have them both show at once, you need to show the state one, then have a submit button who's action is the same page, then after the form is submitted the city dro down list shoudl appear.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  16. #16
    SitePoint Zealot trigger8's Avatar
    Join Date
    Nov 2000
    Location
    Dallas, TX
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your help would be appreciated if you could show me how. Thanks!
    Trigger8

    **************************
    I killed a 6-pack just to watch it die!

  17. #17
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You are familiar with html and forms? YOU need to setup the first form like I showed you with a submit button.

    The action should be the same page that you are on and can be easily done by using

    if (!$state) {
    print '<form action="'. $PHP_SELF .'" method="post">';
    //Your state dropdown here
    print '<input type="submit name="submit" value="submit">';
    }
    else {
    print '<form action="'. $PHP_SELF .'" method="post">';
    //Your city dropdown here
    print '<input type="submit name="submit" value="submit">';

    }

    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  18. #18
    SitePoint Zealot trigger8's Avatar
    Join Date
    Nov 2000
    Location
    Dallas, TX
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm familiar with html and forms, but not with php. I really need help on that.
    Trigger8

    **************************
    I killed a 6-pack just to watch it die!

  19. #19
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmmm try http://www.php.net! I have time to answer questions in short, but I haven't the time or desire to write whole scripts for people, Good Luck!
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  20. #20
    SitePoint Zealot trigger8's Avatar
    Join Date
    Nov 2000
    Location
    Dallas, TX
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cool...It's working now! Thanks freddy! ( and of course, your cat too!)
    Trigger8

    **************************
    I killed a 6-pack just to watch it die!

  21. #21
    SitePoint Zealot trigger8's Avatar
    Join Date
    Nov 2000
    Location
    Dallas, TX
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Another question. I would like to display the selected state name on the next page, which would be the city selection page. How to make this happen?
    Trigger8

    **************************
    I killed a 6-pack just to watch it die!

  22. #22
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    print $state;
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  23. #23
    SitePoint Zealot trigger8's Avatar
    Join Date
    Nov 2000
    Location
    Dallas, TX
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I did, but it appeared to be the state_ID number not the state name. Is state_ID the only value that parse to the next module?

    printf('<option value="%s">%s', $row["State_ID"], $row["State_Name"]);
    Trigger8

    **************************
    I killed a 6-pack just to watch it die!

  24. #24
    SitePoint Zealot trigger8's Avatar
    Join Date
    Nov 2000
    Location
    Dallas, TX
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I found the solution. Thanks anyway!
    Trigger8

    **************************
    I killed a 6-pack just to watch it die!

  25. #25
    SitePoint Zealot trigger8's Avatar
    Join Date
    Nov 2000
    Location
    Dallas, TX
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How to parse the collected information to the next page (template)?

    Let say I've selected California(state) from the first drop down menu(first page), and chose Bakersfield(city) from the second drop down menu(second page). Now I would like these information (state and city) to be parsed to the next page. I have links on the template page that are gonna be populated with the state and city info.
    Any idea?
    Trigger8

    **************************
    I killed a 6-pack just to watch it die!


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
  •