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.
Related
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>";
}
?>
I wanted to have a dropdown's selected value be determined by the results of the a query. But whatever I try, its selected value stays at the very first option.
I've browsed multiple questions with the same problem, but none solved my problem.
I've tried this:
<select name="Period">
<option value="Day" <?php if($PeriodTXT == "Day") echo "selected"?>>Day</option>
<option value="Week" <?php if($PeriodTXT == "Week") echo "selected"?>>Week</option>
<option value="Month" <?php if($PeriodTXT == "Month") echo "selected"?>>Month</option>
<option value="Year" <?php if($PeriodTXT == "Year") echo "selected"?>>Year</option>
</select>
And this:
<select name="Period">
<option <?php echo ($PeriodTXT == 'Day')?"selected":"" ?> >Day</option>
<option <?php echo ($PeriodTXT == 'Week')?"selected":"" ?> >Week</option>
<option <?php echo ($PeriodTXT == 'Month')?"selected":"" ?> >Month</option>
<option <?php echo ($PeriodTXT == 'Year')?"selected":"" ?> >Year</option>
</select>
But it won't work. Also when I use echo "$PeriodTXT"; it echos "Week" (exactly as written in the options of the dropdown), so it should've selected "Week" but it doesn't.
EDIT: $PeriodTXT is supposed to show the selected interval that came with a number count (example "3 Day" it would only keep the "Day" part)
$usersdata = array();
while($row = mysqli_fetch_array($result))
{
$usersdata = $row;
}
$PeriodTXT = preg_replace("/\d+/u", "", $usersdata[4]);
Your problem was extra whitespace around the text while you were removing count with regex, so hidden extra whitespace was yielding condition as false,
$PeriodTXT = '3 Week';
$PeriodTXT = preg_replace("/\d+/u", "", $usersdata[4]); //without trim()
var_dump($PeriodTXT); // outputs string ' Week' (length=5)
but when we trim it
$PeriodTXT = trim(preg_replace("/\d+/u", "", $usersdata[4])); //with trim()
var_dump($PeriodTXT); // outputs string 'Week' (length=4)
So use trim() to fix that, working eg,
<?php
$usersdata = array();
$usersdata[4] = '3 Week';
$PeriodTXT = trim(preg_replace("/\d+/u", "", $usersdata[4]));
?>
<select name="Period">
<option <?php echo ($PeriodTXT === 'Day')?"selected ='selected'":"" ?> >Day</option>
<option <?php echo ($PeriodTXT === 'Week')?"selected ='selected'":"" ?> >Week</option>
<option <?php echo ($PeriodTXT === 'Month')?"selected ='selected'":"" ?> >Month</option>
<option <?php echo ($PeriodTXT === 'Year')?"selected ='selected'":"" ?> >Year</option>
</select>
Also notice I have changed the HTML part from "selected":"" to "selected ='selected'":""
I understand you send a form using POST method, and after you send it, you would like to try display it with the selected option.
Please describe what is $PeriodTXT variable and how do you set it up.
Here is a solution to make it easy, hope it will help you.
$sent = $_POST['Period'];
$options = aray("Day", "Week", "Month", "Year");
echo '<form method="post" action="">
<select name="Period">';
foreach ($options as $v)
{
$selected = ($sent == $v) ? ' selected' : null;
echo '<option value="'.$v.'"'.$selected.'>'.$v.'</option>';
}
echo '</select>
<input type="submit" value="send">
</form>';
EDIT:
Code with "while" you have posted, seems to be wrong.
You are creating $userdata variable each time when while() goes. In fact it's not a real array, as you want it to be. It only works, when you select only one record from database (when mysql_num_rows($result) == 1). Otherwise it' wrong.
It should be done this way:
$usersdata = array();
while($row = mysqli_fetch_array($result))
{
$usersdata[] = $row;
}
$PeriodTXT = preg_replace("/\d+/u", "", $usersdata[$index][4]);
Notice, that you have to know which row ($index variable) you are doing changes in.
$usersdata becomes an Associative array.
Use var_dump() function to check what a variable contains itself. I hope it will get you closer to the solution. If you need more help, you have to post more code, because for now not everything is clear here.
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');
});
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>
I have a database of Employees and a dropdown list which I generated with PHP, then with javascript I tried to put the right type of blood of each employee in the database.
I have a database of Employees and a dropdown list which I generated with PHP, then with javascript I try to put the right type of blood for each employee in the database.
First I retrieved the data:
$rsEmployee = employee::getEmployees();
while($row = mysql_fetch_assoc($rsEmployee))
{
echo "
<select id='slcBloodType' name='slcBloodType' onload='chooseItem(this, '$row['slcBloodType']')'>
<option value='A+'> A+ </option>
<option value='A-'> A- </option>
<option value='B+'> B+ </option>
<option value='B-'> B- </option>
<option value='AB+'> AB+ </option>
<option value='AB-'> AB- </option>
<option value='O+'> O+ </option>
<option value='O-'> O- </option>
</select>";
}
The javascript function:
function elegirOption(list, value){
array = ["A+", "A-", "B+", "B-", "AB+", "AB-", "O+", "O-"];
index = -1;
for(var i = 0; i < array.length; i++){
if(value == array[i]){
index = i;
break;
}
}
if(index != -1){
this.selectedIndex = index;
}
}
But is not working, it seems that I can't pass a php variable as a parameter to a javascript function...
There are others dropdown list with which I want to do that.
Why not do it like this:
$blood_types = array("A+", "A-", ...);
echo '<select>';
while(....) {
foreach($blood_types as $blood_type) {
echo '<option';
if($blood_type == $row['slcBloodType'])
echo 'selected="selected"';
echo '>'.$blood_type.'</option>';
}
}
You don't have to specify the value attribute since it's the same as the text.
First you have several issues with your code such as duplicated IDs which is bad. Gere is how i'd do it, this is a massive cleanup of your code but should make it very close to what you had:
At the beginning of your page
It's always better to load your data in memory and then reuse it later below in your output. And no, unless you have 1 million lines, memory shouldn't be an issue...
<?php
//Read my data i need to work with
$rsEmployee = employee::getEmployee();
$employees = array();
while($row = mysql_fetch_assoc($rsEmployee)){
$employees[] = $row;
}
Further down in your page header
Now at this point we'll create the initialization code... You need JQUERY to make this work. It's a very good browser agnostic way to work and saves you HEAPS of trouble.
?><script>
$(function(){ <?php
foreach($employees as $employee){ ?>
$('#slcBloodType<?php echo $employee['id']; ?>').val('<?php echo $employee['slcBloodType']; ?>');
<?php } ?>
});
</script><?php
Further down in the HTML portion of your page
Allright, at this point, now you output the HTML and make sure your id's are unique:
<?php foreach($employees as $employee){ ?>
<select id="slcBloodType<?php echo $employee['id']; ?>" name="slcBloodType[<?php echo $employee['id']; ?>]">
<option value='A+'> A+ </option>
<option value='A-'> A- </option>
<option value='B+'> B+ </option>
<option value='B-'> B- </option>
<option value='AB+'> AB+ </option>
<option value='AB-'> AB- </option>
<option value='O+'> O+ </option>
<option value='O-'> O- </option>
</select>
<?php } ?>
Good luck applying this better coding technique, it'll save you HEAPS of trouble later if you start coding like this right now!
But is not working, it seems that I can't pass a php variable as a parameter to a javascript function...
This is because you cannot directly pass a PHP variable to javascript.
Your options are:
Setting cookies in PHP which javascript can later read
Have PHP echo the javascript code and inbetween have PHP variables printed out. For example:
< script language="javascript" type="text/javascript" >
var levels ="< ?php echo $levels ? >" ;
< /script >