Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8428317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:01:11+00:00 2026-06-10T05:01:11+00:00

Possible Duplicate: PHP – PRE-select drop down option I have a situation in my

  • 0

Possible Duplicate:
PHP – PRE-select drop down option

I have a situation in my php project where a user can edit his account.

In editing, there is a city field which is a drop down list of cities.

I want to already select one city which is in profile before editing(the one which user enter at the time of registration).

Also he is able to change his city by choosing fron drop down list.

Html code:

<div class="search_bar1_txt">State:</div>
<div class="search_bar1">
<select class="styled" name="state_trainer">
 <option>-select-</option>
 <option>washington</option>
 <option>perth</option>
 <option>delhi</option>
 <option>london</option>
 </select></div>
</div>

on editing I am using this code to fetch current data(city) of user:

<?php
if(isset($_GET['userid']))
{
$sql = "select city from `wp_pelleresuser` where userId =".$_GET['userid'];
$result = mysql_query($sql);
$value = mysql_fetch_assoc($result);
}?>

please tell me how can I get already selected one option which is fetch from database.

And also it is changeable.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T05:01:13+00:00Added an answer on June 10, 2026 at 5:01 am

    You can do it in a number of ways, the simplest (and probably least elegant) is to do something like this:

    <select class="styled" name="state_trainer">
    <?php
    $myCity='london'; // assumed to be data from database...
    echo '<option'.($myCity=='-select-') ? ' selected ' : ' ' .'-select-</option>';
    echo '<option'.($myCity=='washington') ? ' selected ' : ' ' .'washington</option>';
    ?>
    

    This is of course horrid.

    I would rather suggest that you check the data as you are pulling your information out of the database and putting your initial dropdown list together.

    If you are making an array of data to create the drop down list (for example) check it right there and then. If the city matches what you want, do it inside your loop right off the bat.

    $usersCity="london";
    $myCityList=array();
    
    while( ... ) // Database loop that is pulling the data from the database.
    {
        $selected='';
        if($userCity==$row['city'])
        {
            $selected=' selected ';
        }
        $myCityList[]='<option'.$selected.'>'.$row['city'].'</option>';
    }
    

    Then to display the drop down list, you can simply do this:

    $cityCount=count($myCityList);
    for($i=0;$i<$cityCount;$i++)
    {
        echo $myCityList[$i].'\n';
    }
    

    The users city will already be selected.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: PHP - send file to user I have written a script that
Possible Duplicate: PHP Get end string on url between / and / I have
Possible Duplicate: php false place in condition I have noticed that a lot of
Possible Duplicate: php loop through associative arrays i have a variable which prints an
Possible Duplicate: php loop through associative arrays i have this piece of code which
Possible Duplicate: PHP code is not being executed (i can see it on source
Possible Duplicate: php mail() function on localhost I am using php mail(). I have
Possible Duplicate: PHP: How to remove specific element from an array? How can i
Possible Duplicate: php regex <b> to <b> Inside my admin panel I have a
Possible Duplicate: PHP 2-way encryption: I need to store passwords that can be retrieved

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.