I have a select box with options up to 20, for example, if option 5 or any other is selected and saved in a database in the specific column it won't show next time...
please guide me
<select class="form-control text-center" name="code" id="Code" >
<option value="">Select Code</option>
<?php for($i = 1; $i <= 20; $i++){ ?>
<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php } ?>
</select>
Controller:
function index(){
//below code should be in Model this is only for example
$this->db->select('code');
$query = $this->db->get_where('hr_levels', array('created_by' => $_SESSION['username']));
if ($query->num_rows() > 0) {
$code = $query->row('code');
} else {
$code = 0;
}
$option = array();
for($i=1;$i<=20;$i++){
if ($code != $i) {//no need to add if in view, just pass the data to view
$option[$i] = $i;
}
}
$data['options'] = $option;
$this->load->view('option_view',$data);
}
view:
<select class="form-control text-center" name="code" id="Code" >
<option value="">Select Code</option>
<?php foreach($options as $key => $value){
<option value="<?php echo $key; ?>"><?php echo $value; ?></option>
<?php } ?>
</select>
You can approach it like this
$availableOption = range(1,20);
$selectedOrInDb = [5,6,8,9]; // Selected OR from DB
$remaining = array_diff($availableOption, $selectedOrInDb);
Now you can use $remaining to loop through
<select class="form-control text-center" name="code" id="Code" >
<option value="">Select Code</option>
<?php foreach($remaining as $key => $value){ ?>
<option value="<?php echo $value; ?>"><?php echo $value; ?></option>
<?php } ?>
</select>
Controller
public function get_selected(){
$this->load->model('model_name');
$data['selected'] = $this->model_name->function_name($_SESSION['username']);
//Already saved values will be get on the basis of any condition
return $this->load->view('file.php');
}
Model
public function function_name($username){
return $this->db->get_where('table', ['created_by' => $username])->row()->value;
}
Now you get already selected value in $selected
<select class="form-control text-center" name="code" id="Code" >
<option value="">Select Code</option>
<?php for($i = 1; $i <= 20; $i++){
if($i != $selected)?>
<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php } } ?>
</select>
Related
I have a problem when submit form value from the second select don't show. Can someone explain me what I'm doing wrong because in new in this (maybe is because i use onchange event). Thanks in advance.
index.php
<?php
include("includes/conn.php");
$sql = "SELECT * FROM opstina";
$result = $conn->query($sql);
if($result->num_rows > 0){
$opstineArr = array();
while($row = $result->fetch_assoc()){
$opstineArr[] = $row;
}
}
if(isset($_POST["addNesto"])){
$opstine = $_POST["opstine"];
$ime = $_POST["ime"];
if(isset($opstine)){
if(empty($opstine)){
$error["opstine"] = "izaberite opstinu";
}
}
$naselja = $_POST["naselja"];
if(isset($naselja)){
if(empty($naselja)){
$error["naselja"] = "izaberite naselje";
}
}
if(isset($ime)){
if(empty($ime)){
$error["ime"] = "izaberite ime";
}
}
if(count($error) == 0){
echo 2222;
}
}
include("includes/header.php");
?>
<form action="" method="POST">
<select name="opstine" class="form-control" id="select_id" onchange="getNaselja(this.value)">
<option>..Izaberite Opstinu..</option>
<?php foreach($opstineArr as $key => $opstina): ?>
<option value="<?php echo $opstina['OpstinaID']; ?>" <?php if(isset($opstine) && $opstine==$opstina['OpstinaID']) echo "selected"; ?> ><?php echo $opstina['NazivOpstine']; ?></option>
<?php endforeach; ?>
</select>
<?php if(isset($error["opstine"])) echo $error["opstine"]; ?>
<p>
<select name='naselja' id='naseljeLista' class='form-control' style='margin-top:10px;'>
</select>
<span><?php if(isset($error["naselja"])) echo $error["naselja"]; ?></span>
</p><br>
<input type="text" name="ime" value="<?php if(isset($ime)) echo $ime; ?>" class="form-control col-md-7 col-xs-12"><?php if(isset($error["ime"])) echo $error["ime"]; ?><br><br>
<input type="submit" name="addNesto" class="btn btn-sm btn-success">
</form>
<?php include("includes/footer.php"); ?>
getNaselja.php
global $conn;
include("includes/conn.php");
if(!empty($_POST["opstina_id"])){
$opstinaID = $_POST["opstina_id"];
$sql1 = "SELECT * FROM naselje WHERE OpstinaID=".$opstinaID;
$result1 = $conn->query($sql1);
if($result1->num_rows > 0){
$naseljaArr = array();
while ($one = $result1->fetch_assoc()) {
$naseljaArr[] = $one;
}
}
echo "<option value=''>---Izaberite naselje---</option>";
foreach ($naseljaArr as $key => $value) {
if(isset($_POST["naselja"]) && $value["NaseljeID"]==$_POST["naselja"]){
echo "<option value='".$value['NaseljeID']."' selected='selected' style='display:block;'>".$value['NazivNaselja']."</option>";
}else{
echo "<option value='".$value['NaseljeID']."'>".$value['NazivNaselja']."</option>";
}
}
}
footer.php
<script>
function getNaselja(val){
$.ajax({
type: 'POST',
url: 'getNaselja.php',
data: 'opstina_id='+val,
success: function(data){
$('#naseljeLista').html(data);
console.log(data);
}
});
}
</script>
example of form after submit on index.php
When I choose from first select my data on the console looks like:
<option value=''>---Izaberite naselje---</option>
<option value='4610'>Beška</option>
<option value='4611'>Inđija</option>
<option value='4612'>Jarkovci</option>
<option value='4613'>Krčedin</option>
<option value='4614'>Ljukovo</option>
<option value='4615'>Maradik</option>
<option value='4616'>Novi Karlovci</option>
<option value='4617'>Novi Slankamen</option>
<option value='4618'>Slankamenački Vinogradi</option>
<option value='4619'>Stari Slankamen</option>
<option value='4620'>Čortanovci</option>
This always selected the last record from my database.
This is my select tag:
<select name="t_proyek_kd_proyek" id="t_proyek_kd_proyek" class="select-search" />
<option value=""></option>
<?php
$t_cost=$this->db->query("select * from t_proyek ");
foreach($t_cost->result() as $value){
$selected= '';
if($t_proyek_kd_proyek == $value->nm_proyek){
$selected = 'selected';
}
?>
<option value="<?php echo $value->kd_proyek; ?>" echo ' selected="selected"'; >
<?php echo $value->nm_proyek; ?>
</option>
<?php }?>
</select>
And this is my controller:
$row = $this->T_cost_model->get_by_id($id);
if ($row) {
$data = array(
'button' => 'Update',
'action' => site_url('konsultan/cost/update_action'),
'kd_cost' => set_value('kd_cost', $row->kd_cost),
'investasi_awal' =>rupiah(set_value ('investasi_awal', $row->investasi_awal)),
'b_operasional' => rupiah(set_value('b_operasional', $row->b_operasional)),
'g_cost' => rupiah(set_value('g_cost', $row->g_cost)),
'disbenfit' => rupiah(set_value('disbenfit', $row->disbenfit)),
'pwc' => set_value('pwc', $row->pwc),
't_proyek_kd_proyek' => set_value('t_proyek_kd_proyek', $row->t_proyek_kd_proyek),
);
$this->load->view('konsultan/t_cost_form', $data);
In your HTML, your last option tag has selected="selected" explicitly set:
option value="<?php echo $value->kd_proyek; ?>" <?php echo 'selected="selected"'; ?>
Wrap that in a condition and the selected option from your loop should work.
option value="<?php echo $value->kd_proyek; ?>" <?php if ($selected) echo 'selected="selected"'; ?>
change your <select> html code value like this
<select name="t_proyek_kd_proyek" id="t_proyek_kd_proyek" class="select-search" />
<option value=""></option>
foreach($t_cost->result() as $value){
$selected= '';
if($t_proyek_kd_proyek == $value->nm_proyek){
$selected = ' selected="selected" ';
}
?>
<option value="<?php echo $value->kd_proyek; ?>" <?= $selected?> >
<?php echo $value->nm_proyek; ?>
</option>
<?php }?>
</select>
hope it will help
I have state table having state_id and state_name. I am displaying it directly in template file like below..
<select name="state" id="state" class="select-submit2">
<option value="">Select state</option>
<?php
$result=$wpdb->get_results("select * from states");
foreach($result as $row) {
$state_id=$row->state_id;
$state_name=$row->state_name;
echo '<option value='.$state_id.'>'.$state_name.'</option>';
}
?>
</select>
But when I want to edit that how do I show first selected state name.????
Edit page url...
whitecode.in/demo/plotsup_plot/new-property/?listing_edit=6795
This is my function code in function.php file
function getcity(){
global $wpdb;
if($_POST['state'])
{
$id=$_POST['state'];
$property_id = $_GET['listing_edit'];
$district = get_post_meta($property_id, district, true);
$result=$wpdb->get_results("SELECT * FROM districts WHERE state_id='$id'");
//$wpdb->get_results($query);
foreach($result as $row) {
$city_name = $row->district_name;
$city_id = $row->district_id;
?>
<option value="<?php echo $city_id; ?>" <?php if($district == $city_id){ echo
'selected="selected"';} ?>><?php echo $city_name; ?></option>
<?php
//echo '<option value="'.$city_id.'">'.$city_name.'</option>';
}
}
}
As per as discussed in the previous comments, I guess that property is a custom post type in your site. So if that is the scenario then this should be the solution.
<select name="state" id="state" class="select-submit2">
<option value="">Select state</option>
<?php
$property_id = $_GET['listing_edit']; //the PROPERTY_ID should be replaced with the original id might be ina get variable or any process by which you are using for the edit page.
$property_state = get_post_meta($property_id, META_KEY_STATE, true); //META_KEY_STATE is the meta_key name you use to store the value of the state in the postmeta table
$result=$wpdb->get_results("select * from states");
foreach($result as $row) {
$state_id=$row->state_id;
$state_name=$row->state_name;
?>
<option value="<?php echo $state_id; ?>" <?php if($property_state == $state_id){ echo 'selected="selected"';} ?>><?php echo $state_name; ?></option>
<?php
}
?>
</select>
Try this and let me know if you face any issues.
If this is what you are looking for?
<select name="state" id="state" class="select-submit2">
<option value="">Select state</option>
<?php
$result=$wpdb->get_results("select * from states");
foreach($result as $row) {
$state_id=$row->state_id;
$state_name=$row->state_name;
if($state_id == SELECTED_STATE_ID)
echo '<option value='.$state_id.' selected>'.$state_name.'</option>';
else
echo '<option value='.$state_id.'>'.$state_name.'</option>';
}
?>
</select>
just replace SELECTED_STATE_ID with variable which contains which state is selected
Here I am listing all cars.customers want to compare car so they will select from this drop down. A person can select multiple cars. At the first time he is selecting 'Audi' and Saab' I will store it into data base next if he came I need to populate Saab and audi as select how I can do this using php
<select name="cars" multiple>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
Here is my code
<select id="cars" class="multiselect" multiple="multiple" name="cars[]">
<?PHP
if($carslist->num_rows() >0)
{
foreach($carslist->result_array() as $entry):
?> <option value="<?php echo($entry['ID']); ?>" ><?php echo($entry['car_name']); ?></option>
<?php
endforeach;
}
?>
</select>
Following code I tried $resources contain select cars
<select id="cars" class="multiselect" multiple="multiple" name="cars[]">
<?PHP
if($carslist->num_rows() >0)
{
foreach($carslist->result_array() as $entry):
if($resources->num_rows() >0)
{
foreach($resources->result_array() as $car):
if($entry['ID'] == $employee['car_id'])
{
$select = 'selected="selected"';
}
else
{
$select = '';
}
endforeach;
}
?> <option value="<?php echo($entry['ID']); ?>" <?php echo $select;?> ><?php echo($entry['car_name']); ?></option>
<?php
endforeach;
}
?>
</select>
but it showing error
Here, try something like this, and see if it works:
Here is the controller:
<?php
function something(){
$data = array();
$data['cars'] = $this->some_model->some_function_to_return_cars_array();
$data['selected'] = $some_array_of_selected_cars();
$this->load->view('some_view', $data);
}
?>
And this is the view:
<select id="cars" class="multiselect" multiple="multiple" name="cars[]">
<option value="">Select:</option>
<?php
foreach( $cars as $key => $val ){
?>
<option value="<?php echo $val['some_id'] ?>"
<?php
if( in_array( $val['some_id'], $selected ) ) echo ' selected';
?>
><?php echo $val['some_name'] ?></option>
<?php
}
?>
</select>
I have the following:
PHP CODE:
<select name="valoracion<?php echo $i; ?>" id="valoracion<?php echo $i; ?>">
<?php
for($h=1; $h<6; $h++){
echo '<option value="'.$h.'">'.$h.'</option>';
}
?>
</select>
This prints:
<select name="valoracion0" id="valoracion0">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
When this <select> appears I want by default for the selected value to be the max number. How can I do this?
Like this..
<select name="valoracion<?php echo $i; ?>" id="valoracion<?php echo $i; ?>">
<?php
$cnt=6;$sel=""; // <--- Add a variable $sel with nothing assigned
for($h=1; $h<$cnt; $h++){
if($h==($cnt-1)) //<-- Here goes the check
{
$sel="Selected";
}
echo '<option '.$sel.' value="'.$h.'">'.$h.'</option>';
}
?>
</select>
You can try this,
<select name="valoracion<?php echo $i; ?>" id="valoracion<?php echo $i; ?>">
<?php
for($h=1; $h<6; $h++){
if($h == 5){
echo '<option value="'.$h.'" selected >'.$h.'</option>';
}else{
echo '<option value="'.$h.'">'.$h.'</option>';
}
}
?>
</select>
This should work:
<select name="valoracion<?php echo $i; ?>" id="valoracion<?php echo $i; ?>">
<?php
for($h=1; $h<6; $h++){
if($h == 5){ $selected = " selected=\"selected\""; }else{ $selected = NULL; }
echo '<option value="'.$h.'"'.$selected.'>'.$h.'</option>';
}
?>
</select>