How to get value using single field with multiple id? - php

->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>";
}

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 make USA first in my drop down menu?

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..

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

output sql query in php

I'm a noob at php so I need a bit of help. I have a table with a list of hotels, the columns are
name | address | city | stars | price
so far I've made a dynamic drop-down menu where the user selects a city from the table, this is shown in the code below:
<?php
connection stuff blah blah
?>
<?php
$sql = "SELECT city FROM hotels";
$result_db = $db->query($sql);
if (!$result_db) {
echo $db->error . ' Error perform query!';
} else {
echo '<select name="value">';
echo '<option value="">-Select from below-</option>';
while ($row = $result_db->fetch_object()) {
echo '<option city="' . $row->city . '">';
echo $row->city;
echo '</option>';
}
echo '</select>';
}
?>
How would I now create a table where all the details of hotels in the selected city are outputted? I'm assuming I need another sql query, arrays (perhaps?) which then I echo into a table? :S But I have no idea how to.....
you need to use jquery to add event to your dropdown second in your table you should have an ID field..
You're table should be
Id | name | address | city | stars | price
next you need to create a query that will fetch the id and name fields and put it on a dropdown.
next you need to use jquery to add event to dropdown.
$(function() {
$("#hotel").change(function() {
});
})

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