How to make USA first in my drop down menu? - php

I just simply wanted to know how I could make United States appear as the first option in my drop down menu. I currently have a country drop down menu that's in order by name. If there is a way I can just get USA to appear first and not repeat that would be greatly appreciated.
This is the code for the drop down menu
$sql=mysql_query("SELECT * from country order by name");
PS: I know MySQL is outdated or what ever... This is all just back end stuff.

Option 1 (recommended) Make the US the first result in your result set
SELECT *
FROM country
ORDER BY case when name = 'USA' then 1 else 2 end,
name ASC
Option 2: Don't select it in your query and display it first (hardcode it in)
$sql=mysql_query("SELECT * from country where name != 'USA' order by name");
Option 3: Display it first (hardcode it in) and then skip in while iterating through your resultset
if ($row['name'] === 'USA') {
continue;
}

You can use UNION:
SELECT * FROM country WHERE name = 'USA'
UNION SELECT * FROM country WHERE name != 'USA'

Option 1
<select>
<option value="1">USA</option>
<?php
$query = "SELECT * from country where name != 'USA' order by name";
$result = mysqli_query($bd,$query);
while ($country = mysqli_fetch_assoc($result)) {
echo "<option value='".$country['cod']."'>".$country['name']."</option>";
}
?>
</select>
Option 2
<select>
<?php
$query = $query = "SELECT * from country where name != 'USA' order by name"; $result = mysqli_query($bd,$query);
while ($country = mysqli_fetch_assoc($result)) { ?>
<option value="<?php echo $country['cod']; ?>" <?php if ($country['name']=='USA') {echo SELECTED;}?>><?php echo $country['name']; ?></option>";
<?php } ?>
</select>

ORDER BY (country_name = 'USA') DESC, country_name ASC
Here country_name as field in country table
Add this above line to your query..Then onLoad USA selected in drop down..

Related

Get selected values in multiselect form field

I'm updating a PHP form field from a dropdown (single value selected) to a multiselect (multiple values selected).
I have 2 DB tables, one where the complete list of team members reside, and the other where the selected team members reside.
On the PHP page, I want to get the values from the full team members table and show the full list as a multiselect form field.
Then I want to be able to get the values from the selected team member table and have them show as selected options in the full multiselect list mentioned above.
Any idea on how I would accomplish this?
Here's my code, although right now it just returns the full team member list without the selected values.
$query = "SELECT walkername FROM Team_Management WHERE active=1 ORDER BY walkername ASC";
$stmt = $mysqli->prepare($query) or die ("Couldn't execute query: ".mysqli_error($mysqli));
$stmt->execute();
$stmt->bind_result($walkers);
echo "<div class='form-group'>";
echo "<label class='col-lg-3 control-label' for='Walkers'>Walkers:</label>";
echo "<div class='col-lg-5'>";
echo "<select multiple class='form-control' name='walkers[]' id='Walkers'>";
while ($stmt->fetch()) {
echo "<option value='$walkers'>$walkers</option>";
}
echo "</select>";
echo "</div>";
echo "</div>";
$stmt->close();
** UPDATED **
So one thing that I should've added, is that the SELECTED_TEAM_MEMBERS field is a comma separated field.
TEAM_MANAGEMENT table
id || walkername
1 || John
2 || Kate
SELECTED_TEAM_MEMBER table
cid || walkers
1 | John,Ray,Kate
2 | Kate,Matt,Joe
In addition, each group in the walkers field in the SELECTED_TEAM_MEMBER table is tied to a unique client id (cid).
So how can I identify the selected walkers from the complete list in the TEAM_MANAGEMENT table by unique client id.
You can get the list of your selected user with a boolean in your SQL request
SELECT walkername,
CASE WHEN **selected_team_members_name** > '' THEN '1' ELSE '0' END as is_selected
FROM Team_Management
LEFT OUTER JOIN **SELECTED_TEAM_MEMBERS** ON **selected_team_members_name** = walkername
WHERE active=1
ORDER BY walkername ASC
With selected_team_members_name the name of the column in your table and SELECTED_TEAM_MEMBERS the name of your table
And now $walker is as array with 2 key: walkername and is_selected
And after you can try a if to put them the attribute 'selected' when you write your 'option' tag
while ($stmt->fetch())
{
$selected = "";
if($walkers['is_selected'] == '1'){ //If your walker is selected
$selected = "selected";
}
echo "<option ".$selected." value=".$walkers['walkername'].">".$walkers['walkername']."</option>";
}
I may not have understand the context but i hope i helped you.

How to get value using single field with multiple id?

->When i call this code using ajax,it's not work proper
->Result is only for one category other category result not display,how can i solve this?
$city_id=$_GET['id'];
//Get Area
$area_qry="SELECT * FROM tbl_area where city_id IN ('".$city_id."') ";
$area_res=mysql_query($area_qry);
echo "<option value=''>-- Select City Area --</option>";
while($area_row=mysql_fetch_array($area_res)){
echo "<option value='".$area_row['area_id']."'>".$area_row['area_name']."</option>";
}
Solve myself,Here is updated code
$city_id=$_GET['id'];
//Get Area
$area_qry="SELECT * FROM tbl_area where city_id IN ($city_id) ";
$area_res=mysql_query($area_qry);
echo "<option value=''>-- Select City Area --</option>";
while($area_row=mysql_fetch_array($area_res)){
echo "<option value='".$area_row['area_id']."'>".$area_row['area_name']."</option>";
}

How to display specific value from SQL in a dropdown depending on another condition

I need some help for a problem that I have been stuck with for a while.
I have a list with ads showing vehicles of different categories - cars, motorcycles, trucks, boats etc. I have a dropdown that should work as a filter. Depending on what vehicles are in the list, the dropdown values should be equal to the category of the vehicle.
For example, if in the list there are 3 cars: Audi, BMW and Porsche and 2 boats: Cranchi and Azimut, the dropdown should show 5 values: Audi, BMW, Porsche, Cranchi, Azimut.
However, for the example above i only manage to show in the dropdown all car and boat makes from the DB, but not only the relevant to the list entries.
Here is my code, any ideas will be appreciated:
<option value="">Please Select</option>
<?php
if($all=='ALL')
{
foreach($arrVehicleType as $arrVehicleTypes){
$qry_makes_all = "SELECT * FROM ".$tblprefix."makes WHERE status ='1' AND type = '".$arrVehicleTypes."' ORDER BY title ASC";
$rs_makes_all = $db->Execute($qry_makes_all);
while(!$rs_makes_all->EOF){
$sql_query_all_all = "SELECT type FROM ".$tblprefix."vehicles WHERE user_dealer_id = '".$DealeId."' AND make='".$rs_makes_all->fields['id']."' AND user_dealer_type = 2 AND completed_status = 1 AND status = 1 ";
$rs_results_all_all = $db -> Execute($sql_query_all_all);
$TYPE = $rs_results_all_all -> fields['type'];
?>
<option value="<?php echo $rs_makes_all->fields['id'];?>,<?php echo $rs_makes_all->fields['title']." ($vehicletext)";?>
</option>
<?php $rs_makes_all->MoveNext(); } }} else {
while(!$rs_makes->EOF){?>
<option value="<?php echo $rs_makes->fields['id'];?>,,<?php echo $arrVehicleType[0];?>">
<?php echo $rs_makes->fields['title'];?>
</option>
<?php
$rs_makes->MoveNext(); } } ?>
</select>
Use DISTINCT in Mysql.
$qry_makes_all = "SELECT DISTINCT /* list column names */ FROM ".$tblprefix."makes WHERE status ='1' AND type = '".$arrVehicleTypes."' ORDER BY title ASC";
This will select all the distinct values from your table.

php How to sort and brake while loop based on variable

I have a problem ... I can not solve it for two days. I am a beginner in php.
i don't understand why while loop returns me as much selects fields as every select field has options, for example field "choose color" has 3 options, red green and yellow and i get instead one select field 3 of them... see this http://5dstudio.eu/select.jpg
structure of my data base looks like this:
http://5dstudio.eu/data.jpg
my php code:
<?php
$sql = mysql_query("SELECT qty FROM attributes ORDER BY qty ");
while($row = mysql_fetch_array($sql)){
$name_attribute = $row["qty"];
$num = (int)$name_attribute;
echo "<select>";
$sql2 = mysql_query("SELECT name FROM attributes WHERE qty='$name_attribute' ORDER BY qty");
while($row2 = mysql_fetch_array($sql2)){
$value_attribute = $row2["name"];
echo '<option>' ."$value_attribute". '</option>';
}
echo "</select>";
}
?>
Thanks for any tips and help!
Change your first query to this:
SELECT qty FROM attributes GROUP BY qty
or
SELECT DISTINCT qty FROM attributes

How to echo previously selected option in drop down first?

I am using the follow bit below. I am first querying the db to see all available options but this is for an edit form so i want the drop down to show the previously selected value so i did another query and got the selection. When i do it the way i have it below it will keep repeating the previously selected selection after each available option. HOw to fix this?
<option>Select Sales rep</option>
<?php
$query="select agent_id, agent_name from agent_names where agent_id='$ad'
order by agent_name asc";
$result=mysql_query($query);
while(list($agent_id, $agent_name)=mysql_fetch_row($result)) {
echo "<option value=\"".$previousname."\">".$previousselection."</option>
<option value=\"".$agent_id."\">".$agent_name."</option>";
}
?>
Just check if $agent_id equals $previousname (perhaps you mean $previousid?) and echo selected="selected" if so:
while(list($agent_id, $agent_name)=mysql_fetch_row($result)) {
$selected = $agent_id == $previousname;
echo "<option " . ($selected ? "selected=\"selected\"" : "") . " value=\"".$agent_id."\">".$agent_name."</option>";
}
Another option is to output the previous selected item before your while loop, and exclude it in your sql query.
you should take the previos selection out of the while.. like this:
<?php
$query="select agent_id, agent_name from agent_names where agent_id='$ad'
order by agent_name asc";
$result=mysql_query($query);
echo "<option value=\"".$previousname."\">".$previousselection."</option>";
while(list($agent_id, $agent_name)=mysql_fetch_row($result)) {
echo "<option value=\"".$agent_id."\">".$agent_name."</option>";
}
?>
and maybe even add an if so it wont repeat it self:
$query="select agent_id, agent_name from agent_names where agent_id='$ad'
order by agent_name asc";
$result=mysql_query($query);
echo "<option value=\"".$previousname."\">".$previousselection."</option>";
while(list($agent_id, $agent_name)=mysql_fetch_row($result)) {
if ($agent_id != $previousname) {
echo "<option value=\"".$agent_id."\">".$agent_name."</option>";
}
}
?>
You can force MySQL to return your desired result first in the list like this:
$query = "SELECT agent_id, agent_name FROM agent_names WHERE agent_id='$ad'
ORDER BY agent_id = '{$previousname}' DESC, agent_name ASC";
This tells MySQL first to sort by agent_id matching previous selection (i.e. it will be 1 for the previously selected record and 0 for all others, hence sorting by it DESC makes it first in the list. And since all others will have this fields equal to 0, they will be sorted by second field, which is agent_name ASC

Categories