Limit of number of checks in a PHP If statement - php

I'm new to PHP and HTML I have several other IF statements with two or three OR in it and they seem to work just fine.
What I'm checking for is 'NULL', 'blank', 'space' or text 'None'. If I find any of these I want to do the select statement. If no matches I would like to use the $AT_Med_1_1 data which has been pull out of a database.
IF statement as shown below which does not see me to work. It always drops into the SELECT statement even when $AT_Med_1_1 is something else.
if ($AT_Med_1_1 == '' or $AT_Med_1_1 == NULL or $AT_Med_1_1 == ' ' or $AT_Med_1_1 = 'None') {
<select name="AT_Med_1_2">
<option value="None">None</option>
<option value="Oral_Zofran">Oral Zofran</option>
<option value="IV_Zofran">IV_Zofran</option>
<option value="Oxygen">Oxygen</option>
<option value="Ibuprofen">Ibuprofen</option>
<option value="Aetaminothen">Aetaminothen</option>
<option value="Upinephrne">Upinephrne</option>
<option value="Xopenex">Xopenex</option>
<option value="Albuterol">Albuterol</option>
<option value="Valium">Valium</option>
<option value="Magnesiunsulphate">Magnesiunsulphate</option>
<option value="Diphenhyamine">Diphenhyamine</option>
<option value="Ketorolac">Ketorolac</option>
<option value="Promethazion">Promethazion</option>
<option value="Oral_Fluids_Soup">Oral Fluids Soup</option>
<option value="Oral_Fluids_Electrolyte">Oral Fluids Electrolyte</option>
<option value="Oral_Fluids_Water">Oral Fluids Water</option>
</select>
<?php
} else {
// echo 'Showing $AT_';
echo $AT_Med_1_2;
echo nl2br("\n");
}
?>
If I change the IF statement to several elseif it seem to work correctly
if ($AT_Med_1_1 == '')
{
goto test1;
} else if ($AT_Med_1_1 == NULL)
{
goto test1;
} else if ($AT_Med_1_1 == ' ')
{
goto test1;
} else if ($AT_Med_1_1 == 'None')
{
goto test1;
}
goto test;
test1:
?>
<select name="AT_Med_1_1">
<option value="None">None</option>
<option value="Oral_Zofran">Oral Zofran</option>
<option value="IV_Zofran">IV_Zofran</option>
<option value="Oxygen">Oxygen</option>
<option value="Ibuprofen">Ibuprofen</option>
<option value="Aetaminothen">Aetaminothen</option>
<option value="Upinephrne">Upinephrne</option>
<option value="Xopenex">Xopenex</option>
<option value="Albuterol">Albuterol</option>
<option value="Valium">Valium</option>
<option value="Magnesiunsulphate">Magnesiunsulphate</option>
<option value="Diphenhyamine">Diphenhyamine</option>
<option value="Ketorolac">Ketorolac</option>
<option value="Promethazion">Promethazion</option>
<option value="Oral_Fluids_Soup">Oral Fluids Soup</option>
<option value="Oral_Fluids_Electrolyte">Oral Fluids Electrolyte</option>
<option value="Oral_Fluids_Water">Oral Fluids Water</option>
</select>
<?php
goto endtest;
test:
echo $AT_Med_1_1;
echo nl2br("\n");
endtest:

if (... or $AT_Med_1_1 = 'None') {
For the last condition you accidentally wrote = instead of ==.

Related

How to make a sort script with PHP

This is my HTML Code
<form action="sort.php" method="post" >
<select name="option">
<option value="1">Select a category!</option>
<option value="2">Category 1<option>
<option value="3">Category 2</option>
</select>
<select name="option">
<option value="a">Select a 2nd category!(Optional)</option>
<option value="b">Category 1</option>
<option value="c">Category 2</option>
</select>
This is my PHP Code:
<?php
if (isset($_POST['option']))
{
if ($_POST['option'] == '1') { header('Location: /website'); }
elseif ($_POST['option'] == '2') { header('Location: https://example.com/2'); }
elseif ($_POST['option'] == '2') { header('Location: https://example.com/2'); }
elseif ($_POST['option'] == '2') { header('Location: https://example.com/2'); }
elseif ($_POST['option'] == '3') { header('Location: https://example.com/3'); }
elseif ($_POST['option'] == '4') { header('Location: https://example.com/4'); }
elseif ($_POST['option'] == '5') { header('Location: https://example.com/5'); }
elseif ($_POST['option'] == '6') { header('Location: https://example.com/6'); }
}
?>
I want to make it so if someone select value "1" & value "a".Then X Link will open. And I want to make the 2nd button optional. If someone leaves it empty, it will not have any effect. How can I do so?
No need mutiple elseif
$arr =array(2,3,4,5,6);
if ($_POST['option'] == '1')
{
header('Location: /website');
}
elseif($_POST['option'] != '' && in_array($_POST['option'],$arr) )
{
header('Location: example.com/'.$_POST['option']);
}
else
{
echo "may be empty";
}
Note: change your input name and set required to first one
<select name="option" required>
<option value="">Select a category!</option>
<option value="1">Category 1<option>
<option value="2">Category 2</option>
<option value="3">Category 3</option>
<option value="4">Category 4</option>
<option value="5">Category 5</option>
<option value="6">Category 6</option>
</select>
<select name="option1">
<option values="">Select a 2nd category!(Optional)</option>
<option value="1">Category 1<option>
<option value="2">Category 2</option>
<option value="3">Category 3</option>
<option value="4">Category 4</option>
<option value="5">Category 5</option>
<option value="6">Category 6</option>
</select>

how to edit state and country list and update it using php

Hi i am storing countries and states in database i need to edit and update it in database but when i click on edit option iam not able to fetch the record from database.Here is my code.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://lab.iamrohit.in/js/location.js"></script>
Country:<select name="country" class="countries" id="country" value="<?php echo $row['country'];?>">
<option >Select Country</option>
</select><br/>
State:<select name="state" class="states" id="state" value="<?php echo $row['state'];?>">
<option >Select State</option>
</select><br/>
If i click on console the value is printing but iam not able to display in frontend.Can anyone help me regarding this.
please use this code.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://lab.iamrohit.in/js/location.js"></script>
Country:<select name="country" class="countries" id="country">
<option value="<?php echo $row['country'];?>"><?php echo $row['country'];?></option>
<option >Select Country</option>
</select><br/>
State:<select name="state" class="states" id="state">
<option value="<?php echo $row['state'];?>"><?php echo $row['state'];?></option>
<option >Select State</option>
</select><br/>
There were few things which were wrong.
Option should be inside select box
select value should be based on database condition
Thanks
Amit
Select box don't contain value attribute. You are going by wrong way.
You can use select box by this way:-
<select name="country">
<option value="country1" <?php if ($row['country'] == 'country1') { echo ' selected="selected"'; } ?>>country1</option>
<option value="country2" <?php if ($row['country'] == 'country2') { echo ' selected="selected"'; } ?>>country2</option>
<option value="country3" <?php if ($row['country'] == 'country3') { echo ' selected="selected"'; } ?>>country3</option>
</select>
<select name="state">
<option value="state1" <?php if ($row['state'] == 'state1') { echo ' selected="selected"'; } ?>>state1</option>
<option value="state2" <?php if ($row['state'] == 'state2') { echo ' selected="selected"'; } ?>>state2</option>
<option value="state3" <?php if ($row['state'] == 'state3') { echo ' selected="selected"'; } ?>>state3</option>
</select>
Hope it will help you :)
You can't assign value attribute to Select Box.So you can use Like this:
<select name="country">
<option value="country" <?php if ($row['country'] == 'country') { echo ' selected="selected"'; } ?>>country</option>
<select name="state">
<option value="state12" <?php if ($row['state'] == 'state12') { echo ' selected="selected"'; } ?>>state12</option>

Is there a quicker way to select the "selected" value of an HTML dropdown in PHP?

I'm currently using this garbage code to select the correct selected="selected" value for my HTML dropdown. There has got to be a better way... maybe with a switch statement or something else?
Any tips on how to make this more efficient?
if ($quantity == 0)
{
echo '
<option selected="selected" value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9+">9+</option>
';
}
else if ($quantity == 1)
{
echo '
<option value="0">0</option>
<option selected="selected" value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9+">9+</option>
';
}
else if ($quantity == 2)
{
echo '
<option value="0">0</option>
<option value="1">1</option>
<option selected="selected" value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9+">9+</option>
';
}
else if ($quantity == 3)
{
echo '
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option selected="selected" value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9+">9+</option>
';
} etc....
This should work for you:
echo "<select>";
for($count = 0; $count < 10; $count++)
echo "<option " . ($quantity == $count ? 'selected': '') . " value='$count'>$count</option>";
echo "</select>";
You could loop through them and print each one's HTML code, like this:
for($i=0;$i<10;$i++){
$value=$i;
if($value==9) $value="9+";
if($i==$quantity){
echo '<option value="' . $value . '" selected="selected">' . $value . '</option>'
}
else{
echo '<option value="' . $value . '">' . $value . '</option>'
}
}
Using a loop is all you need :
$out='<option';
for($i=0;$i<10;i++){
if($quantity==$i)
$out.='selected="selected" ';
$out.=" value='$i'>$i</option";
echo $out;
}
I usually interpolate the 'selected' variable in to the loop
for($i=0;$i<10;$i++){
$selected = $i==$quantity? 'selected="selected"':'';
echo "<option value=\"$i\" $selected>$i</option>";
}
loop over it:
for($i = 0; $i < 9; $i++){
if($i == $quantity){
$selected = 'selected="selected"';
}else{
$selected = '';
}
$i2 = ($i == 9) ? '9+' : $i;
$select_options .= '<option value="'.$i2.'" '.$selected.'>'.$i2.'</option>';
}
echo $select_options;
Edit: looks like you can't set selected in the select its self, which is pretty stupid if you think about it. I've filed a complaint with the html5 standards people. its illogical to set the selected value of a select element in the individual options.

IF statement with four or more "OR" in the statement [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm just learn PHP code and I have a IF statement below which does not seem to work. I have other IF statement like below but with 3 or less OR statement and they seem to work just find.
if ($AT_Med_1_3 == '' or $AT_Med_1_3 == NULL or $AT_Med_1_3 == ' ' or $AT_Med_1_3 = 'None')
{`enter code here`
// Echo 'Doing $AT_ Input';
?>
<select name="AT_Med_1_3">
<option value="None">None</option>
<option value="Oral_Zofran">Oral Zofran</option>
<option value="IV_Zofran">IV_Zofran</option>
<option value="Oxygen">Oxygen</option>
<option value="Ibuprofen">Ibuprofen</option>
<option value="Aetaminothen">Aetaminothen</option>
<option value="Upinephrne">Upinephrne</option>
<option value="Xopenex">Xopenex</option>
<option value="Albuterol">Albuterol</option>
<option value="Valium">Valium</option>
<option value="Magnesiunsulphate">Magnesiunsulphate</option>
<option value="Diphenhyamine">Diphenhyamine</option>
<option value="Ketorolac">Ketorolac</option>
<option value="Promethazion">Promethazion</option>
<option value="Oral_Fluids_Soup">Oral Fluids Soup</option>
<option value="Oral_Fluids_Electrolyte">Oral Fluids Electrolyte</option>
<option value="Oral_Fluids_Water">Oral Fluids Water</option>
</select>
<?php
} else {
// echo 'Showing $AT_';
echo $AT_Med_1_3;
echo nl2br("\n");
}
?>
If I change the IF statement to several If else statement it work but is ugly code
<?php
// if ($AT_Med_1_1 == '' or $AT_Med_1_1 == NULL or $AT_Med_1_1 == ' ' or $AT_Med_1_1 = 'None')
if ($AT_Med_1_1 == '')
{
goto test1;
} else if ($AT_Med_1_1 == NULL)
{
goto test1;
} else if ($AT_Med_1_1 == ' ')
{
goto test1;
} else if ($AT_Med_1_1 == 'None')
{
goto test1;
}
goto test;
// Echo 'Doing Input';
test1:
?>
<select name="AT_Med_1_1">
<option value="None">None</option>
<option value="Oral_Zofran">Oral Zofran</option>
<option value="IV_Zofran">IV_Zofran</option>
<option value="Oxygen">Oxygen</option>
<option value="Ibuprofen">Ibuprofen</option>
<option value="Aetaminothen">Aetaminothen</option>
<option value="Upinephrne">Upinephrne</option>
<option value="Xopenex">Xopenex</option>
<option value="Albuterol">Albuterol</option>
<option value="Valium">Valium</option>
<option value="Magnesiunsulphate">Magnesiunsulphate</option>
<option value="Diphenhyamine">Diphenhyamine</option>
<option value="Ketorolac">Ketorolac</option>
<option value="Promethazion">Promethazion</option>
<option value="Oral_Fluids_Soup">Oral Fluids Soup</option>
<option value="Oral_Fluids_Electrolyte">Oral Fluids Electrolyte</option>
<option value="Oral_Fluids_Water">Oral Fluids Water</option>
</select>
<?php
goto endtest;
// } else {
// echo 'Showing $AT_';
test:
echo $AT_Med_1_1;
echo nl2br("\n");
endtest:
// }
?>
I'm open to so better suggestion on how to code this IF statement.
I think your main issue is the = instead of ==, however to simplify the if, trim() will remove any whitespace to '' and empty() checks for '' and null:
PHP < 5.5.0
$AT_Med_1_1 = trim($AT_Med_1_1);
if (empty($AT_Med_1_1) || $AT_Med_1_1 == 'None')
PHP >= 5.5.0
if (empty(trim($AT_Med_1_1)) || $AT_Med_1_1 == 'None')
Keep in mind that empty() will also see false and 0 values ("0" and 0.0) as empty.
Also || is the proper OR operator in your case and most cases.

JQuery/PHP: Replace a select box with another select box on a different page

On my main page, I have a simple dropdown box:
<select name="miles" id="miles">
<option value="5">5</option>
<option value="10">10</option>
<option value="15">15</option>
<option value="25">25</option>
<option value="50">50</option>
<option value="75">75</option>
<option value="100">100</option>
</select>
<button type="button" name="button" id="button">Search</button>
When this button is clicked, it performs a JQuery $.load:
$("#miles").load("main_data.php #miles_data");
And here the page button is calling, main_data.php:
<?php
... code to calculate $b
?>
<span id="miles_data">
<option value="5" <?php if ($b == 5) { echo "selected"; } ?>>5</option>
<option value="10" <?php if ($b == 10) { echo "selected"; } ?>>10</option>
<option value="15" <?php if ($b == 15) { echo "selected"; } ?>>15</option>
<option value="25" <?php if ($b == 25) { echo "selected"; } ?>>25</option>
<option value="50" <?php if ($b == 50) { echo "selected"; } ?>>50</option>
<option value="75" <?php if ($b == 75) { echo "selected"; } ?>>75</option>
<option value="100" <?php if ($b == 100) { echo "selected"; } ?>>100</option>
</span>
My goals seem simple: Execute a $.load to another page, calculate an integer and set it to a variable, select the matching dropdown item and return it to the main page.
With my code above, the dropdown box on the main page becomes blank. Does anyone notice a solution?
One possible issue is that the format for a selected 'option' is:
<select>
<option>Volvo</option>
<option selected="selected">Saab</option>
<option>Mercedes</option>
<option>Audi</option>
</select>
Blatantly ripped from here
So you want to have
<option value="5" <?php if ($b == 5) { echo 'selected="selected"'; } ?>>5</option>

Categories