SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: dropdown query

  1. #1
    SitePoint Member
    Join Date
    Nov 2009
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    dropdown query

    i have two dropdown boxes one for state n one for city..

    i wnt tht wen i select the state the city dropdown should show the cities of tht state only..

    i have same code for add an update plz help!

    <?
    $get_type = dbQuery("SELECT state_id, state_title as st FROM india_states ORDER BY state_title");
    if (dbNumRows($get_type) > 0) {
    echo "<select name='state_title' class='box' style='width:210px;' onchange='setAndReset(this);'>";
    echo "<option value=''>Select Type</option>";
    while($row_type = dbFetchAssoc($get_type)) {

    extract($row_type);
    echo "<option value='$state_id'";
    if ($state_title && $state_title==$state_id) {
    echo "selected";
    }
    echo ">$st</option>";
    }
    echo "</select>";
    }
    ?>


    <?
    $get_type = dbQuery("SELECT city_id, city_title as ct FROM india_cities ORDER BY city_title");
    if (dbNumRows($get_type) > 0) {
    echo "<select name='city_title' class='box' style='width:210px;'>";
    echo "<option value=''>Select Type</option>";
    while($row_type = dbFetchAssoc($get_type)) {

    extract($row_type);
    echo "<option value='$city_id'";
    if ($city_title && $city_title==$city_id) {
    echo "selected";
    }
    echo ">$ct</option>";
    }
    echo "</select>";
    }
    ?>

  2. #2
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Change this line:
    PHP Code:
    echo "<select name='state_title' class='box' style='width:210px;' onchange='setAndReset(this);'>"
    PHP Code:
    echo "<select name='state_title' class='box' style='width:210px;' onchange='setAndReset(this.value);'>"
    And your javascript function setAndReset(state) will be like this:
    Code javascript:
    function setAndReset(state){
     	if(state != ''){
     		document.location = 'samepage.php?state=' + state;
     	}
    }

    And the sql query for selecting cities would look like this:
    PHP Code:
    if(isset($_GET['state']) && !empty($_GET['state'])){
        
    $get_type dbQuery("SELECT city_id, city_title as ct FROM india_cities WHERE state=" mysql_real_escape_string($_GET['state']) . " ORDER BY city_title");
        if (
    dbNumRows($get_type) > 0) {
            echo 
    "<select name='city_title' class='box' style='width:210px;'>";
            echo 
    "<option value=''>Select Type</option>";
            while(
    $row_type dbFetchAssoc($get_type)) {
                
    extract($row_type);
                echo 
    "<option value='$city_id'";
                if (
    $city_title && $city_title==$city_id) {
                    echo 
    "selected";
                }
                echo 
    ">$ct</option>";
            }
            echo 
    "</select>";
        }
    }
    else{
        echo 
    "<select name='city_title' class='box' style='width:210px;'>";
        echo 
    "<option value=''>Select Type</option>";
        echo 
    "</select>";

    I assume that you have a state field in your india_cities table.

    Be informed that this will refreshes the page when selecting a state. If you don't want the page to be reloaded then either you should use AJAX or you should print all the possible cities in a JavaScript array and do the needful accordingly when a state is selected.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  3. #3
    SitePoint Member
    Join Date
    Nov 2009
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that doesnt work..in the edit part it doesnt post the city value..

  4. #4
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok do something like this then:
    PHP Code:
    $state '';
    if(isset(
    $_GET['state']) && !empty($_GET['state'])){
        
    $state $_GET['state'];
    }
    else if(isset(
    $row_edit['state']) && !empty($row_edit['state'])){
        
    $state $row_edit['state'];
    }
    if(!empty(
    $state)){
        
    $get_type dbQuery("SELECT city_id, city_title as ct FROM india_cities WHERE state=" mysql_real_escape_string($state) . " ORDER BY city_title");
        if (
    dbNumRows($get_type) > 0) {
            echo 
    "<select name='city_title' class='box' style='width:210px;'>";
            echo 
    "<option value=''>Select Type</option>";
            while(
    $row_type dbFetchAssoc($get_type)) {
                
    extract($row_type);
                echo 
    "<option value='$city_id'";
                if (
    $city_title && $city_title==$city_id) {
                    echo 
    "selected";
                }
                echo 
    ">$ct</option>";
            }
            echo 
    "</select>";
        }
    }
    else{
        echo 
    "<select name='city_title' class='box' style='width:210px;'>";
        echo 
    "<option value=''>Select Type</option>";
        echo 
    "</select>";

    Where your state value/id will be in the $row_edit['state'] array variable. Change it accordingly.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  5. #5
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,194
    Mentioned
    17 Post(s)
    Tagged
    5 Thread(s)
    Nowhere in the code you posted do you ever reference a foreign key in the cities table that would relate a city to a state. That will be needed in order to achieve what you would like. Is that foreign keys name state_id by chance?

    With that said, the query isn't functioning the way you would like because the below code selects all cities.

    Code SQL:
    SELECT city_id, city_title AS ct FROM india_cities ORDER BY city_title

    Your desired functionality will require the addition of a where clause to filter the state. For example, if the state with state_id of 2 was selected then your query should follow the below structure.

    Code SQL:
    SELECT city_id, city_title AS ct FROM india_cities WHERE state_id = 2 ORDER BY city_title;

    Thus, the query within PHP would look similar to the below.

    PHP Code:
    $get_type dbQuery("SELECT city_id, city_title as ct FROM india_cities WHERE state_id = ".mysql_real_escape_string($state_id)." ORDER BY city_title"); 


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
  •