echo $_REQUEST['status'];
if ((isset($_REQUEST['status'])) && ($_REQUEST['status'] == "signin" )){
echo "do this";
} elseif ((isset($_REQUEST['status'])) && ($_REQUEST['status']) == "forget")){
echo "do that";
}
The code not working... Kindly let me know where I am doing wrong.
Thanks in Advance
Related
Revised Code of full deal.. instead of just the small snippet.. Would $row be the start of the variable?
<?php
error_reporting(-1);
$sql = "SELECT * FROM products Where available like 'Y%' order by manufacturer2, product ASC";
$query = mysql_query($sql);
while($row = mysql_fetch_array($query)) {
<--------------------->>
$new_product = "$row"; if ($new_product == "N")
{ echo "No"; } elseif ($new_product == "Y")
{ echo "Yes"; }
<-------->
echo "<div class=reportsalesnew_product>".$row['new_product']."</div>";
echo "<div class=reportsalescase_avail>".$row['case_avail']."</div>";
echo "<div class=reportsalesseasonal>".$row['seasonal']."</div>";
echo "<div class=reportsaleseigth_bbl>".$row['eigth_bbl']."</div>";
echo "<div class=reportsalesquarter_bbl>".$row['quarter_bbl']."</div>";
echo "<div class=reportsaleshalf_bbl>".$row['half_bbl']."</div>";
echo "<div class=reportsalessixth_bbl>".$row['sixth_bbl']."</div>";
echo "<div class=reportsalesthirty_liter>".$row['thirty_liter']."</div>";
echo "<div class=reportsalesfifty_liter>".$row['fifty_liter']."</div>";
echo "</div>";
}
?>
Assuming this is PHP, use the == for equality checks. If you also want to do type checks use the === (recommended).
Also, you might want to look at the boolean data type for this, this is a simple true/false.
Everyone accidentally does it when they begin programming. You've wrote one ='s sign to check for equality but all that is doing is assign it therefore it will always be true(unless it's a constant). Use two ='s for equality, three '='s for type, one '=' for an assignment.
I was trying to make a program to match the string which must contains number of 0-9 using regex.
This was correct but it somehow seems long. Do anyone has alternatives for this code?
if($str = (preg_match('/[1]/', $str) && preg_match('/[2]/', $str)
&& preg_match('/[3]/', $str) && preg_match('/[4]/', $str)
&& preg_match('/[5]/', $str) && preg_match('/[6]/', $str)
&& preg_match('/[7]/', $str) && preg_match('/[8]/', $str)
&& preg_match('/[9]/', $str) && preg_match('/[0]/', $str))) {
//do something
}
Simply use a character range: [0-9].
if (preg_match('/[0-9]/', $str)) {
echo 'It does.';
} else {
echo 'It doesn\'t.';
}
If you were ever in a situation where you wouldn't want "6" you could even change it to [012345789] if you really want to.
As Floris mentions, your code is pretty confusing - if you want all the characters to be displayed individually at least once, you can simply use strpos with a loop:
<?php
$match = true;
for ($i = 0; $i < 9; $i++) {
if (strpos($string, (string)$i) === false) {
$match = false;
break; //No need to continue the loop - we already got our answer
}
}
if ($match) {
echo 'Yes!';
} else {
echo 'No!';
}
?>
Alternatively, I apparently already gave you a function to do this?
Looks like you have all the conditions ANDed together. In that following lookahead based regex should work for you:
preg_match('/(?=[^0]*0)(?=[^1]*1)(?=[^2]*2)(?=[^3]*3)(?=[^4]*4)(?=[^5]*5)(?=[^6]*6)(?=[^7]*7)(?=[^8]*8)(?=[^9]*9)./', $str)
If you want to make sure that your string contains all the digits 0-9, you should probably strip anything that is not a digit, then take unique characters only, and make sure the string length is 10. This is more compact than your expression but not necessarily faster. The php function count_chars does much of this work (using mode = 3):
$str = "12345abcde789d9999969";
preg_match_all('/\d+/', $str, $matches);
$distinct = strlen(count_chars(join($matches[0]),3));
if($distinct==10)
{
echo "all ten digits are present<br>";
}
else
{
echo "not all digits are present<br>";
}
echo "there are " . $distinct . " distinct digits<br>";
Output of the above:
not all digits are present
there are 9 distinct digits
I'm trying to match part of a string to a yes or no answer. The string goes either: '{"valid_js":"yes"...' or '{"valid_js":"no"...'. I'm trying to get the "yes" or "no"
Can I just use something like:
/:."yes"/g
Or do I need something more complicated?
Try something like this...
if (m/"valid_js":"(yes|no)"/)
{
# At this point $1 will contain either yes or no
if ($1 eq 'yes')
{
# Answer is yes
}
else
{
# Answer is no
}
}
This is a generic form of regexp:
valid_js["]:["](yes|no)["] -> $1
You can use that regexp to match or replace.
This should help you:
$input = '{"valid_js":"yes"...';
if (($input =~ m/"valid_js":"(.*?)"/) && ($1 eq 'yes')) {
print "1: yes\n";
}
$input = '{"valid_js":"no"...';
if (($input =~ m/"valid_js":"(.*?)"/) && ($1 eq 'yes')) {
print "2: yes\n";
}
You can test the code here.
I have a validation check when I pass the command line parameters to the perl program.I pass two years first passed argument should be lesser than the second passed argument and both the arguments should be only digits and also they should be exactly 4.
#Sunny>perl check.pl 2007 2008 This is good
#Sunny>perl check.pl 2008 2007 This is bad
#Sunny>perl check.pl 200 2007 This is bad
I have written code for this but not able to figure it out why its not working.
#!usr/bin/perl
#check.pl
if ($#ARGV < 0) { }
else
{
$fiscyear1 = $ARGV[0];
$fiscyear2 = $ARGV[1];
}
if (($fiscyear1 !~ m/\d{4}/) and ($fiscyear2 !~ m/\d{4}/) and ($fiscyear1 < $fiscyear2))
{ print "Bad parameters\n"; }
else
{ print "good parameters\n"; }
This sounds like a bad case of overthinking things...
What you want is to verify that the arguments are two years, in the form YYYY, and that the second year comes after the first.
Well, 9999 would pass any digit check, but it is hardly a valid year. Same with 0101, or 3021, etc.
What you want is to set a given range, and make sure the arguments are numeric.
use strict;
use warnings;
my $yr1 = shift;
my $yr2 = shift || usage();
usage() unless $yr1 =~ /^\d+$/;
usage() unless $yr2 =~ /^\d+$/;
my $min_year = 1900;
my $max_year = 2200;
if ( ($yr1 < $min_year) or ($yr1 > $max_year) ) {
die "Invalid year: $yr1";
}
if ( ($yr2 < $min_year) or ($yr2 > $max_year) ) {
die "Invalid year: $yr2";
}
if ( $yr1 >= $yr2 ) {
die "Invalid sequence: $yr2 is not greater than $yr1";
}
sub usage {
die "Usage script.pl <year1> <year2>";
}
What about this:
if (($fiscyear1 !~ m/^\d{4}$/) or ($fiscyear2 !~ m/^\d{4}$/) or ($fiscyear1 > $fiscyear2))
{ print "Bad parameters\n"; }
I changed the ands in ors and also the final < into > (since you want the first argument is less than the second)
EDIT:
It seems that it works in my case:
I also very strongly accept the advice of using ^$, and have modified my answer accordingly.
You are missing start and end of string ^ and $ in regexps (without them it can match 5 or more symbols):
use strict; use warnings;
my ($fiscyear1, $fiscyear2) = #ARGV;
if ($fiscyear1 =~ m{^\d{4}$} && $fiscyear2 =~ m{^\d{4}$} && $fiscyear1 < $fiscyear2) {
print "good parameters\n";
} else {
print "Bad parameters\n";
}
Update
You can also use unless here like:
unless ($fiscyear1 =~ m{^\d{4}$} && $fiscyear2 =~ m{^\d{4}$} && $fiscyear1 < $fiscyear2) {
print "Bad parameters\n";
exit(1); ## tells the caller side that there is an error
}
print "good parameters\n";
$w = 'self-powering';
%h = (self => 'self',
power => 'pauә',
);
if ($w =~ /(\w+)-(\w+)ing$/ && $1~~%h && $2~~%h && $h{$2}=~/ә$/) {
$p = $h{$1}.$h{$2}.'riŋ';
print "$w:"," [","$p","] ";
}
I expect the output to be
self-powering: selfpauәriŋ
But what I get is:
self-powering: [riŋ]
My guess is something's wrong with the code
$h{$2}=~/ә$/
It seems that when I use
$h{$2}!~/ә$/
Perl will do what I mean but why I can't get "self-powering: selfpauәriŋ"?
What am I doing wrong? Any ideas?
Thanks as always for any comments/suggestions/pointers :)
When you run
$h{$2}!~/ә$/
In your if statement the contents of $1 and $2 are changed to be empty, because no groupings were matched (there were none). If you do it like this:
if ($w =~ /(\w+)-(\w+)ing$/){
my $m1 = $1;
my $m2 = $2;
if($m2~~%h && $m2~~%h && $h{$m2}=~/ә$/) {
$p = $h{$m1}.$h{$m2}.'riŋ';
print "$w:"," [","$p","] ";
}
}
I expect you will get what you want.
Are you running with use warnings enabled? That would tell you that $1 and $2 are not what you expect. Your second regex, not the first, determines the values of those variables once you enter the if block. To illustrate with a simpler example:
print $1, "\n"
if 'foo' =~ /(\w+)/
and 'bar' =~ /(\w+)/;