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
?>
Related
I have a huge number of records in a drop-down box(approximately 27000 or more).
HTML -
<select name="manufacturer" id="manufacturer">
<option value="">Select a Builder</option>
<option value="1-hull">#1 Hull</option>
<option value="49er">49er</option>
<option value="a-m-f">A M F</option>
<option value="a-m-manufacturing">A&M Manufacturing</option>
<option value="a1-custom-trailers">A1 Custom Trailers</option>
- - - - - -
- - - - - -
<option value="zuma">Zuma</option>
</select>
Above drop-down I collect this data from different website. I want to put in my own website with dynamic content. I have tried to insert one by one it is not possible to insert all record in single handed. So I want to any shortcut method for insert all these text in my MySQL database table. I need option inner HTML text will inserting into my database table.
If I will get all the option text in an Array then easily I can manage it. But I am confused how to get all text in Array by using PHP code.
Here's a solution with preg_match_all
<?php
$string = '<option value="">Select a Builder</option>
<option value="1-hull">#1 Hull</option>
<option value="49er">49er</option>
<option value="a-m-f">A M F</option>
<option value="a-m-manufacturing">A&M Manufacturing</option>
<option value="a1-custom-trailers">A1 Custom Trailers</option>
<option value="zuma">Zuma</option>
';
preg_match_all('/>(.*?)<\//',$string,$matches);
var_dump($matches[1]);
?>
DOM Document is your buddy,
$str = '<select name="manufacturer" id="manufacturer">
<option value="">Select a Builder</option>
<option value="1-hull">#1 Hull</option>
<option value="49er">49er</option>
<option value="a-m-f">A M F</option>
<option value="a-m-manufacturing">A&M Manufacturing</option>
<option value="a1-custom-trailers">A1 Custom Trailers</option>
<option value="zuma">Zuma</option>
</select>';
$doc = new DOMDocument();
$doc->loadHTML($str);
$options = $doc->getElementsByTagName('option');
foreach ($options as $option) {
$value[] = $option->nodeValue;
}
$value; // will contain array with all option values
Why you should not parse HTML with regex?
I've tried various thing to get to what I want but i'm in the woods completely.
Let me explain what is required.
I have a php variable containing a string equal to an option's value and I need to get the option corresponding to the string to be selected.
This is the select tag.
<select name="pos" id="pos">
<option value="">POS</option>
<option value="C">C</option>
<option value="LW">LW</option>
<option value="RW">RW</option>
<option value="LD">LD</option>
<option value="RD">RD</option>
<option value="G">G</option>
</select>
Example :
$string = "G";
Resulting select tag :
<select name="pos" id="pos">
<option value="">POS</option>
<option value="C">C</option>
<option value="LW">LW</option>
<option value="RW">RW</option>
<option value="LD">LD</option>
<option value="RD">RD</option>
<option value="G" selected>G</option>
</select>
Thanks a lot guys. I've been working on this forever and got no where.
Using jQuery:
// find required element
var el = jQuery('select#pos option[value="G"]')
// or
var el = jQuery('select#pos option').filter(function(){return jQuery(this).text()=="G"})
// set 'selected' attribute
el.attr("selected", "selected");
// find selected option
var el = jQuery('select#pos option:selected');
// get value
el.text()
i got this variable:
$machine=isset($_POST['machine'])?$_POST['machine']:'';
for my selectbox:
<select id="machine" name="machine">
<option value=""></option>
<option value="1">A</option>
<option value="2">B</option>
<option value="3">C</option>
<option value="4">D</option>
</select>
now i want to made some modifications. i have saved in my settings $favmachine=4.
for example i load the page with
if($favmachine!=0)
{
$machine=$favmachine;
}
my form looks like this (exactly what i want):
<select id="machine" name="machine">
<option value=""></option>
<option value="1">A</option>
<option value="2">B</option>
<option value="3">C</option>
<option value="4" selected="selected">D</option>
</select>
the problem now is when i want to change for example from D to A it goes to D again.. right exactly what i wrote... but i dont know how to handle this problem
i want my $favmachine selected on pageload (works) BUT want to be able to change it to another machine and set this to $machine by $_POST
hope you understand my problem :D
I think you want to overwrite $machine with $favmachine only if the check for a POST value resulted in “nothing” before:
$machine=isset($_POST['machine'])?$_POST['machine']:'';
if($machine==='' && $favmachine!=0)
{
$machine=$favmachine;
}
or in shorter form, using the ? operator once more:
$machine = isset($_POST['machine']) ?
$_POST['machine']
:
( $favmachine != 0 ? $favmachine : '');
(Multiple lines and indentation for readability only.)
I have this php code that input selected="" at a specific url. I need selected="" to in inserted into my html.
<?php
$uri = $_SERVER['REQUEST_URI'];
if ( strpos($uri,'retailers/amazon/?sort=2') !== false ) {
echo 'selected=""';
} else {
echo 'test';
}
?>
This is the html, I need to insert selected=""... Something like this
<option selected="" value="retailers/amazon/?sort=2">Newest to Oldest</option>
...
<select onchange="window.location=this.value;">
<option value="">Select</option>
<option selected="" value="retailers/amazon/?sort=2">Newest to Oldest</option>
<option value="retailers/amazon/?r_sortby=highest_rated&r_orderby=desc">Success Rate: High to Low</option>
<option value="retailers/amazon/?r_sortby=highest_rated&r_orderby=asc">Success Rate: Low to High</option>
<option value="retailers/amazon/?sort=0">Most Comments</option>
</select>
How about this...
<?php
$uri = $_SERVER['REQUEST_URI'];
$sort2 = strpos($uri, 'retailers/amazon/?sort=2');
?>
<option <?php echo ($sort2 === false? '' : 'selected=""'); ?> value="retailers/amazon/?sort=2">Newest to Oldest</option>
I assume you are trying to access the echo command outside the php tag and somewhere in html
it is not possible but there is an solution for this.
so you can try this option.
<select onchange="window.location=this.value;">
<option value="">Select</option>
<option selected="?php echo $uri?" </option>
</select>
its mean you are using the php code within the html area and you can access every variable of php by this.
I'm using search with combo box
Here is my combo box source code
<select name="salary" class="styled">
<option selected="selected" value=''>Any</option>
<option value="10000">10,000</option>
<option value="20000">20,000</option>
<option value="30000">30,000</option>
<option value="40000">40,000</option>
<option value="50000">50,000</option>
<option value="60000">60,000</option>
<option value="70000">70,000</option>
<option value="80000">80,000</option>
<option value="90000">90,000</option>
<option value="100000">100,000</option>
<option value="110000">110,000</option>
<option value="120000">120,000</option>
<option value="130000">130,000</option>
<option value="140000">140,000</option>
<option value="150000">150,000</option>
</select>
I would like to select value based on $salary=$_GET['salary']; if $salary empty I need to select first one as selected
To set a default value for an HTML select element, you need to give the appropriate <option> element the selected attribute. It would look like this:
<option value='50000' selected='selected'>50,000</option>
In your PHP code, you would add this by setting a string for each option, to either "selected='selected'" or blank, depending on whether that option is the one you want to have selected.
This is obviously far easier to put into a loop rather than writing the same code twenty times, so you would need to rewrite your output to create a loop for creating your options.
Hope that helps.
You can do something like this on creation...[not tested]
<?php
$options = array(
'10000' => '10,000',
...
'150000' => '150,000'
); //From MySQL
array_unshift($options, '' => 'Any');
$salary = isset($_GET['salary'])?$_GET['salary']:'';
?>
<select name="salary" class="styled">
<?php foreach ($options as $value=>$text){ ?>
<option <?php if($value == $salary){echo 'selected="selected"'; }?> value="<?php echo $value; ?>"><?php echo $text; ?></option>
<?php } ?>
</select>
If I understand your question correctly you want that if the person doesn't select any thing you want 10000 to be automatically selected for him. And I also assume that the options in the select list are static. So..
<?php
if($_GET['salary']=='')
$salary = 10000;
else
$salary = $_GET['salary'];
?>