Set_select() not working on CodeIgniter - php

I have a select box in my form and when I hit submit the form goes through validation and if an error happens, return all the value. This work expect for my select box, I have use the set_select but it doesnt work data comes in data base
<select name="leave_category_id" class="form-control" onchange="check_leave_category(this.value)" required >
<option value="" >Select Leave Category...</option>
<?php foreach ($all_leave_category as $v_category) :
?>
<option value="<?php echo $v_category->leave_category_id ?>" >
<?php echo set_select('leave_category_id',$cat); ?>
<?php echo $v_category->category ?> </option>
<?php endforeach;
?>
</select>

The set_select() function prints out the selected="selected" attribute in the <option> element where appropriate. But your code is as follows:
<option value="<?php echo $v_category->leave_category_id ?>" >
^
closing the tag --> |
<?php echo set_select('leave_category_id',$cat); ?>
<?php echo $v_category->category ?>
</option>
So when you look at the html, it will look something like this:
<option value="123">selected="selected" Category Name</option>
Got it? Your selected attribute is placed outside the tag. I guess you'll be able to fix it now ;)

Related

How to change form submission into SEO friendly URL

I have a simple form that compares two products using a select form and when user hits submit, it redirects to another site but the problem is the URL is rather long and ugly looking right now its displayed like this:
product_details.php?item1=core-i3-7300&submit=Compare&item2=core-i5-12400
I would like the form to submit in this URL structure:
product/item1/item2
Here is the code for my form, I also already have the HTACCESS rule created to accept the URL structure I want, I just need to correct format from the submission form.
<form action="compare/" method="GET" onSubmit="return validateThisFrom (this);" >
<select id="selectPlatform" Name="item1" required="required" class="classic">
<option value="">- Select Product -</option>
<?php foreach ($Names as $Name) : ?>
<option value="<?php echo $Name->Slug; ?>"><?php echo $Name->Make; ?> - <?php echo $Name->Name; ?></option>
<?php endforeach; ?>
</select>
<button input type="submit" name="submit" value="Compare" class="button red">
<select id="selectPlatform2" Name="item2" required="required" class="classic">
<option value="">- Select Product -</option>
<?php foreach ($Names as $Name) : ?>
<option value="<?php echo $Name->Slug; ?>"><?php echo $Name->Make; ?> - <?php echo $Name->Name; ?></option>
<?php endforeach; ?>
</select>
</form>

how to make the drop down option selected with database value using php

I'm having a select option in which values are populating from the database.
During updation I need to get that value selected.How can I do that?Please help.
Here is my code for select tag
<select name="supplier_id" class="form-control form-control-sm" id="colFormLabelSm">
<option value="0" selected="selected" disabled="disabled">
Select supplier
</option>
<?php
$supp_select = "SELECT supplier_id,supplier_name FROM supplier_details";
$supp_query = mysqli_query($con, $supp_select);
while ($supp_data = mysqli_fetch_assoc($supp_query)) {
?>
<option value="<?php echo $supp_data['supplier_id'] ?>">
<?php echo $supp_data['supplier_name']; ?>
</option>
<?php
}
?>
</select>
Put selected attribute on the option which is to be selected..Using if statement in option tag, check for which value is matching according to the value passed
E.g
<option <?php if(... ) echo "selected"; ?> value="value of db">

PHP: How to display select options if no value from database

I am trying to display my dropdown with a value from the database, but if the value is null I want it to show my options.
Currently it keeps showing me the blank select option.
<select class="form-control col-sm-5" id="freqlevels" name="freqlevels" value="<?php if ($customerinfo['freqlevel']) { echo h($customerinfo['freqlevel']);} else { echo "" ; } ?>"">
<option value=""></option>
<option value="Twice Weekly">Twice Weekly</option>
<option value="Weekly">Weekly</option>
<option value="Fortnightly">Fortnightly</option>
<option value="Monthly">Monthly</option>
</select>
Please can you suggest what I should do?
put your condition outside the value
<?php if ($customerinfo['freqlevel']) { echo value="$customerinfo['freqlevel']";}
hope this will resolve your problem
You need to make use of conditional statements.
<select name="something" id="my-select">
<option value="0">Everyone can see me</option>
<?php if (empty($array['some_key'])) : ?>
<option value="1">I'm only if some_key is empty</option>
..etc..
<?php endif; ?>
</select>
Then you can check values against the option value:
<option value="<?php echo $key; ?>"
<?php echo ($key === $_POST['some_key'] ? 'selected' : ''); ?>>
Hello, world
</option>

i want to display image in select option dynamically but not working

i want to show images in coming from database, but its not displaying. There is no error at all even i checked with console, but if i display image out of select its works.
How to display images?
<select data-show-subtext="true" class=" selectpicker bs-select form-
control" data-live-search="true" data-size="8" name="drawing">
<option value=""></option>
<?php foreach($get_drawing as $row): ?>
<option data-subtext="" value="<?php echo $row->drawing_id; ?>" >
<?php echo $row->drawing_name;?>
<img width="22%" height="10%" class="" src="<?php
echo base_url('drawing/fabricator/admin_3/'.$row->image); ?>">
</img>
</option>
<?php endforeach; ?>
</select>
Hope this will you
you can use data-subtext in the options
your select should be like this :
<select data-show-subtext="true" class=" selectpicker bs-select form- control" data-live-search="true" data-size="8" name="drawing">
<option value=""></option>
<?php foreach($get_drawing as $row): ?>
<option data-subtext="<img width='22%' height='10%'' src='<?=base_url("drawing/fabricator/admin_3/".$row->image);?>'>"
value="<?php echo $row->drawing_id; ?>" >
<?php echo $row->drawing_name;?>
</option>
<?php endforeach; ?>
</select>
Result Looks like :
<select class="selectpicker" data-size="5" tabindex="-98">
<option value="1" data-subtext="<img src='http://localhost/drawing/fabricator/admin_3/logo-dark.png'>">Ketchup</option>
</select>
for more : https://silviomoreto.github.io/bootstrap-select/examples/#styling
HTML default select allow only text display in option.
If you want to display images inside your select options you need to do it in javascript by creating a fake select acting on the real one on change.
There is a lot of jquery plugins doing that.
The most famous plugin is select2, you can find it here https://select2.org/ and an example in here https://select2.org/dropdown
You can add a image in dropdown option, it work only on Firefox:
<select>
<option style="background-image:url(apple.png);">Apple</option>
<option style="background-image:url(orange.png);">Orange</option>
</select>
Else, you can use a JS widget library as:
Select2: Exemple: https://jsfiddle.net/fr0z3nfyr/uxa6h1jy/
Bootstrap-select: https://silviomoreto.github.io/bootstrap-select/examples/#icons
You can add an image to select options using style attribute with background property
background: url('drawing/fabricator/admin_3/' + image ) no-repeat 100% 100%;'
For more information check my answer here also
https://stackoverflow.com/a/49469243/3134112

How to use select option value

Im trying to figure out how I can use both standard select option value together with a while loop select option value from mysql database. Right now I have this but won´t work any suggestions?
<option value="All">All</option>
<option value="<?php echo $row_list['name'] ;?>"><?php echo $row_list['name'];?></option>
<select ...>
<option value="standard">...</option>
<?php while(...) { output more options here } ?>
</select>
You can try this:
<?php
$cars=array("Mercedes", "Audi");//Let's have simple arrray with two objects
?>
<select>
<option value="All"> All</option>
foreach($cars as $car){//Let's iterate through array elements
?>
<option value="<?php echo $car;?>"> <?php echo $car;?> </option>
<?php
}
?>
</select>

Categories