I would like to get a person's weight, who is over 18 years old so very likely to be 100 pounds plus, so I want to check if the $_POST variable for the weight entered is 3 digits only. I have a form where the user enters their weight...
if (!is_numeric($weight)) {
echo "You did not enter a numeric weight.";
die;
}
Using the above to make sure it's numeric... but also want to verify the number is 3 digits.
Thank you
You can do:
is_numeric($weight) && ($weight > 99) && ($weight < 1000)
Related
I'm building an application to help customer calculate various product prices.
Right now I'm building a feature where user enters a single number to the application and submits a form. Based on that number, I would like to define another variables value.
What I'd like to achieve
If user input is number between 1-10, set variable number to 200.
If user input is number between 11-20, set variable number to 400.
If user input is number between 21-30, set variable number to 600.
If user input is number between 31-40, set variable number to 800.
If user input is number between 41-50, set variable number to 1000.
And so on... So basically increasing by 200 every tenth.
Of course, I could do something like this:
$userInput = 11;
$result;
if($userInput => 1 && $userInput =< 10)
$result = 200;
if($userInput => 11 && $userInput =< 20)
$result = 400;
if($userInput => 21 && $userInput =< 30)
$result = 600;
But it isn't really a great solution, because it takes lot of code and if user sets number out of the determined range it doesn't work..
How can I implement this with as little amount of code as possible?
If I have the math right, you just need to divide the number by 10, and use ceil to round the fraction up. From there, multiply it by 200;
function getVariable($num) {
$divisor = ceil($num / 10);
return $divisor * 200;
}
echo getVariable(1)."\n"; // 200
echo getVariable(6)."\n"; // 200
echo getVariable(13)."\n"; // 400
echo getVariable(27)."\n"; // 600
echo getVariable(48)."\n"; // 1000
echo getVariable(50)."\n"; // 1000
echo getVariable(88)."\n"; // 1800
echo getVariable(100)."\n"; // 2000
I what to validate a field so it will throw an error if its value empty or the length is less than or equal to 10.
But it only validates when its empty if the length is 1 or more it submits the value.
Need Help here to make it validate
if (empty($_POST["comment"]) && $_POST["comment"] <= 10) {
$comment_err = "Please send a message more than 10 characters";
}
else {
$comment = sanitize($_POST["comment"]);
}
Your question is not clear and precise, but I think what you're looking for if :
Throw an error if variable is empty
Throw an error if variable length is less than 10
So use OR ( || ) in your if statement, and strlen() to get variable length (as outlined in comment by Funk Forty Niner):
if(empty($_POST["comment"]) || strlen($_POST["comment"]) <= 10){
$comment_err = "Please send a message more than 10 characters";
}
else{
$comment = sanitize($_POST["comment"]);
}
Just check the length. If it's greater than 10 length, then it's definitely not empty, so you don't need to check for that explicitly.
if(isset($_POST["comment"]) && strlen($_POST["comment"]) > 10){
isset is just there to prevent an undefined index warning if that comment key doesn't exist.
(This reverses your if and else blocks, by the way, because it checks for good data instead of the error condition.)
This was partially answered in the comments, but Funk Forty Niner is such a generous soul that he gives away his wisdom for free with no expectation of fake internet points, all he asks for in return is some r e s p e c t when he comes home.
You have two problems in your code:
first your condition is not correct, you cannot have at the same time an empty index and checking his size. So you should have empty($_POST["comment"]) || $_POST["comment"] <= 10
you also cannot check the length of a string by checking the value against an integer, as #Funk Forty Niner told in comments, you have to use strlen() function http://php.net/manual/en/function.strlen.php
so your final code will be:
if(empty($_POST["comment"]) || strlen($_POST["comment"]) <= 10){
$comment_err = "Please send a message more than 10 characters";
}
else{
$comment = sanitize($_POST["comment"]);
}
I was searching for a regex which matches my requirement. But I couldn't find an exact one .
My requirement is
Add validation check to avoid Phone numbers with:
1) 6 digits equal (e.g. 000000 ; 111111)
2) sequence numbers (7 digits) (e.g.
1234567 ; 7654321)
I tried and got this piece of code finally
if (preg_match('/(\d)\1{5}/', $phone)) {
echo "Invalid Phone number";
}
But it matches only the first case. Hope some one will help me. Thanks in advance!
This is one of those times that I'd break away from regex.
This will perform your expected validation (and includes "around-the-clock" number sequences).
PHP Demo
$phone='000000';
$len=strlen($phone);
$rnd_the_clk='0123456789012345';
if(($len==6 && $phone==str_repeat($phone[0],6)) // length is 6, check only one integer used
||
($len==7 && (strpos($rnd_the_clk,$phone)!==false || strpos($rnd_the_clk,strrev($phone))!==false))){ // length is 7, check sequential
echo "invalid";
}else{
echo "valid";
}
I have this PHP code
What this code do is matching month count to numbers .
If condition is true echo result!
Result is displayed in table for each user.
$isT is count of months between 2 dates example 1,2,3 or 9
if($isT=='3'
OR $isT=='6'
OR $isT=='9'
OR $isT=='12'
OR $isT=='15'
OR $isT=='18'
OR $isT=='21'){
//echo something
}
What i want is make this numbers automatically generated
So it would be like:
if($isT==$generatednumber[$i]){
//echo something
}
i need numbers in this order 3 6 9 12 15. ..
basically +3 to the last number
First make sure its more than 0 then check if its multiple of 3.
if($isT >0 && ($isT % 3) == 0)){
//echo something
}
As i said modulus (%) is needed here:-
if(!empty($isT) && ($isT % 3) == 0)){
//echo something
}
Note:- this code will check:-
1.Your variable is set
2.Have some value
3.Is a multiple of 3.
I'm working on a shipping module for wine, and was wondering if anyone could give me a hand - basically:
The wine can be shipped in cases of 8, 12 or 15 bottles, each with its own price. The module needs to take the total number of bottles in the order, and work out which combination of cases gives the lowest price. Eg in an order of 31 bottles, the lowest price works out to 1 case of 15 and two cases of 8, (rather than 2 cases of 15 and 1 of 8, or 2 of 12 and one of 8). Currently, I have the following, which almost works, but misses a few possible combinations
foreach ($rates as $case_size => $case_price)
{
$price = floor($total_bottles / $case_size) * $case_price;
$rem = $total_bottles % $case_size;
if($rem > 12)
{
//needs to use another case of 15
$price = $price + $rates[15];
}
elseif($rem > 8)
{
//needs an extra case of 12
$price = $price + $rates[12];
}
elseif($rem > 0)
{
//needs an extra case of 8
$price = $price + $rates[8];
}
$quotes[] = $price;
}
return min($quotes);
From your post your saying that the most price-effective system wouldn't just the one that has uses the lowest cost per bottle of the container, but also needs to be the most efficient at filling the containers. However your algorithm is only looking at would use the fewest large boxes possible. You need an algorithm that will completely fill each case possible.
I would do something like this: Use a recursive program to find the combination that would most completely fill each case.
function fit_case($number, $case_size) {
$rem = $number % $case_size;
$next_size=magic_voodo0();
if($rem==0) { //if perfectly fills it you're done
return ($number/$case_size)*$rates[$case_size];
} else if(($rem % $next_size)/$next_size>.5) {
//if over 50% fills the next case add the next smaller case
return floor($number/$case_size)*$rates[$case_size]+fit_case($rem, $next_size);
} else { //otherwise back off 1 of the biggest cases, and fill the rest
return (floor($number/$case_size)-1)*$rates[$case_size]+fit_case($rem, $next_size);
Hope this helps.
Different approach. Use a lookup table that has all combinations of boxes for a specific number of bottles.
1 bottle - 8
...
31 bottle - 15-8-8,15-15-8,8-8-8-8, and so on
and so on
Use another lookup table for the different rates per box per country
In your function
get table row for country prices
get the different combinations for the number of bottles
do a foreach loop on combinations
save the price and combination of first loop to variables
compare the price of the next loop with the saved value
if it is lower, save price and combination/if not, continue
loop through all combinations
return lowest price/box combination