set multiple select list options to selected from query - php

I have a field named 'majors' which contains a string of integers seperated by commas. This field is populated from a multiple select list on insert query using the following variable:
$majors_string = implode(",", $majors);
Now on the edit page, I want to have those values set the options in the same list. Part of the problem is that the list is generated dynamically using:
<select name="majors[]" size="9" multiple="multiple" id="majors">
<?php
$query = mysql_query("SELECT * from major ORDER BY title ASC");
for($i=0;$i<mysql_num_rows($query);$i++) {
$row=mysql_fetch_assoc($query);
?>
<option value="<?php echo $row['major_pk']; ?>"><?php echo $row['title']; ?></option>
<?php
}
?>
</select>
On the initial query on the edit page, I have:
$query_topic = "SELECT * FROM topic WHERE topic_pk = '$topic_pk'";
$result_topic = mysql_query($query_topic, $connection) or die(mysql_error());
$row_topic = mysql_fetch_assoc($result_topic);
$retrieved_majors = explode(",", $row_topic['majors']);
But I'm unsure as to set the options in the multiple list to the values in array $retrieved_majors.
Thanks for any help!

I think you're asking how to set the default for which options are selected, right?
<option value="<?php echo $row['major_pk']; ?>" <?php if(array_search($row['major_pk'], $retrieved_majors) !== false) {echo 'selected';} ?> ><?php echo $row['title']; ?></option>

Related

Duple select from database in same table but diffent proposes

First option of select must be the name referring to the ID. The remaining select options are the remaining names
<select class="input" name="client_id">
<?php
$sel_client_detail="Select * from client WHERE client_id=".$id."";
$result_detail = mysqli_query($con,$sel_client_detail);
while($new_record_row = mysqli_fetch_assoc($result_detail)) { ?>
<option selected><?php echo $row['nome'];?></option>
<?php };?>
<?php
$sel_client="Select * from client";
$result = mysqli_query($con,$sel_client);
?>
<option>-----------</option>
<?php while($new_record_row = mysqli_fetch_assoc($result)) { ?>
<option><?php echo $new_record_row['nome'];?></option>
<?php };?>
</select>
Output:
<select>
<option selected> Izzi (current ID name)</option>
<option> ____________</option>
<option> Other existing clients</option>
<option> Other existing clients</option>
<option> Other existing clients</option>
<option> Other existing clients</option>
</select>
If you want the user to be first in your option list just run the query once and build the HTML parts in 2 seperate strings. Then once the loop is complete put them together and echo them
<?php
echo '<select class="input" name="client_id">';
$itsme = '';
$others = '<option>-----------</option>';
$sql = "Select * from client";
$result = $con->query($sql);
while($row = $result->fetch_assoc()){
if ( $id == $row['id'] ) {
$itsme = "<option selected='selected'>$new_record_row[nome]</option>";
} else {
$others += "<option>$new_record_row[nome]</option>";
}
}
// put the option tags together in the order you specified
echo $itsme . $others . '</select>';
Here's a different, but more conventional, approach to this common scenario:
Why not just make the chosen ID selected when you get to it in the list? Then it will still show to the user first. It's more efficient than having two separate queries.
Like this:
<select class="input" name="client_id">
<?php
$sel_client="Select * from client";
$result = mysqli_query($con,$sel_client);
?>
<option>-----------</option>
<?php while($new_record_row = mysqli_fetch_assoc($result)) { ?>
<option <?php echo ($new_record_row["client_id"] == $id ? "selected": ""); ?> ><?php echo $new_record_row['nome'];?></option>
<?php };?>
</select>

Retain selected option after form submitted

I have the following code that get some options from database using php and mysql
<select class="form-control" id="Type" name="Type">
<option></option>
<?php
$TypeQuery = "SELECT DISTINCT Type FROM Details";
$TypeQueryExecute = mysqli_query($conn, $TypeQuery);
while($TypeQueryRow = mysqli_fetch_array($TypeQueryExecute)){
$Type = $TypeQueryRow['Type'];
echo "<option value='{$Type}'>{$Type}</option>";
}
?>
</select>
I want to retain the dropdown list selection after form submission. I am using php $_POST[''] method for form submission. I tried the following way. But it is not working.
<select class="form-control" id="Type" name="Type">
<option></option>
<?php
$TypeQuery = "SELECT DISTINCT Type FROM oaDetails";
$TypeQueryExecute = mysqli_query($conn, $TypeQuery);
while($TypeQueryRow = mysqli_fetch_array($TypeQueryExecute)){
$Type = $TypeQueryRow['Type'];
?>
<option <?php if ($_POST['Type']==$Type) echo 'selected="selected"'; ?> >
<?php echo $Type; ?>
</option>
<?php
}
?>
</select>
Does anyone know what am I doing wrong here?
Edit 1
I tried the following way. Now it is not showing any options. Instead some errors
<option value=<?php echo $_POST['Type']; ?> <?php if ($_POST['Type'] == $Type) echo 'selected="selected"'; ?> ><?php echo $_POST['Type']; ?></option>
Edit 2
I tried following way and this time it is retaining the values. But only thing is if I select an option with spacing in between and click submit, the value is not retaining and just go to default blank. You can see what I mean by option with spacing as below
Options with no space as below retaining and working well as per my need
You are missing some quotes in your output HTML, that's why spaces in the value throw you out. It should be
<option value="<?php echo $_POST['Type']; ?>" <?php if ($_POST['Type'] == $Type) echo 'selected="selected"'; ?> ><?php echo $_POST['Type']; ?></option>

Save ID from dropdown list into other table

I have a form with fields and one dropdown list. The data from the dropdown list comes out the table: categorie. (fields are: catID, catName)
When I select a categorie from the drop down list and fill all the other input fields, it saves all the input fields and only the catName from the categorie table into the tabel: event.
How can I also save the selected catID from the categorie table into the event table?
Can someone helm me out here?
Regards, Benny
<?php require '../Connections/localhost.php'; ?>
<?php
if(isset($_POST['newEvent'])) {
session_start();
$eventName = $_POST['SelCatName'];
$eventSDate = $_POST['Event-StartDate'];
$eventSTime = $_POST['Event-StartTime'];
$eventEDate = $_POST['Event-EndDate'];
$eventETime = $_POST['Event-EndTime'];
$eventDescription = $_POST['Event-Description'];
$catID = $_POST["catID"];
$sql = $con->query("INSERT INTO event (eventName, eventSDate, eventSTime, eventEDate, eventETime, eventDescription, catID)Values('{$eventName}', '{$eventSDate}', '{$eventSTime}', '{$eventEDate}', '{$eventETime}', '{$eventDescription}', '{$catID}')");
header('Location: eventOK.php');
}
?>
<form action="" method="post" name="RegisterForm" id="RegisterForm">
<div class="FormElement">
<select name="SelCatName" class="TField" id="SelCatName">
<option selected="selected" id="0">--Selecteer een categorie--</option>
<?php
$GetAllCategories = $con->query("SELECT * FROM categorie");
while ($ViewAllCategories = mysqli_fetch_array($GetAllCategories)){
?>
<option id="<?php echo $ViewAllCategories['catID']; ?>"><?php echo $ViewAllCategories ['catName']; ?> </option>
<?php } ?>
</select>
You made a mistake in <option id="id"></option>
if you wanna take value from select child.
you must change id to value="" or you must add value="id" proper.
change your select box block completely like below..
<select name="SelCatName" class="TField" id="SelCatName">
<option selected="selected" id="0">--Selecteer een categorie--</option>
<?php
$GetAllCategories = $con->query("SELECT * FROM categorie");
while ($ViewAllCategories = mysqli_fetch_array($GetAllCategories)){
?>
<option value="<?PHP echo $ViewAllCategories['catID'].'-'.$ViewAllCategories['catName']; ?>"> <?PHP echo $ViewAllCategories['catName']; ?></option>
<?php } ?>
</select>
and a tip for mysql query... to the best of one's ability don't use "select * from" use it if you dont need to all colums better than "*"
$con->query("SELECT catID, catName FROM categorie");
PHP file
<?php require '../Connections/localhost.php'; ?>
<?php
if(isset($_POST['newEvent'])) {
session_start();
//$eventName = $_POST['SelCatName'];
$eventSDate = $_POST['Event-StartDate'];
$eventSTime = $_POST['Event-StartTime'];
$eventEDate = $_POST['Event-EndDate'];
$eventETime = $_POST['Event-EndTime'];
$eventDescription = $_POST['Event-Description'];
$catIDs = $_POST["SelCatName"];
$catID = explode("-", $catIDs);
$sql = $con->query("INSERT INTO event (eventName, eventSDate, eventSTime, eventEDate, eventETime, eventDescription, catID)Values('{$catID[1]}', '{$eventSDate}', '{$eventSTime}', '{$eventEDate}', '{$eventETime}', '{$eventDescription}', '{$catID[0]}')");
header('Location: eventOK.php');
}
?>
to me you are missusing the attribute of ID, Replace the ID for value: your option tag already have the id and name
<select name="SelCatName" class="TField" id="SelCatName">
your code will be:
<?php
$GetAllCategories = $con->query("SELECT * FROM categorie");
while ($ViewAllCategories = mysqli_fetch_array($GetAllCategories)){
?>
<option value="<?php echo $ViewAllCategories['catID']; ?>"> <?php echo $ViewAllCategories ['catName']; ?> </option>
<?php } ?>
Therefore you will find your post as only SelCatName = (numerical ID)
Now, if you need that the list of the catID and catName stores to the next page you can re-run your query in the next page and store them in an array.

how to write select option in cake php and show the result in options

<select>
<?php $result=mysql_query('select id from contact');
while($row=$result){
$id = $row['id'];
?>
<option value='<?php echo $id; ?>'><?php echo $id; ?></option>
<?php } ?>
</select >
How to write this select box in cakephp ..
Make a list query in your controller (http://book.cakephp.org/2.0/en/models/retrieving-your-data.html):
$results = $this->Model->find('list', $options);
and then add it to the form element in your view (http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html):
$this->Form->input('Model.field', array('options' => $results));

Setting selected option in drop-down box

My SELECT looks like the following:
<?php
$query = "SELECT * FROM Rec_SW2_Rel AS a JOIN SW2 b ON a.Sbj_ID = b.IDsbj GROUP BY a.Sbj_ID ORDER BY b.Descriptor";
$result = mysql_query($query);
?>
<select name="country" onchange="getState(this.value)">
<?php
while ($line = mysql_fetch_array($result, MYSQL_ASSOC))
{
?>
<option value="<?php echo $line['Sbj_ID']; ?>">
<?php echo $line['Descriptor']; ?>
</option>
<?php
}
mysql_close();
?>
</select>
Querying the DB and setting up the drop-down works. The problem is that the value listed first isn't automatically selected. If a user wants to use it, for further navigation, they must first select a different one and then select the first once again.
I couldn't alter the values in the DB. If I insert selected='selected' it returns the last value of the result set, but always without being selected.
You maybe want this? First selected option when the form is loaded is blank.
<select name="country" onchange="getState(this.value)">
<option value=""></option>
<?php
while ($line = mysql_fetch_array($result, MYSQL_ASSOC))
{
or select the selected data from the database? selected column with selected value.
<select name="country" onchange="getState(this.value)">
<?php
$first = true;
while ($line = mysql_fetch_array($result, MYSQL_ASSOC))
{
?>
<option value="<?php echo $line['Sbj_ID']; ?>" <?php echo ($line['selected']=='selected') ? 'selected="selected"' : '' ; ?>>
you can test with respect to $line['Sbj_ID'] if this is = to the value you want by default
<?php
$query = "SELECT * FROM Rec_SW2_Rel AS a JOIN SW2 b ON a.Sbj_ID = b.IDsbj GROUP BY a.Sbj_ID ORDER BY b.Descriptor";
$result = mysql_query($query);
?>
<select name="country" onchange="getState(this.value)">
<?php
while ($line = mysql_fetch_array($result, MYSQL_ASSOC))
{
?>
<option value="<?php echo $line['Sbj_ID']; ?>" <?php if($line['Sbj_ID']==value_you_want_selected){?>selected<?php } ?>>
<?php echo $line['Descriptor']; ?>
</option>
<?php
$i++; }
mysql_close();
?>

Categories