PHP how to make some selectbox from a database query - php

i wanna get this, value of 3 selectbox are same:
http://i.stack.imgur.com/EqvpT.jpg
then, i write this code:
$jabatan = mysql_query("SELECT * FROM jabatan WHERE id_jabatan NOT IN (1,10,11,12)") or die(mysql_error());
if (isset($_GET['id'])) {
$query = mysql_query("SELECT *, month(tanggal_larut) as bulan_larut, day(tanggal_larut) as hari_larut, year(tanggal_larut) as tahun_larut FROM larut WHERE id_larut ='$_GET[id]'") or die(mysql_error());
$row = mysql_fetch_object($query);
}
<li>
<label>Pemateri 1</label>
<select name="pemateri1">
<?php
while ($p1 = mysql_fetch_object($jabatan)) {
?>
<option value="<?php echo isset($p1->id_jabatan) ? $p1->id_jabatan : '' ?>" <?php if (isset($_GET['id'])) { if($p1->id_jabatan == $row->pemateri1) {echo "selected";} else {echo "";} } ?> ><?php echo isset($p1->nama_jabatan) ? $p1->nama_jabatan : '' ?></option>
<?php
}
?>
</select>
</li>
<li>
<label>Pemateri 2</label>
<select name="pemateri2">
<?php
while ($p2 = mysql_fetch_object($jabatan)) {
?>
<option value="<?php echo isset($p2->id_jabatan) ? $p2->id_jabatan : '' ?>" <?php if (isset($_GET['id'])) { if($p2->id_jabatan == $row->pemateri2) {echo "selected";} else {echo "";} } ?> ><?php echo isset($p2->nama_jabatan) ? $p2->nama_jabatan : '' ?></option>
<?php
}
?>
</select>
</li>
<li>
<label>Pemateri 3</label>
<select name="pemateri3">
<?php
while ($p3 = mysql_fetch_object($jabatan)) {
?>
<option value="<?php echo isset($p3->id_jabatan) ? $p3->id_jabatan : '' ?>" <?php if (isset($_GET['id'])) { if($p3->id_jabatan == $row->pemateri3) {echo "selected";} else {echo "";} } ?> ><?php echo isset($p3->nama_jabatan) ? $p3->nama_jabatan : '' ?></option>
<?php
}
?>
</select>
</li>
i get this:
http://i.stack.imgur.com/vYnpd.jpg
Ok, please somebody help me, how to correct the code? thanks

Can you try this.... if you want the same select box option why using more time while loop, you can do in one loop. For sql injection read this REF
<?php
$jabatan = mysql_query("SELECT * FROM jabatan WHERE id_jabatan NOT
IN (1,10,11,12)") or die(mysql_error());
if (isset($_GET['id'])) {
$query = mysql_query("SELECT *, month(tanggal_larut) as bulan_larut,
day(tanggal_larut) as hari_larut, year(tanggal_larut) as tahun_larut
FROM larut WHERE id_larut ='".mysql_real_escape_string($_GET[id])."'") or
die(mysql_error());
$row = mysql_fetch_object($query);
}
$Selectbox_options = '';
while ($p1 = mysql_fetch_object($jabatan)) {
$value = isset($p1->id_jabatan) ? $p1->id_jabatan : '';
$selection_value = '';
if (isset($_GET['id'])) {
if ($p1->id_jabatan == $row->pemateri1) {
$selection_value = "selected";
} else {
$selection_value = "";
}
}
$option_title = isset($p1->nama_jabatan) ? $p1->nama_jabatan : '';
$Selectbox_options .= '<option value="' . $value . '" ' . $selection_value . ' >' . $option_title . '</option>';
}
?>
<li>
<label>Pemateri 1</label>
<select name="pemateri1">
<?php echo $Selectbox_options;?>
</select>
</li>
<li>
<label>Pemateri 2</label>
<select name="pemateri2">
<?php echo $Selectbox_options;?>
</select>
</li>
<li>
<label>Pemateri 3</label>
<select name="pemateri3">
<?php echo $Selectbox_options;?>
</select>
</li>

Related

How to set Selected value to dropdown list from database PHP/SQL

I want to set selected values in dropdown list to the one im editing.
$id = $_GET['edit'];
$result = $polaczenie->query("SELECT * FROM wizyty WHERE idwizyty=$id")
or die($mysqli->error());
while($row=mysqli_fetch_assoc($result))
{
$id = $row['idwizyty'];
$data = $row['data'];
$pracownik = $row['pracownik'];
$usluga = $row['usluga'];
$klient = $row['klient'];
$time = $row['starttime'];
$cena = $row['cena'];
}
Dropdown list
<label>Pracownik</label>
<?php
$query = "SELECT imie, nazwisko FROM `pracownicy`";
$wynik1 = mysqli_query($polaczenie, $query);
?>
<select name="nowypracownik" value="<?php echo $pracownik; ?>">
<?php while($row1 = mysqli_fetch_array($wynik1)):;?>
<option selected="<?php echo $pracownik?>"><?php echo $row1[0] .' '. $row1[1] ;?></option>
<?php endwhile; ?>
</select><br><br>
I tried to do it like this but it's not working.
The only value for ‘selected’ attribute is ‘selected’
<option value="value" selected="selected">value</option>
So your code should look something like this
<select name="nowypracownik" value="<?php echo $pracownik; ?>">
<?php while($row1 = mysqli_fetch_array($wynik1));?>
$value = $row1[0] .' '. $row1[1];
$selected = $value == $pracownik ? 'selected="selected"' : '';
<option <?php echo $selected?>><?php echo $value;?></option>
<?php endwhile; ?>
</select><br><br>

Dynamic Select Boxes work half

I have two fail for my script.
1.publicAdvart.php
<script type="text/javascript">
$(document).ready(function () {
$('#select_marka').change(function () {
$.post(
'modeli.php',
{id: $('#select_marka').val()},
function (res) {
$('#model').html(res);
}
);
});
});
</script>
<select class="form-control" name="marka" id="select_marka">
<option></option>
<?php
$sel = "SELECT * FROM marka";
$query = mysqli_query($conn, $sel);
while ($row = mysqli_fetch_array($query)) {
?>
<option value="<?php echo $row['id']; ?>"><?php echo $row['marka']; ?></option>
<?php
}
?>
</select>
2.modeli.php
<?php
if (isset($_POST['id']) && $_POST['id'] != '') {
$marka_id = $_POST['id'];
?>
<select id="select_marka" class="form-control">
<?php
$selModel = "SELECT * FROM marka_model WHERE marka_id='$marka_id'";
$queryModel = mysqli_query($conn, $selModel);
while ($rows = mysqli_fetch_array($queryModel)) {
?>
<option value="<?php echo $rows['model']; ?>"><?php echo $rows['model']; ?></option>
<?php
}
?>
</select>
<?php
}
?>
Where is a problem? These two script half work.
This is OK click "Opel" -> "meriva". http://i.stack.imgur.com/ma5XZ.jpg
Next step 2 is a problem, i want "name" marka and "name" model.
Problem this is here.I don't get "marka - id", i want "name" http://i.stack.imgur.com/Nm1Yk.jpg
Update:
step2.php
<?php
if (isset($_POST['submit'])) {
$marka = $_POST['marka'];
$model = $_POST['model'];
$_SESSION['marka'] = $marka;
$_SESSION['model'] = $model;
}
?>
<div class="col-md-12 well advertTittle">
<h4 class="well">
<?php echo $_SESSION['marka'] . '&nbsp' . $_SESSION['model'] . '&nbsp' . stripslashes($_SESSION['modify']); ?>
</h4>
<span><?php echo $_SESSION['price'] . ' лв.'; ?></span>
</div>
You forget to give the "name" attribute for yours "marka" select field:
<?php
...
?>
<select id="select_marka" class="form-control"> // <- here
<?php
$selModel = "SELECT * FROM marka_model WHERE marka_id='$marka_id'";
....
Should be something like this:
<?php
...
?>
<select id="select_model" class="form-control" name="model"> // <- here!!!
// "id" attribute also should be changed
<?php
$selModel = "SELECT * FROM marka_model WHERE marka_id='$marka_id'";
....
Also, yours step2.php should be like this:
<?php
if (isset($_POST['submit'])) {
$_SESSION['marka'] = $_POST['marka'];
$_SESSION['model'] = $_POST['model'];
}
function validData($data) {
return
isset($data['marka'])
&& isset($data['model'])
&& isset($data['price'])
;
}
if (validData($_SESSION)) {
$marka_id = intval($_SESSION['marka']);
$model_id = intval($_SESSION['model']);
$marka = mysqli_fetch_array(mysqli_query($conn, "SELECT name FROM marka where id = $marka_id"));
$model = mysqli_fetch_array(mysqli_query($conn, "SELECT name FROM marka_model WHERE id = $model_id"));
?>
<div class="col-md-12 well advertTittle">
<h4 class="well">
<?php echo $marka['name'] . '&nbsp' . $model['name'] . '&nbsp' . (isset($_SESSION['modify']) ? stripslashes($_SESSION['modify']) : ''); ?>***
</h4>
<span>
<?php echo $_SESSION['price'] . ' лв.'; ?>
</span>
</div>
<?php } else { ?>
Not all fields filled.
<?php } ?>

selected value get from db into dropdown select box option using php mysql error

I need to get selected value from db into select box. please, tell me how to do it. Here is the code.
Note: 'options' value depends on the category.
<?php
$sql = "select * from mine where username = '$user' ";
$res = mysql_query($sql);
while($list = mysql_fetch_assoc($res)){
$category = $list['category'];
$username = $list['username'];
$options = $list['options'];
?>
<input type="text" name="category" value="<?php echo '$category' ?>" readonly="readonly" />
<select name="course">
<option value="0">Please Select Option</option>
<option value="PHP">PHP</option>
<option value="ASP">ASP</option>
</select>
<?php
}
?>
I think you are looking for below code changes:
<select name="course">
<option value="0">Please Select Option</option>
<option value="PHP" <?php if($options=="PHP") echo 'selected="selected"'; ?> >PHP</option>
<option value="ASP" <?php if($options=="ASP") echo 'selected="selected"'; ?> >ASP</option>
</select>
The easiest way I can think of is the following:
<?php
$selection = array('PHP', 'ASP');
echo '<select>
<option value="0">Please Select Option</option>';
foreach ($selection as $selection) {
$selected = ($options == $selection) ? "selected" : "";
echo '<option '.$selected.' value="'.$selection.'">'.$selection.'</option>';
}
echo '</select>';
The code basically places all of your options in an array which are called upon in the foreach loop. The loop checks to see if your $options variable matches the current selection it's on, if it's a match then $selected will = selected, if not then it is set as blank. Finally the option tag is returned containing the selection from the array and if that particular selection is equal to your $options variable, it's set as the selected option.
for example ..and please use mysqli() next time because mysql() is deprecated.
<?php
$select="select * from tbl_assign where id='".$_GET['uid']."'";
$q=mysql_query($select) or die($select);
$row=mysql_fetch_array($q);
?>
<select name="sclient" id="sclient" class="reginput"/>
<option value="">Select Client</option>
<?php $s="select * from tbl_new_user where type='client'";
$q=mysql_query($s) or die($s);
while($rw=mysql_fetch_array($q))
{ ?>
<option value="<?php echo $rw['login_name']; ?>"<?php if($row['clientname']==$rw['login_name']) echo 'selected="selected"'; ?>><?php echo $rw['login_name']; ?></option>
<?php } ?>
</select>
Just Add an extra hidden option and print selected value from database
<option value="<?php echo $options;?>" hidden><?php echo $options;?></option>
<option value="PHP">PHP</option>
<option value="ASP">ASP</option>
Select value from drop down.
<select class="form-control" name="category" id="sel1">
<?php foreach($data as $key =>$value) { ?>
<option value="<?php echo $data[$key]->name; ?>"<?php if($id_name[0]->p_name==$data[$key]->name) echo 'selected="selected"'; ?>><?php echo $data[$key]->name; ?></option>
<?php } ?>
</select>
THE EASIEST SOLUTION
It will add an extra in your options but your problem will be solved.
<?php
if ($editing == Yes) {
echo "<option value=\".$MyValue.\" SELECTED>".$MyValue."</option>";
}
?>
$option = $result['semester'];
<option >Select</option>
<option value="1st" <?php if($option == "1st") echo 'selected = "selected"'; ?>>1st</option>
<option value="2nd" <?php if($option == "2nd") echo 'selected = "selected"'; ?>>2nd</option>
<option value="3rd" <?php if($option == "3rd") echo 'selected = "selected"'; ?>>3rd</option>
<option value="4th" <?php if($option == "4th") echo 'selected = "selected"'; ?>>4th</option>
<option value="5th" <?php if($option == "5th") echo 'selected = "selected"'; ?>>5th</option>
<option value="6th" <?php if($option == "6th") echo 'selected = "selected"'; ?>>6th</option>
<option value="7th" <?php if($option == "7th") echo 'selected = "selected"'; ?>>7th</option>
<option value="8th" <?php if($option == "8th") echo 'selected = "selected"'; ?>>8th</option>
</select>
BEST code and simple
<select id="example-getting-started" multiple="multiple" name="category">
<?php
$query = "select * from mine";
$results = mysql_query($query);
while ($rows = mysql_fetch_assoc(#$results)){
?>
<option value="<?php echo $rows['category'];?>"><?php echo $rows['category'];?></option>
<?php
}
?>
</select>
You can also do like this ....
<?php $countryname = $all_meta_for_user['country']; ?>
<select id="mycountry" name="country" class="user">
<?php $myrows = $wpdb->get_results( "SELECT * FROM wp_countries order by country_name" );
foreach($myrows as $rows){
if( $countryname == $rows->id ){
echo "<option selected = 'selected' value='".$rows->id."'>".$rows->country_name."</option>";
} else{
echo "<option value='".$rows->id."'>".$rows->country_name."</option>";
}
}
?>
</select>
Answer is simple.
when u pass value from dropdown.
Just use as if else.
for eg:
foreach($result as $row) {
$GLOBALS['output'] .='<option value="'.$row["dropdownid"].'"'.
($GLOBALS['passselectedvalueid']==$row["dropwdownid"] ? ' Selected' : '').'
>'.$row['valueetc'].'</option>';
}
<?php
$sql = "select * from mine where username = '$user' ";
$res = mysql_query($sql);
while($list = mysql_fetch_assoc($res)){
$category = $list['category'];
$username = $list['username'];
$options = $list['options'];
?>
<input type="text" name="category" value="<?php echo '$category' ?>" readonly="readonly" />
<select name="course">
<option value="0">Please Select Option</option>
<option value="PHP" <?php echo $options == 'PHP' ? 'selected' : ''; ?> >PHP</option>
<option value="ASP" <?php echo $options == 'ASP' ? 'selected' : ''; ?> >ASP</option>
</select>
<?php
}
?>
USING PDO
<?php
$username = "root";
$password = "";
$db = "db_name";
$dns = "mysql:host=localhost;dbname=$db;charset=utf8mb4";
$conn = new PDO($dns,$username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "select * from mine where username = ? ";
$stmt1 = $conn->prepare($sql);
$stmt1->execute(array($_POST['user']));
$all = $stmt1->fetchAll(); ?>
<div class="controls">
<select data-rel="chosen" name="degree_id" id="selectError">
<?php
foreach($all as $nt) {
echo "<option value =$nt[id]>$nt[name]</option>";
}
?>
</select>
</div>
I'm using eval() PHP function like this:
My PHP code:
$selOps1 = $selOps2 = $selOps3 = '';
eval('$selOps'. $dbRow["DBitem"] . ' = "selected";');
Then in my select box I use it like this:
<select>
<option <?=$selOps1?> value="1">big</option>
<option <?=$selOps2?> value="2">Middle</option>
<option <?=$selOps3?> value="3">Small</option>
</select>
Put value from db into a variable and check like following code example
<select class="form-control" name="currency_selling" required >
<option value="">Select Currency</option>
<option value="pkr" <?=$selected_currency == 'pkr' ? ' selected="selected"' : '';?> >PKR</option>
<option value="dollar" <?=$selected_currency == 'dollar' ? ' selected="selected"' : '';?> >USD</option>
<option value="pounds" <?=$selected_currency == 'pounds' ? ' selected="selected"' : '';?> >POUNDS</option>
<option value="dirham" <?=$selected_currency == 'dirham' ? ' selected="selected"' : '';?> >DRHM</option>
</select>
This may help you.
?php
$sql = "select * from mine where username = '$user' ";
$res = mysql_query($sql);
while($list = mysql_fetch_assoc($res))
{
$category = $list['category'];
$username = $list['username'];
$options = $list['options'];
?>
<input type="text" name="category" value="<?php echo '$category' ?>" readonly="readonly" />
<select name="course">
<option value="0">Please Select Option</option>
// Assuming $list['options'] is a coma seperated options string
$arr=explode(",",$list['options']);
<?php foreach ($arr as $value) { ?>
<option value="<?php echo $value; ?>"><?php echo $value; ?></option>
<?php } >
</select>
<?php
}
?>

Updating multiple rows using select PHP and MySQL

Below is the code where I am trying to change the values of one column in multiple rows but I am a little bit stuck.
File 1:
<form name="update-location" method="post" action="recibos_location_update.php">
<div align="right"><button type="submit">Actualizar Estado</button></div>
<hr />
<ul>
<?php
$get_logs = mysql_query("SELECT * FROM recibos ORDER BY id DESC");
while ($logs = mysql_fetch_array($get_logs)){
$serial = $logs['serial'];
$check = $logs['location'];
$problem = $logs['problem'];
?>
<li class="<?php
if ($check == $location1){echo 'recibos-container-shop-li';}
if ($check == $location2){echo 'recibos-container-haji-li';}
if ($check == $location3){echo 'recibos-container-return-li';} ?>">
<?php echo $serial . " | " . $logs['model'] . " | " . $problem; ?>
<div align="right">
<?php
if ($check != 'Returned'){
?>
<input type="hidden" name="serial[]" value="<?php echo $serial; ?>" />
<select name="location">
<option <?php if ($check == $location1){echo 'selected';} ?> value="<?php echo $location1; ?>" style="background: #fff8bf;">Mobils Calfont</option>
<option <?php if ($check == $location2){echo 'selected';} ?> value="<?php echo $location2; ?>" style="background: #ffc1bf;">Haji Jorda</option>
<option <?php if ($check == $location3){echo 'selected';} ?> value="<?php echo $location3; ?>"style="background: #ecffbf;">Devuelto</option>
</select>
<?php
}
else {
echo "<i><font color='#5e8029'>Devuelto</font></i>";
}
?>
</div>
</li>
<?php } ?>
</ul>
File 2
<?php
include "connect.php";
include "links.php";
foreach($_POST["serial"] as $serial){
$location = $_POST['location'];
echo $location . " " . $serial . "<br>";
}
mysql_query("UPDATE recibos SET location='$location' WHERE serial='$serial'");
//header("Location: " . $_SERVER['HTTP_REFERER']);
?>
and the output I am getting is:
Haji 900005
Haji 900004
Haji 900002
But for all 3 output lines, it should give different names as names in 3 select boxes were different.
What I need is for the process form to update every row with what value is contained in select box options.
html
<select name="location[]">
php
foreach($_POST["serial"] as $k=> $serial){
$location = $_POST['location'][$k];
mysql_query("UPDATE recibos SET location='$location' WHERE serial='$serial'");
}
foreach($_POST["serial"] as $serial){
$location = $_POST['location'];
echo $location . " " . $serial . "<br>";
mysql_query("UPDATE recibos SET location='$location' WHERE serial='$serial'");//update
}
// mysql_query("UPDATE recibos SET location='$location' WHERE serial='$serial'");

selected state mysql array

I am trying to combine a table select output with a piece of code I found to keep the selected state:
<select>
<?php
$desired_option = 'arsenal';
$arr = array('arsenal', 'aston villa', 'birmingham', 'blackpool', 'bolton');
for($i = 0; $i < count($arr); $i++) {
$selected = ($arr[$i] == $desired_option) ? 'selected="selected"' : '';
echo "<option value=\"{$arr[$i]}\" {$selected}>{$arr[$i]}</option>";
}
?>
</select>
<select id="teams" onchange="this.form.submit();" name="teamid">
<?
include('db.php');
$getTeams = mysql_query("SELECT name, id FROM team") or die(mysql_error());
while ($teamsData = mysql_fetch_array($getTeams))
{
?>
<option value="<? echo $teamsData['id']; ?>" ><? echo $teamsData['name']; ?></option>
<?
}
?>
</select>
I have tried everything. Any ideas?
Thanks :)
<select id="teams" onchange="this.form.submit();" name="teamid">
<?
include('db.php');
$selected = 'team_to_be_selected';
$getTeams = mysql_query("SELECT name, id FROM team") or die(mysql_error());
while ($teamsData = mysql_fetch_array($getTeams))
{
?>
<option value="<?php echo $teamsData['id']; ?>" <?php echo ($teamsData['name'] == $selected) ? 'selected="selected"' : ''; ?>><?php echo $teamData['name'];?></option>
<?
}
?>
</select>

Categories