How to Select Month from Combobox with php? - php

I have a combobox and It consists of the Month. the following code :
<select name='Bmonth' id='Bmonth'>
<option value='1'>January</option>
<option value='2'>February </option>
<option value='3'>March</option>
<option value='4'>April</option>
<option value='5'>May</option>
<option value='6'>June</option>
<option value='7'>July</option>
<option value='8'>August</option>
<option value='9'>September</option>
<option value='10'>October</option>
<option value='11'>November</option>
<option value='12'>December</option>
</select>
I want the page number is 4 received, Combobox to view 'April'. for example , if the address of page http//test.php?page=4,combobox view April ... No change in the combobox code!

You are passing a GET Parameter, so do:
<?php
//checking if GET URL exists, if yes store it in variable
$page = (isset($_GET['page']))?$_GET['page']:'';
?>
<!-- checking if variable value is 4, if yes, select the option -->
<option value='4' <?php echo ($page==4)?'selected':''?> >April</option>
and so on for all the options.
You could do this through javascript too, but since you haven't place javascript tag in your question, I'm not explaining that.

Define an array of months. Receive the month number via $_GET and store it in a variable. Get the corresponding month name from the array. Loop through the month array, and on each iteration, check if the month selected by the user is same as the current month ($name). If so, set $selected equal to selected='selected'. If not, set it to an empty string. Print the <option> tag.
<select name='Bmonth' id='Bmonth'>
<?php
$months = array(
1=>'January','February','March','April',
'May','June','July ','August','September',
'October','November','December',
);
$key = $_GET['page']; // Month number
$default = $months[$key]; // Month name
foreach ($months as $num => $name) {
$selected = ($name == $default) ? "selected='selected'" : "";
printf('<option value="%s" %s>%s</option>', $num, $selected, $name);
}
?>
</select>

Selecting a particular option in SELECT TAG works like this -
<SELECT>
<OPTION>Something
<OPTION SELECTED>Something Else // This option gets selected
In your case, you can assign the attr selected using prop() of jquery
<?php
$page = isset($_GET['page']) ? $_GET['page'] : "";
?>
$(document).ready(function(){
$("select").val("<?php echo $pg ?>").prop('selected', 'true');
});

Related

How do I get the selected option value as a string in PHP?

My aim is to get the selected item from the drop down and covert it to type string. After conversion, am comparing the result with another string and then use if() statement to execute some code. But the conversion does not seem to work because the program only execute else() statement. How can I do this? somebody help Please.
Below is my html and php code.
<select name="p_category">
<option selected disabled >-- select --</option>
<option value="computers">Computer</option>
<option value="phones" >Phone</option>
<option value="accessories">Accessory</option>
<option value="general">Display</option>
</select>
<?php
$getCategory = $_POST['p_category'];
$value = strval($getCategory);
$comps = "computer";
if($value == $comps){
echo "<script>alert('Equal')</script>";
}else{
echo "<script>alert('Not equal')</script>";
}
?>

How to sort a select option according to user input

Please I don't have any idea on how to achieve this so I need help. I have this code:
<select>
<option>Sunday</option>
<option>Monday</option>
/* this option goes down to Saturday */
</select>
And the user have to choose an option which is inserted into the database, let's assume the user chooses Tuesday and wants to edit it later on. Now I want the option that will be visible to be Tuesday eg:
<select>
<option>Sunday</option>
<option>Monday</option>
<option>Tuesday</option> /* this option will be the visible option before the user opens the selection */
</select>
And I want to achieve this through PHP and the variable assigned to the outputted database info is $day so $day = 'Tuesday'; please how do I achieve this?
To do this from within PHP you could do
<select>
<option <?PHP echo $day=='Sunday' ? '"selected=selected"' : ''); ?>>Sunday</option>
<option <?PHP echo $day=='Monday' ? '"selected=selected"' : ''); ?>>Monday</option>
<option <?PHP echo $day=='Tuesday' ? '"selected=selected"' : ''); ?>>Tuesday</option>
...
</select>
But it is not very elegant and gets difficult to code and more importantly maintain if the selection were to contain more that the seven days of the week.
So using an array of day names and a foreach loop and your $day variable containing the user selected day.
<?php
$dow = array('Sunday','Monday','Tuesday','Wednesday',
'Thursday','Friday','Saturday');
?>
<select name="DayOfWeek">
<?php
foreach ( $dow as $d ) :
$sel = $d == $day ? 'selected="selected"' : '';
echo "<option $sel>$d</option>";
endforeach;
?>
</select>
You can try this is example and you can apply for all of select options
<select>
<option <?PHP echo ($day=='Tuesday' ? selected : ""); ?>>Tuesday</option>
</select>

Displaying the assigned value in the MySQL DB in a select tag

when I'm displaying a user info, it has roles. God and Pesant. The code looks like this:
<td>
<select name='' id='' class='form-control'>
<option value='god'>God</option>
<option value = 'pesant'>Pesant</option>
</select>
</td>
When i'm displaying this info, how can I call from MySQL DB to show the value that is assigned to it? Right now it will always show God to everyone, and if I press Save, it saves the admin aka God mode to everyone.
Thanks. :)
You can check the values that you were getting from database as
<option value='god' <?php echo ($value === 'god') ? 'selected' : ''; ?>>God</option>
<option value ='pesant' <?php echo ($value === 'pesant') ? 'selected' : ''; ?>>Pesant</option>
This'll check that the values that you were getting from database matches with option value if yes then it'll place selected attribute else ''
Check & add selected attribute
<option value='god' <?php echo ($value == 'god') ? 'selected' : ''; ?>>God</option>
<option value ='pesant' <?php echo ($value == 'pesant') ? 'selected' : ''; ?>>Pesant</option>
Assuming $value will contain the value from database. If have run a query and fetch the value from database.

load select box with a selected option using php

Hi I have come across a problem now I do have a solution however it involves many of if statements and I would like to know it there is a neater option
what I would like is
<select>
<option value ="00:00">00:00</option>
<option value ="01:00">01:00</option>
<option value ="02:00">02:00</option>
<option value ="03:00">03:00</option>
</select>
then get an value from and have that box selected
what I'm am currently thinking is
$selectedbox = array();
if($tablevalue == "00:00"){
$selectedbox[0] = 'selected="selected"';
}
if($tablevalue == "01:00"){
$selectedbox[1] = 'selected="selected"';
}
then have
<select>
<option value ="00:00" <?php echo $selectedbox[0]; ?>>00:00</option>
<option value ="01:00" <?php echo $selectedbox[1]; ?>>01:00</option>
<option value ="02:00" <?php echo $selectedbox[2]; ?>>02:00</option>
<option value ="03:00" <?php echo $selectedbox[3]; ?>>03:00</option>
</select>
is there an easier way of getting the same result
for ($i = 0; $i <= 3; $i++) {
$sel = ("0{$i}:00" == $tablevalue) ? ' selected="selected"' : '';
echo <<<EOL
<option value="0{$i}:00"{$sel}>0{$i}:00</option>
EOL;
}
I would get rid of the if statements and put the code inline with the option tag with a ternary. Ex:
<select>
<option value ="00:00" <?=$tablevalue=="00:00"?'selected="selected"':''?>>00:00</option>
<option value ="01:00" <?=$tablevalue=="01:00"?'selected="selected"':''?>>01:00</option>
<option value ="02:00" <?=$tablevalue=="02:00"?'selected="selected"':''?>>02:00</option>
<option value ="03:00" <?=$tablevalue=="03:00"?'selected="selected"':''?>>03:00</option>
</select>
Take a look at PHP switch statements: http://php.net/manual/en/control-structures.switch.php
I think you would want something like this:
switch ($tablevalue) {
case "00:00":
$selectedbox[0] = 'selected="selected"';
break;
case "01:00":
$selectedbox[0] = 'selected="selected"';
break;
//etc, etc
}
PHP switch is a better way to handle if statements if you're always comparing the same variable.

Select an option in a select box, given the option in word form in a PHP form

I have a standard select box in my php form for my website containing a list of counties in the UK, an example of which is shown below:
<p class='form_title'>County<br>
<select id="county" name="county">
<optgroup label="England">
<option>Bedfordshire</option>
<option>Berkshire</option>
<option>Bristol</option>
....
<option>Wiltshire</option>
<option>Worcestershire</option>
</optgroup>
<optgroup label="Wales">
<option>Anglesey</option>
...
<option>Radnorshire</option>
</optgroup>
<optgroup label="Scotland">
<option>Aberdeenshire</option>
...
<option>Wigtownshire</option>
</optgroup>
<optgroup label="Northern Ireland">
<option>Antrim</option>
...
<option>Tyrone</option>
</optgroup>
</select>
</p>
Once a user has submitted details, they can then edit them.
I therefore need a way to have selected the option that they previously chose, given that I have the option they chose saved in word form, such as Bedfordshire.
I know I need to add the word selected to one of the options, but I was wondering if there was a better way to do it than a massive case statement.
Thanks
You should set an option value for each option to track the value.
Also you should be generating that from a database and then you can set selected based on a $_GET or $_POST:
for example:
foreach($county as $name){
$selected = '';
if($name == $_GET['name']){
$selected = ' selected="selected"';
}
echo '<option value="'.$name.'"'.$selected.'>'.$name.'</option>';
}
this doesn't include your optgroup but that also should come from database.
Try something like this:
foreach($places as $place) {
$selected = $prevSelectionId == $place['id'] ? " selected='selected' " : "";
echo "<option $selected value='".$place['id']."'>".$place['name']."</option>";
}
</select>

Categories