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
Related
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.
I have two tables. Enrollment and Product. I want to list Product on a <select>.
Within this <select>, I only want certain Product items to appear, whereby the condition is to read from Enrollment table's ProductID which is a foreign key to Product table.
How does one exclude certain results in a <select> that had already existed in a different table?
<?php
$sql = 'SELECT * FROM product ORDER BY ProductID ASC';
$result_select = mysql_query($sql);
$rows = array();
while($row = mysql_fetch_array($result_select))
$rows[] = $row;
echo "<div class=\"spanstyle\">Add course/product:<select name='add_product'>";
echo "<option selected>Choose here</option>";
foreach ($rows as $row) {
echo "<option value='" . $row['ProductID']."'>" . $row['ProductName']."</option>";
}
echo "</select></div>";
$select1 = $_POST['add_product'];
if (!strpos($select1, 'Choose here')) {
$sql3="INSERT into enrollment (StudentID, ProductID) VALUES ($StudentID, $select1)";
mysql_query($sql3);
}
?>
First reaction is that you need to modify your SQL query here, not the PHP loop. Something like (and this is a quick first shot so don't trust it without testing)
SELECT * FROM product WHERE ProductID NOT IN (SELECT ProductID from enrollments) ORDER BY ProductID ASC
This would exclude any row in product whose ProductID also appears in the enrollments table.
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..
I have mySQL DB where I have this: link
I need to return result where first item will be item with id==1 and then add to that everything else in alphabetical order. This is what I want:
[Názov predmetu], [Algoritmizácia úloh], [Fyzika 1]...
Now Im using this:
<?php
$result = mysql_query("SELECT nazov FROM tbPredmety ORDER BY nazov ASC");
echo "<select name='subject_name'>";
while ($row = mysql_fetch_assoc($result))
{
echo "<option value = '" . $row[nazov] . "'>" . $row[nazov] . "</option>";
}
echo "</select>";
?>
You can add a CASE clause inside your ORDER BY clause:
SELECT nazov
FROM tbPredmety
ORDER BY
-- item(s) with item_id 1 will appear first
CASE WHEN item_id = 1 THEN 0 ELSE 1 END,
nazov ASC
I used THEN 0 ELSE 1 but for your purpose you could use any pair of values in which the former value is considered less than the latter value when sorting.
EDIT: For mysql you may need to use CASE ... END CASE. If the above does not work try changing END to END CASE.
I am trying to get data of 'indication' from two different tables.
The script works fine when selecting FROM number_one only.
Did try this with a , inbetween but that doesn't work.
How should I do this?
query2 = mysql_query("SELECT `indication` FROM `number_one`, `number_two` ORDER BY `indication` DESC");
while($row2 = mysql_fetch_object($query2)){
if($explode2[1] == $row2->indication){
echo "<option value=\"$row2->indication\" selected=\"selected\">$row2->indication</option>";
}
else{
echo "<option value=\"$row2->indication\">$row2->indication</option>";
}
}
Solution
query2 = mysql_query("SELECT `indication` FROM `number_one` UNION ALL SELECT `indication` FROM `number_two` ORDER BY `indication` DESC");
while($row2 = mysql_fetch_object($query2)){
if($explode2[1] == $row2->indication){
echo "<option value=\"$row2->indication\" selected=\"selected\">$row2->indication</option>";
}
else{
echo "<option value=\"$row2->indication\">$row2->indication</option>";
}
}
Assuming you have two tables, number_one and number_two with the indication column, your query would cross-join the two tables, giving you a set of rows which each have two columns (number_one.indication and number_two.indication, in all the possible combinations.
If I understand the question correctly, you want to have them as one column, with all the values from both tables. This can be done using the UNION ALL set operator, as I demonstrate below:
SELECT indication
FROM ( SELECT indication
FROM number_one
UNION ALL
SELECT indication
FROM number_two)
ORDER BY 1 DESC