I have the following drop down list, that has a common item 'a'. This item is shown two times in output, but it want to have it only once. Can anybody help me?
<select name="">
<option value="a">a</option>
<option value="a">a</option>
<option value="b">b</option>
</select>
My code is
<select name="brand" id="brand" class="txtfld" >
<option value="">Select</option>
<?php
$country_sql="SELECT DISTINCT(brand) FROM customer where status='A' and brand<>''";
$result_country=executequery($country_sql);
while($country_array=ms_stripslashes(mysql_fetch_array($result_country)))
{
$sel_con=($country_array['brand']==$_REQUEST['brand'])? " selected='selected'" : " ";
$brand=$country_array['brand'];
$brand2=explode('|',$brand);
if(count($brand2)<2)
{
?><option value="<?=$brand;?>" ><?=$brand;?></option><?php
}
else
{
for($i=0;$i<count($brand2);$i++)
{
?><option value="<?=$brand2[$i];?>" ><?=$brand2[$i];?></option><?php
}
}
}
?>
</select>
You can use array_unique() which will keep only unique values of your array :
$brand2 = explode('|',$brand);
$brand2 = array_unique($brand2);
EDIT
Try :
$new_array = array();
while($country_array=ms_stripslashes(mysql_fetch_array($result_country)))
{
$brand=$country_array['brand'];
$brand2=explode('|',$brand);
$NewArray = array_unique (array_merge ($NewArray, $brand2));
}
Now you have all unique value in array, loop through to show the list.
NOTE: It might be a overhead if you have big data.
Related
I have a dropdown to display price ranges for searching.
HTML for dropdown:
<select name="price">
<option value="">All (-)</option>
<option value="">Rs.400 to Rs.1,000 (3)</option>
<option value="">Rs.1,000 to Rs.2,000 (6)</option>
<option value="">Rs.2,000 to Rs.4,000 (1)</option>
<option value="">Rs.4,000+ (1)</option>
</select>
Using this option values, now I need to separate first and second price from user selection.
Eg. If someone select Rs.400 to Rs.1,000 (3), then I need to get 400 and 1,000.
Can anybody tell me how can I do this in php. I tired it with php explode. but I could not figure this out correctly.
Hope somebody may help me out.
Thank you.
What about something like this:
HTML
<select name="price">
<option value="all">All (-)</option>
<option value="400|1000">Rs.400 to Rs.1,000 (3)</option>
<option value="1000|2000">Rs.1,000 to Rs.2,000 (6)</option>
<option value="2000|4000">Rs.2,000 to Rs.4,000 (1)</option>
<option value="Over">Rs.4,000+ (1)</option>
</select>
PHP
<?php
//Get the data from the form
$price = $_POST['price'];
$prices = explode("|", $price);
?>
For example, if the user selects "Rs.400 to Rs.1,000", the value of $prices will be:
$price[0] = "400";
$price[1] = "1000";
I hope that helps. If it doesn't, I suppose I wasn't clear on what you were asking.
Inspired by a previous answer. I'd suggest.
HTML
<select name="price">
<option value="|">All (-)</option>
<option value="400|1000">Rs.400 to Rs.1,000 (3)</option>
<option value="1000|2000">Rs.1,000 to Rs.2,000 (6)</option>
<option value="2000|4000">Rs.2,000 to Rs.4,000 (1)</option>
<option value="4000|">Rs.4,000+ (1)</option>
</select>
PHP
<?php
function get_numeric($val) {
if (is_numeric($val)) {
return $val + 0;
}
return false;
}
$price_selected = isset($_REQUEST['price']) && trim($_REQUEST['price'])!="" ? $_REQUEST['price'] : "|"; // sets a default value
list($lower, $upper) = array_map('get_numeric',explode('|', $price_selected, 2));
var_dump($lower); // false when there's no lower limit
var_dump($upper); // false when there's no upper limit
?>
I have this HTML/PHP Code that lists options in a select element.
Whats the best way to make the correct option selected based on a record from a MySQL database:
This works fine, but is there any easier way to do it with one line of code rather than doing an if statement per option?
<select name="status" id="status">
<option value="Open"<?php if($ticket["status"]=="Open"){echo('selected="selected"');}?>>Open</option>
<option value="Needs Action"<?php if($ticket["status"]=="Needs Action"){echo('selected="selected"');}?>>Needs Action</option>
<option value="Customer Reply"<?php if($ticket["status"]=="Customer Reply"){echo('selected="selected"');}?>>Customer Reply</option>
<option value="Completed"<?php if($ticket["status"]=="Completed"){echo('selected="selected"');}?>>Completed</option>
</select>
use an array:
echo "<select name='status' id='status'>";
$statuses = array('Open', 'Needs Action', 'Customer Reply', 'Completed');
foreach ($statuses as $status) {
echo "<option value='$status' " . ($ticket['status'] == $status) ? "selected='selected'" : "" . "/>";
}
echo "</select>";
You may want to put those values in the database or if you want to avoit it, just put them into an array and print all the options using a loop and mark as selected the right one.
Yes, the code can be shorter (and much more readable):
if($ticket["status"]=="Open") {
$openSelected = "selected='selected'";
}
if($ticket["status"]=="Needs Action") {
$needsActionSelected = "selected='selected'";
}
if($ticket["status"]=="Customer Reply") {
$customerReplySelected = "selected='selected'";
}
if($ticket["status"]=="Completed") {
$completedSelected = "selected='selected'";
}
<select name="status" id="status">
<option value="Open" <?php print($openSelected) ?>>Open</option>
<option value="Needs Action"<?php print($needsActionSelected) ?>>Needs Action</option>
<option value="Customer Reply"<?php print($customerReplySelected) ?>>Customer Reply</option>
<option value="Completed"<?php print($completedSelected) ?>>Completed</option>
</select>
EDIT// #Barmar has a better answer.
I have a db_field (shortcode) that returns a string when submited query. the value from that return is AB,EF,GH (exactly this!
The second part is that i have a textarea with a list of that shortcodes. So i trying to highlight (selected) the same macthed elements.
for example:
$String_in_Database = AB,EF,GH;
Wish to have that:
<select name="Country[]" id="Country" multiple="multiple" size="5">
<option value="AB" selected>AB</option>
<option value="CD">CD</option>
<option value="EF" selected>EF</option>
<option value="GH" selected>GH</option>
......
</select>
This is how i generate the options:
<?php $MyArray = $settingsUser['set_disallowcountries']; ?>
<?php foreach($disallCountry as $key => $value) { ?>
<option value="<?php echo $value['short'] ?>" <?php if(is_array($value['short'], $MyArray)) { echo 'selected'; }?>><?php echo $value['long'] ?></option>
<?php } ?>
Maybe you want to explode this string into array?
$MyArray = explode(",",$String_in_Database);
Im slightly unsure to the question, so you want to check if the user has the selected country and have it selected in the option dropdown/box?
<?php
$userSelected= explode(",", $string_in_database);
$allCountries = array('AA', 'BB', 'CC');
foreach($allCountries as $country)
if(in_array($country["short"], $myArray){
$selected = 'selected';
} else {
$selected = '';
}
echo '<option value="'.$country["short"].'" '.$selected.'>'.$country["long"].'</option>';
}
With this, if the user string from the database (im guessing that is the users settings) is the same as AA, BB or CC, they will be selected, if it is not, they won't be selected.
To my knowledge I think this should work, I may be wrong as I have not tested it though. Just my thoughts!
I am adding a feature to edit the ads a user puts. For that I want to make the value selected which is selected by the user when he entered the values . How can I implement this?
<div class="nm">
Category
</div>
<div class="fl">
<select name="category" id = "ad_category" onchange="cangeSubCat(this.value)" >
<option value="">---Select Category---</option>
<option value="real_estate">Real Estate</option>
<option value="hotels_resturants">Hotels and Resturants</option>
<option value="car">Car Rental</option>
</select>
</div>
Add the selected HTML <option> attribute, in XHTML it is selected="selected".
<option value="value" selected>title</option>
^^^^^^^^
Documentation: http://www.w3.org/TR/html4/interact/forms.html#h-17.6.1
I mean how will I know which one should be selected..it will be different for different users . I wnt it to be selected which user selects during adding the ad
That depends on your code. You can do this with an array with all value/title pairs (value => title) and the value which is selected. Then when key (value) equals the selected one, the attribute is added in output. As it's an array it's easy to iterate over it.
$otpions = array(
'a' => 'A',
'b' => 'B',
);
$selected = 'b';
echo '<select>';
foreach ($options as $value => $title)
{
printf('<option value="%s" %s>%s</option>',
$value, $selected == $value ? 'selected' : '', $title);
}
echo '</select>';
okay lets assume you fetch value from db and that select box value is in $select variable then you've to write
<option <?php if($select=="real_estate") { echo "selected='selected'"; } ?> value="real_estate">Real Estate</option>
<option <?php if($select=="hotels_resturants") { echo "selected='selected'"; } ?> value="hotels_resturants">Hotels and Resturants</option>
<option <?php if($select=="car") { echo "selected='selected'"; } ?> value="car">Car Rental</option>
You can use this magic function
function __selectedDb($ctrlValue,$dbValue)
{
if($ctrlValue == $dbValue)
return "selected='selected'";
else
return "";
}
like
<select name="category" id = "ad_category" onchange="cangeSubCat(this.value)" >
<option value="" <?php echo __selectedDb("","$cat")?>>---Select Category---</option>
<option value="real_estate" <?php echo __selectedDb("real_estate","$cat")?>>Real Estate</option>
<option value="hotels_resturants" <?php echo __selectedDb("hotels_resturants","$cat")?>>Hotels and Resturants</option>
<option value="car" <?php echo __selectedDb("car","$cat")?>>Car Rental</option>
</select>
I write in core php please convert php code to smarty
I have , within a foreach , a dropdown:
`<select name="position[]">
<option value="1st">First</option>
<option value="2nd">Second</option>
<option value="3rd">Third</option>
</select>`
I need to be able to get the values from position[]when the form is posted
I had assumed it was $_POST['position'][0] , $_POST['position'][1] etc.
But that doesn't work .
Try this:
<?php
foreach($array as $key=>$value){ ?>
<select name="position[<?php echo $key; ?>]">
<option value="1">First</option>
<option value="2">Second</option>
<option value="3">Third</option>
</select>
<?php } ?>
You should then be able to access each select like this:
$_POST['position'][$key]
You have not included multiple in html code of select.
You should use
<select name="name[]" multiple size"5">
Try this:
$test=$_POST['position'];
if ($test){
foreach ($test as $t){
echo 'You selected '.$t.'<br />';
}
}
and also in select tag enable multiple selection by:
<select name="position[]" multiple="multiple">