I'm trying to populate a select field with PHP. The problem is I can't figure out how to display them because I'm getting the one that's value matches in the database showing up twice because I'm echoing it as selected and then looping it all the results. How can I just display the selected one that matched the fields value and then all the ones that don't match the selected one?
TABLE CATEGORIES
cat_id cat_name
1 soccer
2 baseball
3 basketball
TABLE ARTICLES
art_id art_cat_id
1 1
PHP / HTML
<select name="category">
<?php
$sql = "SELECT cat_id cat_name, art_id, art_cat_id
FROM categories LEFT JOIN articles
ON categories.cat_id = articles.art_cat_id
WHERE art_id = 1";
$result = query($sql);
if($result===false) {
echo("Query Fail");
}
else {
?>
<option value="<?php echo $data['art_cat_id'] ?>" selected="selected"><?php echo $data['cat_name'] ?></option>
<?php
while( $data = mysqli_fetch_array($result)) {
?>
<option value="<?php echo $data['cat_id'] ?>"><?php echo $data['cat_name'] ?></option>
<?php
}
}
?>
</select>
What it's returning
<select name="category">
<option value="1" selected="selected">soccer</option>
<option value="1">soccer</option>
<option value="2">baseball</option>
<option value="3">basketball</option>
</select>
What I'm looking for
<select name="category">
<option value="1" selected="selected">soccer</option>
<option value="2">baseball</option>
<option value="3">basketball</option>
</select>
Skip the row if the value matches the first one.
A snippet:
?>
<option value="<?php echo $data['art_cat_id'] ?>" selected="selected"><?php echo $data['cat_name'] ?></option>
<?php
while( $data = mysqli_fetch_array($result)) {
if ($data['art_cat_id'] == $data['cat_id']) continue;
?>
<option value="<?php echo $data['cat_id'] ?>"><?php echo $data['cat_name'] ?></option>
<?php
}
Related
I have a categories table where I have different categories. I have also add Other in the category. When I'm getting the categories to populate the select I want the other option to appear at last. how can I do that?
<select name="category" id="category" class="form-control <?php if (isset($errors['category'])) echo 'form-error'; ?>">
<option value="">Select a category...</option>
<?php while ($category = mysqli_fetch_assoc($categories)) { ?>
<option value="<?php echo h($category['id']); ?>" <?php if (isset($_POST['category']) && h($category['id']) === $_POST['category']) echo 'selected'; ?>><?php echo h($category['name']); ?></option>
<?php } ?>
</select>
If the Id of Other category is fixed, then you have to ignore it in your database query.
Let's say that the Id of that option is 1, our query will be like this:
select * from categories where Id <> 1
This query brings all categories except "other".
After that do the following:
<select name="category" id="category" class="form-control <?php if (isset($errors['category'])) echo 'form-error'; ?>">
<option value="">Select a category...</option>
<?php while ($category =
mysqli_fetch_assoc($categories)) { ?>
<option value="<?php echo h($category['id']); ?>" <?php
if (isset($_POST['category']) && h($category['id']) ===
$_POST['category']) echo 'selected'; ?>><?php echo
h($category['name']); ?></option>
<?php } ?>
<option value="1">Other</option>
</select>
hope it helps
I want to echo the selected value from the database to update it then store it
for example I have an asset with category printers from table category which contains other categories and when I want to edit this asset on the edit page I should get a dropdown list contains all the categories and selected on printers then if I want to change it I will if not leave unchanged
The array is drop-down from table category inner joined with user_asset table in the database by asset_category as a foreign key
this is what I have done so far
<label for="basicinput">الصنف : </label>
<?php
$result = mysqli_query($conn, "SELECT * FROM category");
?>
<select name="asset_category" class="form-control" required>
<?php while( $row = mysqli_fetch_array($result)) {?>
<option value="<?php echo $row['category_id'];?>">
<?php echo $row['cate_name'];?>
</option>
<?php }?>
</select>
</div>
You can add if check if ($row['cate_name'] == 'computer') { ?> and then add selected to this option:
<label for="basicinput">الصنف : </label>
<?php
$result = mysqli_query($conn, "SELECT * FROM category");
?>
<select name="asset_category" class="form-control" required >
<?php while( $row = mysqli_fetch_array($result)) {
if ($row['cate_name'] == 'computer') { ?>
<option value="<?php echo $row['category_id'];?>" selected><?php echo $row['cate_name'];?></option>
<?php } else { ?>
<option value="<?php echo $row['category_id'];?>"><?php echo $row['cate_name'];?></option>
<?php }
}?>
</select>
Notice: If you have multiple elements with that category it will select the last one.
the answer is very simple.. let's put this code
<label for="basicinput">الصنف : </label>
<?php
$result = mysqli_query($conn, "SELECT * FROM category");
?>
<select name="asset_category" class="form-control" required>
<?php while( $row = mysqli_fetch_array($result)) {
if($row['cate_name']== printers) { ?>
<option value="<?php echo $row['category_id'];?>" selected="selected">
<?php echo $row['cate_name'];?> </option>
<?php } else { ?>
<option value="<?php echo $row['category_id'];?>">
<?php echo $row['cate_name'];?> </option>
<?php }?>
</select>
</div>
The logic is that using while loop, checking the condition using if class, and when it satisfies make it as selected. Then it will be echo as selected Value.
Hi all i am trying to display the course list in drop down depending on company id.
I have 8 courses. in that i have one course name called PEI Course(course id is 33). When i am trying to assign courses to user that course will appear only for company PEI (company id is 14) and i have another course name called ppc course(course id is 46). When i am trying to assign courses to user that course will appear only for 3 companies that is ex: a(company id is 19),b(company id is 20),c(company id is 26).
How can i do that can any one help me.
Here is my drop down Code:
<select name="courseid" id="courseid" class="form-control" required>
<?php
$sql = "select id,fullname from {course} where id NOT IN(1,33) ";
$courses = $DB->get_records_sql($sql);
?>
<option value="">Choose Course</option>
<?php
if (sizeof($courses)): foreach ($courses as $row):
$coursename = $row->fullname;
?>
<option value="<?php echo $row->id; ?>"><?php echo $row->fullname; ?></option>
<?php
endforeach;
endif;
?>
</select>
Can anyone help me how can i do that.
Thanks in advance.
I would prefer you go the database route as I stated in the comment section. You could try this in the mean time though:
$courses = [44, 52, 67];
$companyId = 14;
$pei = "<option value='33'>33</option>";
$ppc = "<option value='46'>46</option>";
?>
<select name="courseid" id="courseid" class="form-control" required>
<option value="">Choose Course</option>
<?php
if($companyId == 14){
echo $pei;
}elseif (in_array($companyId, array(19, 20, 26))) {
echo $ppc;
}
foreach ($courses as $row) {
?>
<option value="<?php echo $row; ?>"><?php echo $row; ?></option>
<?php
}
?>
</select>
Change the `$companyId` to 19 and test again.
Please modify this code so that i will get 2 option value from this code
<select name="student_class1" class="input-xlarge" >
<option value="null">--Select Class---</option>
<?php $sel_service = "select * from all_services where school_id='$school_id'";
$sel_service= mysql_query($sel_service);
while($display_class= mysql_fetch_assoc($sel_service))
{ ?>
<option value="<?php echo $display_class['sub_cat_id']; ?>"><?php echo ucfirst($display_class['sub_cat_name']); ?></option>
<?php } ?>
student_class1 giving me value sub_cat_id
i want
student_class2 will give me sub_cat_name
iwant both values by one select
plz modify it
i already spent my whole sunday on this problem
something like?
<form action="" method="get" accept-charset="utf-8">
<select name="student_class1" class="input-xlarge" >
<?php
$sel_service = "select * from all_services where school_id='$school_id'";
$sel_service= mysql_query($sel_service);
while ($row = mysql_fetch_assoc($sel_service)) {
?>
<option value="null">--Select Class---</option>
<option value="<?php echo $row['sub_cat_id']; ?>"></option>
<option value="<?php echo $row['sub_cat_name']; ?>"></option>
<?php
}
?>
</select>
</form>
This question was asked already, but my question is very simple.
In the my account page, I have the employee country in a dropdown.
How to select a value in the combo, when in edit mode?
Let's assume you have the user's country in $user_country and the list of all countries in $all_countries array:
<select id="country">
<?php
foreach ( $all_countries as $country ):
$selected = "";
if ( $country == $user_country )
$selected = "selected";
?>
<option value="<?php echo $country; ?>"
selected="<?php echo $selected; ?>">
<?php echo $country; ?>
</option>
<?php
endforeach; ?>
</select>
should work.
An option tag will be the default for a select list when the selected attribute is set. In the following code option 2 will show up as the current selected option when the page loads:
<select>
<option value="1">1</option>
<option value="2" selected="selected">2</option>
<option value="3">3</option>
</select>
To achieve this in your PHP code conditionally display the selected attribute on your options against what the current value is:
<option value="1"<?php if($user['country'] == '1') { ?> selected="selected"<?php } ?>>1</option>
<option value="2"<?php if($user['country'] == '2') { ?> selected="selected"<?php } ?>>2</option>
<option value="3"<?php if($user['country'] == '3') { ?> selected="selected"<?php } ?>>3</option>
function p_edit_combo($cCurstatus,$h_code_default,$h_name=NULL){
<select name="<?php echo $cCurstatus;?>" id="<?php echo $cCurstatus;?>" class="main_form_select">
<option value="">Select</option>
<?php
$sql_h = "SELECT h_code,h_name FROM med_hl WHERE status = 1";
$sql_h_result = mysql_query($sql_h);
while($row=mysql_fetch_array($sql_h_result)){
$h_code = $row['h_code'];
$h_name = $row['h_name'];
?>
<option <?php if($h_code_default==$h_code){ ?> selected="selected" <?php }?> value='<?php echo $h_code; ?>' >
<?php echo $h_code."|".$h_name; ?>
</option>
<?php } ?>
</select>
<?php
}
**i have two table
" users" colmns(fname,lname,...as on ohther_infomation,hobbies datatype(int))
"info" columns (id (primary_key),hobbies(varchar 200)); in which i stored for hobbies name
In my case i am storing values in from (1,2,3,4) in hobbies (int) filled of users table which i matached them through join after time of fetch them,
in my info table i stored hobbies by their name (reading, writing,playing,gyming)
$row has our users selected hobbies (int)
$rows has list of our hobbies(varchar)
edit.php i need Dropdown value selected :==== And i am Doing Like this :--- (100% Working)**
<div class="form-control">
<label for="hobbies">Hobbies</label>
<select name="hobbies">
<?php
$query = "SELECT * FROM info";
$results = mysqli_query($connect, $query);
while ($rows = mysqli_fetch_array($results)) {
?>
<option <?php if ($rows['id'] == $row['hobbies']) { ?> selected="selected" <?php } ?> value='<?php echo $rows['id']; ?>'>
<?php echo $rows['hobbies']; ?>
</option>
<?php
}
?>
</select>
<span class="text-danger"><?php if (isset($err_hobbies)) echo $err_hobbies; ?></span>
</div>