I have a php drop down list which retrieves locations from a database, it gets all the data correctly, but sometimes if two records has the same in the database, it will add the item twice in the drop down. This is a location drop down, and some locations are duplicates, i would like to know what code can i add to remove duplicate entries and just keep one.
Here is my code:
<label for="select-service">
<strong>Enter a Location:</strong>
</label>
<select class="form-control" id="select-location" class="col-xs-12 col-sm-4 form-control" required>
<option value="">Select Location</option>
<?php
foreach($appointment_locations as $location) {
$LocationsArray = explode(",", $location->notes);
foreach($LocationsArray as $singleLocation):
?>
<option value="<?=$singleLocation ?>"><?=$singleLocation ?></option>
<? endforeach;
};?>
</select>
EDIT:
Here is the output
I tried using foreach(array_unique($appointment_locations) as $location) { but it doesnt show my second provider when i click on the location Rosebank
1) validate your inserted data to avoid such a situations
2) why you do not group by your retrieved data from database?
if you do not have access to do this, you can try a simple solution rather than the array_unique solution
$tmp = [];
foreach ($repeatedLocations as $location) {
if (isset($tmp[$location]) == false) {
// do your stuff
$tmp[$location] = true;
}
}
unset($tmp);
try
foreach(array_unique($appointment_locations) as $location) {
//... code
or
$LocationsArray = array_unique(explode(",", $location->notes));
https://www.php.net/manual/en/function.array-unique.php
Related
Here am having two tables namely tools and tool_use.
tools table looks like this
id name tools_names quantity type
13 cutting player cutting playerA,cutting playerB,cutting playerC 3 engineer
12 REFLECTORS REFLECTORSA,REFLECTORSB 2 team
tool_use table looks like this
id user_id type tools
8 siraj engineer cutting playerA,cutting playerB
7 siraj team REFLECTORSB
6 siraj team REFLECTORSA
i want to display the tools_names except inserted to tool_use table while inserting but the entire tools_names are displaying eventhough the result looks like in the table.here is my control
public function ajax_tools()
{
$data['tools']=$this->Tools_model->view_available_tools($_POST['type']);
foreach ($data['tools'] as $key=>$val) {
$data['toolss'][] =explode(',',$val['tools_names']);
}
$data['tools_names'] = $this->Tools_model->get_tool_names($_POST['type'])->result();
foreach ($data['tools_names'] as $row)
{
if (($key =array_search($row->tools,$data['toolss'])) !== false)
{
unset($data['toolss'][$key]);
$data['toolss'] = array_values($data['toolss']);
}
}
return $data['toolss'];
$this->load->view('ajax_tools',$data);
}
Here is my models
public function view_available_tools($type)
{
$this->db->order_by('id','desc');
$this->db->where('status',1);
$this->db->where('type',$type);
$query=$this->db->get('tools');
return $query->result_array();
}
public function get_tool_names($type)
{
return $this->db->get_where('tool_use',array('type'=>$type));
}
this is my view
<div class="form-group">
<label for="type" class="control-label">Type:</label>
<select name="type" id="type" class="form-control" required>
<option value="">please select</option>
<option value="team" <?php echo set_select('type','team'); ?>>Team</option>
<option value="engineer" <?php echo set_select('type','engineer'); ?>>Engineer</option>
</select>
</div>
<div class="form-group ">
<label for="tools" class="control-label">Tools:</label>
<select name="tools[]" id="tools" multiple="multiple" required>
<option value="">please select</option>
</select>
</div>
<script>
$('#type').change(function(){
var type=$('#type').val();
var url='<?php echo base_url(); ?>tools/tools_control/ajax_tools';
$.post(url, {type:type}, function(data)
{
$('#tools').html(data);
});
});
</script>
please help me to solve my issue
When you array_search, you're trying to search for $row->tools which supposedly contains cutting playerA,cutting playerB. And you search for that inside an array that does not contain the same kind of comma-separated lists of values but instead contains their exploded versions (as you did an explode on line 3).
if tools in table tool_use contain single values i find it out the solution but if it contains more values i mean stored as an array keeping me away from the destination please have a look
public function ajax_tools()
{
$data['tools']=$this->Tools_model->view_available_tools($_POST['type']);
foreach ($data['tools'] as $key=>$val)
{
$data['toolss'][] = $data['t']=explode(',',$val['tools_names']);
}
$data['tools_names'] = $this->Tools_model->get_tool_names($_POST['type'])->result();
var_dump($data['tools_names']);
foreach ($data['tools_names'] as $val)
{
if (($key =array_search($val->tools,$data['t'])) !== false)
{
unset($data['t'][$key]);
$data['t'] = array_values($data['t']);
}
}
$this->load->view('ajax_tools',$data);
}
I have a form on my website where one can select multiple other editors for a post.
The list of qualified editors is echoed by PHP, which works. But when multiple are selected HTML makes it into one list of the ids. F.E. If Jay (id = 4) and Sam (id = 9) are selected. The received value will be $_POST['editors'] = 49.
My code:
<select multiple class="editors" name="editors" id="editors">
<?php
//Gebruikers ophalen
$editorArr = getEditors();
foreach($editorArr as $editor){
echo "<option value='".$editor['id']."'>".$editor['email'].' - '.$editor['type']."</option>";
}
?>
</select>
and the handling PHP
$editors = htmlentities($_POST['editors']);
Okay! Try this:
<select class="editors" name="editors[]" id="editors" multiple>
<?php
//Gebruikers ophalen
$editorArr = getEditors();
foreach ($editorArr as $editor) {
echo "<option value='".$editor['id']."'>".$editor['email'].' - '.$editor['type']."</option>";
}
?>
</select>
The name should be an array so it can treat all selected values separately.
Hope it helps
services = $row['services_provided'];//services is in the projects table, cmt_services
<select name="services" multiple> <?php
dbConnect();
$result=mysql_query("SELECT * FROM cmt_services order servicesid");
while ($result2=mysql_fetch_array($result)) {
$Selectedservice="";
if (is_array($s_p)){
foreach ($services as $key=> $value) {
if ($result2['servicesid']==$value)
{
$Selectedservice = "selected";}
}
echo '<option value="'.$result2['servicesid'].'"'.$SelectedBus.'>'.$result2['service_name'].' '.$SelectedBus.'</option>';
}
}?>
</select>
//
Dropdown populates using data in cmt_services stored in project table using servicesid as 1,2,3. How can I display 1,2,3 as selected among 1,2,3,4,5,6?
There is a little trick in PHP where you use square-braces to tell PHP to expect an array:
<select name="services[]" multiple>
You should now find that you automatically have an array from your request.
$services = $_POST['services']; // this is an array
here I have stupid question, hope you can help me.
I create a menu using Select element and option like this:
<option selected="selected">Select type...</option>
<option value="1">Doctor</option>
<option value="2">Patient</option>
and every time I need to pick one value from this menu and use the submit button next to it to transfer data.
But every time the page refreshed, this menu will reveal: Select type...
I want it to reveal the value I chose last time, but don't know how.
Many thanks in advance!!
You'll want to move that selected="selected" onto the selected option.
Doing so in PHP isn't too rough. Just check the $_POST or $_GET (however you sent the form) value for your select box, such as $_POST["selectBox"] for each value down the list. When you find a match, echo out the selected="selected" string there. If the value was empty, output it on your default value.
The easiest way to achieve this is to populate the <select> options in an array, then loop through it to display the <option> list and mark them as selected is the $_POST variable matches the correct value:
<?php $myselect = array(1=>'Doctor', 2=>'Patient'); ?>
<select name="myselect">
<option>Select type...</option>
<?php foreach ($myselect as $value => $label): ?>
<option value="<?php echo $value; ?>"<?php if (isset($_POST['myselect']) && $_POST['myselect'] == $value) echo ' selected'; ?>>
<?php echo $label; ?>
</option>
<?php endforeach; ?>
</select>
<select name="myselect">
<?php
$myselect = array('Select type...','Doctor','Patient');
for($i=0; $i<=2; $i++){
echo "<option value=\"{myselect[$i]}\"";
if (isset($_POST['myselect']) && $_POST['myselect'] == $myselect[$i]){
echo 'selected=\"selected\"';
}
echo ">{$myselect[$i]}</option>";
}
?>
</select>
You have to use the server-side language of you choice to store the selected value in a database, xml or text file.
Edit : I think I may have misunderstood your question.
There are a few ways to do this.
On submit you can save that value as a $_SESSION value and use that to set the select on page load.
Using Javascript you can either set a cookie on change or alter the url to add a parameter (url?selecttype=1) and set that on page load using PHP.
There's a good use of cookies in JS on quirksmode: http://www.quirksmode.org/js/cookies.html
You need to change which one is selected to match the request....
function create_select($properties, $opts)
{
$out="<select ";
foreach ($properties as $propname=>$propval) {
$out.=" $propname='$propval'";
}
$out.=">\n";
foreach ($opts as $val=>$caption) {
$out.="<option value='$value'";
if ($_REQUEST[$properties['name']]==$val) $out.=" SELECTED";
$out.=">$caption</option>\n";
}
$out.="</select>";
return $out;
}
print create_select(array('name'=>'direction',
'id'=>'direction',
'class'=>'colourful',
'onChange'=>''),
array('N'=>'North',
'S'=>'South',
'E'=>'East',
'W'=>'West'));
I've a select menu hardcoded in a wordpress (php) theme, but the manager requires to edit those frequently. Is it possible to populate the select dropdown options from a text file import? So he just has to edit the text file and the menu options would change.
The current menu looks like this:
<select name="location" id="sort-location" class="sort-dropdown">
<option value="" selected="selected">LOCATION:</option>
<option value="" disabled="">--------------</option>
<option value="hongkong">Hong Kong</option>
<option value="taiwan">Taiwan</option>
<option value="mainland_china">Mainland China</option>
<option value="" disabled="">--------------</option>
<option value="">SHOW ALL</option>
</select>
Sure -- make yourself a small loop that runs through the lines in the format you choose.
<?php
$select = file_get_contents('select.txt');
$lines = explode("\n", $select);
foreach ($lines as $line) {
// let's say our format is like this:
// value|name|selected|disabled
// or:
// -
// for separator
if ($line == '-') {
echo '<option disabled="disabled">----------</option>';
} else {
list($value, $name, $selected, $disabled) = explode("|",$line);
echo '<option value="'.$value.'"',
$selected?' selected="selected"':'',
$disabled?' disabled="disabled"':'',
'>'.$name.'</option>';
}
}
?>
Sure, just fetch the text file into an array using file() and create the select out of it. A very simple implementation: menu.txt:
hongkong Hong Kong
taiwan Taiwan
mainland_china Mainland China
Note the tabs between value and label.
Then in PHP:
$menu_items = file("menu.txt");
foreach ($menu_items as $menu_item)
{
// Explode
$menu_item_exploded = explode("\t", $menu_item);
$option_value = htmlspecialchars(trim($menu_item_exploded[0]));
$option_label = htmlspecialchars(trim($menu_item_exploded[1]));
echo "<option value='$option_value'>$option_label</option>";
}
As far as I can see, you have the following left to solve:
How to pre-set a predefined value (You need to echo selected in the right item)
How to deal with the user editing out a value from the text file that was pre-set in your select.
Error handling if the file is not existent or not accessible
Error handling if the user screws up the line breaks, or something similar - maybe count the lines, and/or detect whether there are tabs inside the file