Gateway is down because of PHP code in one situation - php

Here is the code (HTML / PHP) :
<div class="col-sm-6">
<div class="form-group">
<select id="horseSize" name="horseSize" class="form-control" >
<option value="<?php echo htmlspecialchars($horseSize); ?>" selected><?php echo htmlspecialchars($horseSize); ?> m</option>
<?php
$i = 0.50;
while($i <= 2.00){
if ($i != $horseSize){
?>
<option value="<?php echo htmlspecialchars(number_format($i, 2)) ; ?>"><?php echo htmlspecialchars(number_format($i, 2)) ; ?> m</option>
<?php
$i = $i + 0.01;
}
}
?>
</select>
</div>
</div>
$horseSize comes from a SQL query and is working fine. Th problem is in displaying webpage.
When i am trying to display this webpage, my website goes down because of gateway giving no answer. This triggers only in one situation : if the variable value of $horseSize is inferior to 1. As if it was not possible to display something like 0,....
Maybe I did something wrong in this code, but I tried many times to fix, I couldn't reach it.

you are incrementing $i only under if condition, so value like 0.50 it will always never increase. Also floating point comparison will not work. please read: Floating point numbers
<div class="col-sm-6">
<div class="form-group">
<select id="horseSize" name="horseSize" class="form-control" >
<option value="<?php echo htmlspecialchars($horseSize); ?>" selected><?php echo htmlspecialchars($horseSize); ?> m</option>
<?php
$i = 0.50;
$precision = 0.00001;
while($i <= 2.00){
if(abs($i-$horseSize) > $precision) {
?>
<option value="<?php echo htmlspecialchars(number_format($i, 2)) ; ?>"><?php echo htmlspecialchars(number_format($i, 2)) ; ?> m</option>
<?php
$i = $i + 0.01;
} else {
$i = $i + 0.01;
}
}
?>
</select>
</div>
</div>

Related

PHP Assign Value from dropdown selection

I have this loop coded to create a dropdown with the numbers 0-30.
<td style="text-align:left;">
<select id="iPhone12Case" name="iPhone12Case">
<?php
for ($i=0; $i<=30; $i++) {
?>
<option value="<?php echo $i;?>"><?php echo $i;?></option>
<?php
}
?>
</select>
</td>
I cannot seem to figure out how to get the value of I to work in my calculations on an order processing form.
I assign the variable using
$iPhone12 = htmlspecialchars($_POST['iPhone12Case']);
but if I try to output $iPhone12 * 15 for example the answer is always zero.
Make sure you select is wrapped in a form element with method="POST". You should also use isset to make sure it exists.
<form action="" method="post">
<td style="text-align:left;"><select id="iPhone12Case" name="iPhone12Case">
<?php
for ($i = 0; $i <= 30; $i++) {
?>
<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php
}
?>
</select></td>
<button>submit</button>
</form>
<?php
if (isset($_POST['iPhone12Case'])) {
$iPhone12 = htmlspecialchars($_POST['iPhone12Case']);
printf($iPhone12 * 15);
}

Set default value for select dropdown in Php

I'm a absolute newby for php and i'm struggling with just a basic thing.
I want to set a default value for a select box i have. Default value should be 2
My code
<div class="selector-wrapper">
<select id="adults" class="form-control" onchange="toggleSetGuests()">
<?php
for($i = 0; $i <= 20; $i++):
?>
<option value="<?php echo $i ?>" <?php if($i == 0){ echo('selected="true"'); } ?>>
<?php echo $i ?> Adults
</option>
<?php
endfor;
?>
</select>
</div>
So in this select box i get options from 0 - 20. I want to set the default value as 2 instead of 0
My approach was like below
<option value="<?php echo $i ?>" <?php if($i == 2){ echo('selected="true"'); } ?>>
But did not work.
How do i set the default value to be selected as the value 2
<option value="<?php echo $i ?>" <?php if($i == 2){ echo ' selected'; } ?>>
should work.You don't need selected=true

How to change this 24 hours time format in 12 Hours

Hi i am facing a problem while editing the plugin. the default time format of this plugin is 24 hours but i want to convert it into 12 hours with AM/PM as well.
Here is the code that i have
<div class="col-xs-4 col-sm-4 col-md-4">
<div class="form-group qc-input-container">
<select name="quickcab_form_departure_time_hour" id="quickcab_form_departure_time_hour" class="booking-input quickcab-select-input form-control" required>
<option disabled selected><?php echo esc_html__('Hour', 'quickcab'); ?></option>
<?php
for ( $i = 1; $i <= 12; $i++ ) {
?>
<option value="<?php echo sprintf('%02d', $i); ?>"><?php
echo sprintf('%02d', $i);
?></option>
<?php
}
?>
</select>
</div>
You can easily get the 12 hours time by using date() function. I'm giving an example for you.
// suppose your time is 19:24:15
$date = '19:24:15';
echo date('h:i:s a', strtotime($date));
The output will be
07:24:15 pm
You can get the output also by using DateTime
$date = new DateTime('19:24:15');
echo $date->format('h:i:s a') ;
h is used for 12 digit time
i stands for minutes
s seconds
a will return am or pm (use in uppercase for AM PM)
To keep it really simple you could just add another loop to 12 to get a full 24 hours, then a simple change to the sprinf() will get you the AM and PM, like this
sprintf('%02dAM', $i);
and in the second loop
sprintf('%02dPM', $i)
So your code
<div class="col-xs-4 col-sm-4 col-md-4">
<div class="form-group qc-input-container">
<select name="quickcab_form_departure_time_hour" id="quickcab_form_departure_time_hour" class="booking-input quickcab-select-input form-control" required>
<option disabled selected><?php echo esc_html__('Hour', 'quickcab'); ?></option>
<?php
// first 12 hours
for ( $i = 1; $i <= 12; $i++ ) {
?>
<option value="<?php echo sprintf('%02dAM', $i); ?>"><?php echo sprintf('%02dAM', $i);?></option>
<?php
}
// second 12 hours
for ( $i = 1; $i <= 12; $i++ ) {
?>
<option value="<?php echo sprintf('%02dPM', $i); ?>"><?php echo sprintf('%02dPM', $i);?></option>
<?php
}
?>
</select>
</div>

set variable 2 digits in a for loop

I'm having an stupid issue here; I'm trying to make in a form, a select with the time the user starts, and the time they finish, so logically, I want to display the time as:
00
01
02.. I mean, 2 digit
But although I put 00 for the initial value of $i, it displays only 1 digit anyway!
Is it any way I can set it to two digits?
Thanks!!
<div class="interval">
<input type="checkbox" id="interval_check"/>
<div id="desde">
De: <select class="hores" name="hores" disabled>
<?php for ($i = 00; $i <= 23; $i++) : ?>
<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php endfor; ?>
</select>:<select class="minuts" name="minuts" disabled>
<?php for ($i = 00; $i <= 45; $i+=15) : ?>
<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php endfor; ?>
</select>
</div> <div id="fins">
fins: <select class="hores" name="hores" disabled>
<?php for ($i = 00; $i <= 23; $i++) : ?>
<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php endfor; ?>
</select>:<select class="minuts" name="minuts" disabled>
<?php for ($i = 00; $i <= 45; $i+=15) : ?>
<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php endfor; ?>
</select>
</div>
</div>
just use sprintf to create the string for you.
<?php for ($i = 0; $i <= 23; $i++) : ?>
<option value="<?php echo $i; ?>">
<?php echo sprintf("%02d", $i); ?>
</option>
<?php endfor; ?>
this will add a 0 if the string is less than 2 characters. I'm presuming that you don't care about the value.

Prevent new line from being formed within a div

I have the following HTML/PHP sign up form:
<div class="form_block" id="dob-check" style="display:none; float:left">
<p><label for="sign_up_dob_day">DOB</label> <select name="sign_up_dob_day" id="sign_up_dob_day" class="required"><option value="">-</option><?php for($day = 1; $day <= 31; $day++) : ?>
<option value="<?php echo $day ?>" <?php echo set_select('sign_up_dob_day', $day, ((is_array($dob) && count($dob) == 3 && $dob[2]==$day)? true : false)); ?>><?php echo $day ?></option>
<?php endfor; ?>
</select>
<select name="sign_up_dob_month" class="required">
<option value="">-</option>
<?php $months = getMonthsArray(); ?>
<?php for($month = 1; $month <= 12; $month++) : ?>
<option value="<?php echo $month ?>" <?php echo set_select('sign_up_dob_month', $month, ((is_array($dob) && count($dob) == 3 && $dob[1]==$month)? true : false)); ?>><?php echo $months[$month - 1] ?></option>
<?php endfor; ?>
</select>
<select name="sign_up_dob_year" class="required">
<option value="">-</option>
<?php for($year = 2005; $year >= 1930; $year--) : ?>
<option value="<?php echo $year ?>" <?php echo set_select('sign_up_dob_year', $year, ((is_array($dob) && count($dob) == 3 && $dob[0]==$year || (empty($dob[0]) && $year==1990)))? true : false); ?>><?php echo $year ?></option>
<?php endfor; ?>
</select>
<?php echo form_error('sign_up_dob_day'); ?>
<a class="hand" id="dob-why" onclick="showMessage()"> Why do we need this?</a></p>
</div>
Which displays as follows:
However, if a user fails to enter a DOB, the formatting of the select boxes gets completely messed up, as below:
I guess its because the width of the div is not enough when adding the 'field required' text.
I have tried putting an outer div but this did not work. I have also tried floating left, but this also did not work.
I am new to PHP so cannot see how to remove the second 'field required' message-this may possibly help?
Any other ideas eg prevent a new line being formed within the div?
Try using display: inline-block on inputs and error messages instead of display: block.
Should fix the problem.

Categories