how do i get dropdown selected for each user.
user table
------------
id job
1 1
2 2
job table
----------
id name
1 Doctor
2 Sales
$q = $db->query("SELECT * FROM affiliate LEFT JOIN user ON user.job = affiliate.id_affiliate");
while($r = $q->fetch_array()) :
if($r['id_user'] == $_SESSION['id_user'] && $r['job'] == $r['id_affiliate']) {
echo '<option selected value="'.$r['id_affiliate'].'">'.$r['org'].'</option>';
} else {
echo '<option value="'.$r['id_affiliate'].'">'.$r['org'].'</option>';
}
endwhile;
selected="selected" or just selected should normally work. If not there is a problem with your if statement. One simple way is to echo out the content of the if statement like this:
note!! the echo should normally be done outside the select open tag, just paste the following outside the select open tag but just after your query.
while($r = $q->fetch_array()) :
echo $r['id_user'] .'=='. $_SESSION['id_user'] .'&&'. $r['job'] .'== '.$r['id_affiliate'].'<br />';
endwhile;
you can now check if the values actually match. if not then there is your problem.
How about modifying the following ...
if( ($r['id_user'] == $_SESSION['id_user']) && ($r['job'] == $r['id_affiliate']) )
Not sure if it matters, but I have selected at the end of my option.
<option value='cat' selected>
Related
I have a task that I need to retrieve data from the database and set it in the Combo Box. Fortunately, I have done it.
Now, I have a Search Button which retrieves the data relevant in these text and combo boxes. My Issue is, After I click Search Button all my combo box and text box selected values become empty. How can I set those same data after clicking Search button ?
My Code Effort is,
<?php
$sql="select cat_id,cat_name from disease_category group by cat_id ";
foreach ($dbo->query($sql) as $row){
if(isset($_REQUEST['cat_name'])&&$_REQUEST['cat_name']==$row[cat_name])
{
echo "<option value=$row[cat_id] selected='selected' >$row[cat_name]</option>";
}
Else
{
echo "<option value=$row[cat_id]>$row[cat_name]</option>";
}
}
?>
My SEARCH button code,
<?php
include 'config.php';
if(isset($_REQUEST['SUBMIT']))
{
$cat=$_REQUEST['cat'];
$subcat=$REQUEST['subcat']
$sel=mysql_query("SELECT * from table_name where cat_id like '$cat%' AND sub_id like '$sub_cat%'AND survier like '$survier%' ")
}
It should be pretty simple. I still don't fully understand what you're trying to do. But if all you want is to dynamically populate an options list based on the results of a SQL query.
<?php
$sql = '
SELECT
*
FROM
`my_table`
';
$query = mysql_query($sql) OR die(mysql_error());
print '<select name="dropdown">';
while ($row = mysql_fetch_assoc($query)) {
print '<option value="'. $row['cat_id'] .'"';
if (
isset($_REQUEST['cat_name']) &&
$_REQUEST['cat_name'] == $row['cat_name']
) { print ' selected="selected"'; }
print '>'. $row['cat_name'] .'</option>';
}
print '</select>';
?>
You should be able to modify the SELECT query to fit your needs, and modify content within the while() loop, as well. That should get you going if I understand what you're trying to do.
I'm pulling rows from a mysql database to fill a drop down box. It works, but it misses out one option. I think it might be due to the fact I'm using the <optgroup> tag in the middle, where the option would be - in fact if I remove this, the whole list is printed.
My database has data similar to below:
Module Code|Module Name
-----------|-------------
SE1AA11 |Animal Acting
SE1BB11 |Boring Billiards
... | ...
SE2AA11 |Animal Archery
SE2BB11 |Boring Boxes
... | ...
(... indicates more data and yes, the module names are made up)
When the page loads, it misses out the first of the SE2 options. Any ideas why? Any help would be appreciated. Code below:
<select name="module1" id="module1" style="display: none;">
<option value="" selected disabled>Module 1</option>
<?php
$sql = "SELECT * FROM Modules";
$result=mysqli_query($con,$sql);
echo '<optgroup label="Part One">';
while (($row = mysqli_fetch_array($result)) &&
(substr($row['ModuleCode'], 0, -4) == "SE1")){
$code = $row['ModuleCode'];
$name = $row['ModuleName'];
echo '<option value="'.$code.'">' . $name . '</option>';
}
echo '</optgroup>';
echo '<optgroup label="Part Two">';
while (($row = mysqli_fetch_array($result)) &&
(substr($row['ModuleCode'], 0, -4) == "SE2")){
$code = $row['ModuleCode'];
$name = $row['ModuleName'];
echo '<option value="'.$code.'">' . $name . '</option>';
}
echo '</optgroup>';
?>
</select>
You miss out one record because of this: when your first while loop has iterated over all SE1* records, it does another call to mysqli_fetch_array() which fetches the next record from your result set. This record does not comply with your second condition (it does not start with 'SE1') so PHP moves to the next while loop, where another call is made to mysqli_fetch_array() which will fetch the next record from your result set.
Because your first 'SE2*' item was already fetched by the first loop, but never processed by that loop, you will not see that record back in your dropdown list.
I have not found a solution to prevent this. I am using PHP to self populate my option fields so I don't have to manually add them all in. I have four select fields and three out of the four work perfect but the fourth one.
On the company field it will populate one blank field and then it populates the rest just fine. I have a solution for this that will remove this space but I was informed that it is not the best practice. Anyone think they know a better and more correct way of achieving this?
PHP Select Field
<label for="company">Company</label><br />
<select id="company" name="users" onChange="showUser(this.value)">
<?php include 'datalogin.php'; // Populates the Company select field
$result = mysqli_query($con, "SELECT DISTINCT Company FROM `roster` ORDER BY Company ASC;");
echo '<option value="">' . 'Select a Company' .'</option>';
while ($row = mysqli_fetch_array($result)) {
if ($row['Company'] == NULL) { //The empty if is to remove the blank space in the select field
}
else {
echo '<option value="'.urlencode($row['Company']).'">'.htmlspecialchars($row['Company'],ENT_QUOTES).'</option>';
}
}
?>
</select>
Ideally I was told you should use "!" to tell the if statement to echo if it is not null but it still produces the blank space:
if ($row['Company'] !== NULL) {
echo '<option value="'.urlencode($row['Company']).'">'.htmlspecialchars($row['Company'],ENT_QUOTES).'</option>';
}
This is what I mean by the blank space:
(Pretend it is a select field)
[Select a field]
[ ]
[Cleveland ]
[Columbus ]
[Toldeo ]
Live Agent Search Site
SELECT DISTINCT Company
FROM `roster`
WHERE LENGTH(Company) > 0
ORDER BY Company ASC;
I have a select box with three options. When an option is selected a save button is then click which performs a database update on the field. This will involve having two submit buttons in one form. I have created a different action on each submit button, this will be the save button:
if ($_POST['update_status'] == 'Save') {
$keys = array_keys($_POST['order_status']);
//perform the database update to change the option value
}
if(isset($_POST['order_selected'])) {
//send email
}
At the moment my select box for each option is embedded in a table like:
echo '<select name="order_status[] id="id"">';
echo '<option value = "Pending" name="order_status['.$i.']" class = "pending"' . ($row['status'] == 'Pending' ? ' selected=selected' : '') . '>Pending</option>';
echo '<option value = "Approved" name="order_status['.$i.']" class = "approved"' . ($row['status'] == 'Approved' ? ' selected=selected' : '') . '>Approved</option>';
echo '<option value = "Disapproved" name="order_status['.$i.']" class ="disapproved"' . ($row['status'] == 'Disapproved' ? ' selected=selected' : '') . '>Disapproved</option>';
echo '</select>';
now at the moment I have the update query working, except it is outside of the form and works by posting a unique order I.D from the database and then performing he update. however I can only do 'approved'. 'pending' is not an issue as I have already set a flag in the database when an order is created, as default pending.
Instead of have this functionality outside of the form, I would like to be apple to select a dropdown item, hit save, and then the database update query is run changing the value in the select box, within the same form, if this would be possible?.
at the moment I have the query and submit button outside of the form which looks like:
<?php
if (!empty($_POST)) {
$id = intval($_POST['approval']);
$query = "UPDATE Orders SET status = 'Approved' WHERE id = $id";
mysql_query($query);
}
$query = "SELECT ID, Orderno, status FROM Orders WHERE status = '0'";
$result = mysql_query($query);
?>
<select name="approval">
<?php while ($row = mysql_fetch_assoc($result)): ?>
<option value="<?php echo $row['ID']; ?>"><?php echo $row['Orderno']; ?></option>
<?php endwhile; ?>
</select>
<input type="submit" action ="" value="Approve Order" />
Many Thanks, any pointers on how I should be tackling this are much appreciated.
I'm pretty confused by your question, but if you are trying to have the database query run without having the user leave the page, and then update the attributes of some of your HTML elements, you are going to have to use AJAX.
this is my code which populates a drop down menu, all working perfectly, but when editing a database record, i want the first value in the drop down to be what is currently in the database, how would i do this?
<li class="odd"><label class="field-title">Background <em>*</em>:</label> <label><select class="txtbox-middle" name="background" />
<?php
$bgResult = mysql_query("SELECT * FROM `backgrounds`");
while($bgRow = mysql_fetch_array($bgResult)){
echo '<option value="'.$bgRow['name'].'">'.$bgRow['name'].'</option>';
}
?>
</select></li>
You would set the selected="selected" attribute on the relevant <option>. Presumably there would be some sort of check in your while loop, checking against the variable that contains the current value.
You can do like:
$counter = 1;
while($bgRow = mysql_fetch_array($bgResult)){
if ($counter === 1)
{
echo '<option value="'.$bgRow['name'].'" selected="selected">'.$bgRow['name'].'</option>';
}
else
{
echo '<option value="'.$bgRow['name'].'">'.$bgRow['name'].'</option>';
}
$counter++;
}
As can be seen I have added selected="selected" for you so it will work automatically for you :)
Correct me if I'm wrong, I believe that you have another database table which holds the selected background (e.g. users table with background field), you need to do a query to get the background from the other table and add selected="selected" attribute to the option tag of the background, please check the code below (hope it helps):
<?php
$result = mysql_query("SELECT `background` FROM `users` LIMIT 1");
$myBg = mysql_fetch_array($result, MYSQL_ASSOC);
$bgResult = mysql_query("SELECT * FROM `backgrounds`");
while($bgRow = mysql_fetch_array($bgResult)){
if($myBg['background'] == $bgRow['name'])
echo '<option value="'.$bgRow['name'].'" selected="selected">'.$bgRow['name'].'</option>';
else
echo '<option value="'.$bgRow['name'].'">'.$bgRow['name'].'</option>';
}
?>