SET selected in the select tag - php

<select class="form-control" name="Church" id="Church">
<option>Church List</option>
<?php
$select_church = mysql_query("SELECT * FROM tblchurchs");
while($get_detail = mysql_fetch_array($select_church)){
echo'<option value="'.$get_detail['AChurchID'].'">'.$get_detail['ChurchName']." - ".$get_detail['Address'].'</option>';
}
?>
</select>
This is Edit.php
This is the result of that code
but i want is. after getting the data from the database. it will automatically selected the value of the option base on the database data.

Suppose the query result returned from tblchurchs contains a field Selected which is either set to 1 or 0. If it is set to 1 then you have to select that option in your drop-down list, and if it is set to 0, it is not selected.
<select class="form-control" name="Church" id="Church">
<option>Church List</option>
<?php
$select_church = mysql_query("SELECT * FROM tblchurchs");
while($get_detail = mysql_fetch_array($select_church)){
echo'<option value="'.$get_detail['AChurchID'].'"';
if($get_detail['Selected'] == 1)
{
echo ' selected="selected"';
}
echo '>'.$get_detail['ChurchName']." - ".$get_detail['Address'].'</option>';
}
?>
</select>

to getting selected for selected option.
first you have to save the selected data getting from database to a variable and then using an if loop you can do this.
<?php
$AChurchID=1; // this is your already selected value that is in db .
?>
<select class="form-control" name="Church" id="Church">
<option>Church List</option>
<?php
$select_church = mysql_query("SELECT * FROM tblchurchs");
while($get_detail = mysql_fetch_array($select_church)){
$sel="";
if($AChurchID==$get_detail['AChurchID']){
$sel="selected";
}
echo'<option value="'.$get_detail['AChurchID'].'" '.$sel.'>'.$get_detail['ChurchName']." - ".$get_detail['Address'].'</option>';
}
?>
This will work in your case. This is works fine in ma case. Please have a try.

Related

Use mysql query result in more than one place in code

I'm building a system that tracks contact lenses. I'm storing the contact lens info in a database as sometimes prices/availabilities change and i access this info from multiple points in the program. I'm trying to interface with this list using a dropdown by doing "SELECT * FROM contacts" as a query. my code looks like this :
$contact_list = mysqli_query($link, "SELECT brand FROM contacts ORDER BY brand");
Then I echo that list out in a while loop using PHP to populate the options in the dropdown.
My question is this: I have these dropdowns for each eye on the same form. So it's "Brand Right Eye"....other miscellaneous info about the right eye....then "Brand Left Eye". But ONLY the right eye is populating with the brand info because it appears first in the code. What i'm having to do is copy/paste the exact same query and do
$contact_list2 = mysqli_query($link, "SELECT brand FROM contacts ORDER BY brand");
then later if I need the dropdown again, I need to do $contact_list3..and so on. Why can i not generate a drop down using the same variable? Why does it stop responding to calling the variable after the first execution of it and is there any work around that I can implement that would allow me to not have to copy/paste the same query with a different variable association each time?
just for refernce, my php while code is this:
<select class="form-control" name = "brandOS">
<option value="0">Please Select</option>
<?php
while($row = mysqli_fetch_array($contact_list))
{
?>
<option value = "<?php echo($row['brand'])?>" name = "brandOS">
<?php echo($row['brand']) ?>
</option>
<?php
}
?>
</select>
I have this loop copy/pasted for right eye and left eye. But it only works on which ever drop down appears first in the code.
A possible solution will be more efficient in term of performance could be :
<?php
$left_eye = '<option value="0">Please Select</option>';
$rigth_eye = '<option value="0">Please Select</option>';
while($row = mysqli_fetch_array($contact_list))
{
//logic for left eye
$left_eye .= <<<HTML
<option value ="{$row['brand']}" name = "brandOS">
{$row['brand']}
</option>
HTML;
//logic for rigth eye
$rigth_eye .= <<<HTML
<option value ="{$row['brand']}" name = "brandOS">
{$row['brand']}
</option>
HTML;
}
?>
<select class="form-control" name = "brandOS">
<?php echo $left_eye ; ?>
</select>
<select class="form-control" name = "brandOS">
<?php echo $rigth_eye ; ?>
</select>
With this solution you get your result in the same while loop. If the left and right select are the same you can use the same variable.
Store the brands in an array, then you can just loop through the array.
<?php
$contact_list = mysqli_query($link, "SELECT brand FROM contacts ORDER BY brand");
$brands = array();
while($row = mysqli_fetch_array($contact_list))
{
array_push($brands, $row['brand']);
}
?>
<select class="form-control" name = "brandOS">
<option value="0">Please Select</option>
<?php
foreach($brands as $brand){
?>
<option value = "<?php echo($brand[0])?>" name = "brandOS">
<?php echo($brand[0]) ?>
</option>
<?php
}
?>
</select>
You can use a PHP array, like the SESSION one, to store values and use them across your site. Be sure you call "session_start()" method on each page you use that array, though.
//Initialize sessions
session_start();
...
//Right after getting result from query
$_SESSION['contact_list'] = $contact_list;
To use it, just be sure to call the method I told you above, and call the variable with the same syntax:
<?php
while($row = mysqli_fetch_array($_SESSION['contact_list'])) { ?>
Hope this helps.

Php pdo display enum values in dropdown

i am trying below code to display list of enum values in select dropdown box.
but its displaying only dropdown box, but values are not displaying....
tablename = tbl_users, column name = userStatus
<select>
<?
$stmt = $user_home->runQuery('SHOW COLUMNS FROM '.tbl_users.' WHERE field="'.userStatus.'"');
while($data = $stmt->fetch()) {
foreach(explode("','",substr($row[1],6,-2)) as $option) {
print("<option>$option</option>");
}
}
?>
<select>
Note : I really tried lot before posting question here & i am new to php coding, still learning....
To display list of enum values in select dropdown:
<select name="select">
<?php
$sql = 'SHOW COLUMNS FROM table_name WHERE field="field_name"';
$row = $dbh->query($sql)->fetch(PDO::FETCH_ASSOC);
foreach(explode("','",substr($row['Type'],6,-2)) as $option) {
print("<option value='$option'>$option</option>");
}
?>
</select>
For display enum value as dropdown you can do something like this.
<?php $status = array('Y'=>'Approve','N'=>'unapprove'); ?>
<select>
<?php foreach($status as $key=>$state) { ?>
<option value="<?php echo $key;?>"><?php echo $state;?></option>
<?php } ?>
</select>

Select list box display double same value

i have a list box that contain name of persons. In the column "Requestor" there are many names that repeat with the same name. So when I make the list box, the name shows all the name and some name shows more than one with the same name. How can i make the name just come out one only. Below are my codes.
<select name="requestor" id="requestor">
<option value="0">-- Select requestor --</option>
<?php
$getallCapex_transaction = mysql_query("SELECT * FROM capex_transaction ");
while($viewallCapex_transaction = mysql_fetch_array($getallCapex_transaction))
{
?>
<option id="<?php echo $viewallCapex_transaction ['Project_id'];?>"><?php
echo $viewallCapex_transaction['Requestor']; ?></option>
<?php } ?>
</select>
And one more question how can I show list of month and year that connect with mySql? can someone show me some codes. Thanks
Well, if there are multiple rows with the same "Requestor" but different "Project_id", that's just how it's gonna turn out. You can select distinct by Requestor in your SQL, but that will leave you unaware of what record you're pointing at.
Put your values in an array and some something like array_unique to create an unique array. Try something like this:
<?php
$requestorArray = array();
?>
<select name="requestor" id="requestor">
<option value="0">-- Select requestor --</option>
<?php
$getallCapex_transaction = mysql_query("SELECT * FROM capex_transaction ");
while($viewallCapex_transaction = mysql_fetch_array($getallCapex_transaction))
{
$requestorArray[$viewallCapex_transaction ['Project_id']] = $viewallCapex_transaction['Requestor'];
}
$requestorArray = array_unique($requestorArray);
foreach($requestorArray as $projectId => $requestor)
?>
<option id="<?php echo $projectId;?>"><?php
echo $requestor; ?></option>
<?php } ?>
</select>

echo drop down menu onto a page

I have a drop down menu with two options in html, I also created a PHP script that checks what option from the drop down menu has been selected and based on the selection executes a mysql query to fetch data from database.
But I am also trying to echo out a new drop down menu with the results obtained from database and that is where the I am struggling because no errors are diaplayed but also no drop down menu is 'echoed' out onto the page.
HTML:
<?php require "course.php" ?>
<select id="workshop" name="workshop" onchange="return test();">
<option value="">Please select a Workshop</option>
<option value="Forex">Forex</option>
<option value="BinaryOptions">Binary Options</option>
</select>
PHP code:
$form['workshop'] = $_POST['workshop'];
$form['forex'] = $_POST['Forex'];
$form['binary'] = $_POST['Binary'];
//Retrieve Binary Workshops
if($form['workshop'] == 'Forex'){
$sql2 = "SELECT id, course, location FROM courses WHERE course LIKE '%Forex%' OR course LIKE '&forex%'";
$query2 = mysqli_query($link, $sql2);
echo "<select id='Forex' name='Forex' style='display: none'>";
while($result2 = mysqli_fetch_assoc($query2)){
echo "<option value=''>".$result2['course']."</option>";
}
echo "</select>";
echo '</br>';
}
Could someone point out a mistake I am doing or perhaps suggest where I could look for answers
you said $query2 is displaying value in print_r.so the only mistake i find in your code is display:none .
remove display:none
echo "<select id='Forex' name='Forex' style='display: none'>";
----------------------------------------------------------------------^

set default drop down value on php generated form

I generated a drop down menu with the code below. How can I set the field to the GET variable after submit is clicked?
<select>
<?php
$sql="SELECT c FROM problems WHERE b='$id'";
$result=mysql_query($sql);
$options="";
while ($row=mysql_fetch_array($result)) {
$id=$row["c"];
$c=$row["c"];
$options.="<option value=\"$id\">".$c;
}
?>
<option value=''>C <?=$options?> </option>
</select>
<select>
<?php
$sql="SELECT c FROM problems WHERE b='$id'";
$result=mysql_query($sql);
$options="";
while ($row=mysql_fetch_array($result)) {
$id=$row["c"];
$c=$row["c"];
if($_GET['var'] == $id)
echo '<option selected="selected" value=\"$id\">' . $c . '</option>';
else
echo '<option value=\"$id\">' . $c . '</option>';
}
?>
</select>
Basic idea is compare value GET data with database data and using if else condition add selected="selected" if condition matched. I am directly printing string as they will not be getting use later on.
I'm a bit confused about what you want to know. To have an option selected by default, use the selected="selected" attribute on that option.
If you want to know how to get the submitted value in PHP, you need to assign the name attribute to the <select> tag. In general, however, you've got the idea right.

Categories