Explode with operator - php

I have a working code to restrict & validate subdomain.
$exp = explode('.', 'blog.mydomain.my.');
print_r($exp);
if(count($exp) == 3 && $exp[1] == "mydomain" && $exp[2] == "my" || $exp[3] == "") {
echo "<br>";
echo 'subdomain valid';
} else{
echo "<br>";
echo 'not valid';
}
now it need to check if its only false and I'm not so sure about the $exp[3] != "" comparison. From example below the subdomain should be valid but it give me error.
echo "<br>";echo "<br>";
$exp2 = explode('.', 'blog.mydomain.my.');
print_r($exp2);
if(count($exp2) != 3 || $exp2[1] != "mydomain" || $exp2[2] != "my" || $exp[3] != "") {
echo "<br>";
echo 'not valid';
}
Accepted numbers of subdomain is hello.mydomain.my or hello.mydomain.my. (with trailing dot). While hello.world.mydomain.my is not accepted.
Thanks in advance

This should give you what you want.
if(count($exp2) < 3 || count($exp2) > 4 || $exp2[1] != "mydomain" || $exp2[2] != "my" || (count($exp2) != 4 && $exp[3] != "")) {
echo "<br>";
echo 'not valid';
}

I would go for a regex solution, possibly even encapsulate it in a function:
function isValidDomain($domain)
{
return preg_match('/^[\w]+\.(mydomain)\.(my)\.?$/', $domain) ? true : false;
}
var_dump(isValidDomain('www.google.com'));
var_dump(isValidDomain('test.invalid.domain'));
var_dump(isValidDomain('hello.mydomain.my'));
var_dump(isValidDomain('hello.mydomain.my.'));

Related

How to Exit If Statement and Run Another IF Statement?

$var1="exit";
$var2="run";
$var3="go";
if($var1 != '' && $var2 != '' && $var3 != '' ){
//first condtion
if($var1 == 'clear'){
echo 'clear';
}
else {
exit();
}
//2nd condtion
if($var2 == 'run'){
echo 'run';
}
//3rd condtion
if($var3 == 'go'){
echo 'go';
}
}
**I try to Exit() First If Condition, continue to next IF Condition, But Cannot Continue next IF condition totally Exit Overall Please Fix My Problem Thankyou **
No need to use else condition if you want to run second if statement
if($var1 != '' && $var2 != '' && $var3 != '' ){
//first condtion
if($var1 == 'clear'){
echo 'clear';
}
//2nd condtion
if($var2 == 'run'){
echo 'run';
}
//3rd condtion
if($var3 == 'go'){
echo 'go';
}
}
What About the idea of this one.
<?php
$var1="exit";
$var2="run";
$var3="go";
if($var1 != '' && $var2 != '' && $var3 != '' )
{
if($var2 == 'run'){
echo 'run';
}
elseif($var3 == 'go'){
echo 'go';
}
elseif($var1 == 'clear'){
echo 'clear';
}
else {
exit();
}
}
just use only if.. use return
$var1="exit";
$var2="run";
$var3="go";
if($var1 != '' && $var2 != '' && $var3 != '' ){
//first condtion
if($var1 == 'clear'){
echo 'clear';
}else {
return;
}
//2nd condtion
if($var2 == 'run'){
echo 'run';
}else {
return;
}
//3rd condtion
if($var3 == 'go'){
echo 'go';
}else {
return;
}
}

Counting Empty Columns in Rows

I have a property table and it has six columns. Users upload photo and image name is stored in the column.
Now I want to count the number of columns for each row which are empty.
I am already able to do that but the code looks too long, I want to write efficient code, is there a way to rewrite the following efficiently.
while($data=$select->fetch()){
$imagecounter=0;
if ($data['property_image1'] !== "" && $data['property_image2'] !== "" && $data['property_image3'] !== "" && $data['property_image4'] !== "" && $data['property_image5'] !== "" && $data['property_image6'] !== "") {
echo $imagecounter=6;
} else if ($data['property_image1'] !== "" && $data['property_image2'] !== "" && $data['property_image3'] !== "" && $data['property_image4'] !== "" && $data['property_image5'] !== "") {
echo $imagecounter=5;
} else if ($data['property_image1'] !== "" && $data['property_image2'] !== "" && $data['property_image3'] !== "" && $data['property_image4'] !== "") {
echo $imagecounter=4;
} else if ($data['property_image1'] !== "" && $data['property_image2'] !== "" && $data['property_image3'] !== "") {
echo $imagecounter=3;
} else if ($data['property_image1'] !== "" && $data['property_image2'] !== "") {
echo $imagecounter=2;
} else if ($data['property_image1'] !== "") {
echo $imagecounter=1;
}
}
You can do it like below:-
while($data=$select->fetch()){
$data1 = array($data['property_image1'],$data['property_image2'],$data['property_image3'],$data['property_image4'],$data['property_image5'],$data['property_image6']);
$count = count($data1); // count of original array
$count1 = count(array_filter($data1)); // remove empty indexes and count the values
echo "empty columns number is :-".($count-$count1);
}
Note:- $count1 is count of non-empty values
try this code
while($data=$select->fetch()):
$imagecounter = 0;
for($i=1; $i<=6; $i++)
if(!empty($data["property_image$i"]))
$imagecounter++;
echo $imagecounter;
endwhile;
does the column name has its rule?
assuming it's 'property_image{number}'
while($row=$result->fetch()) {
$count = 0;
for($i=0; $i<6; $i++) {
if($row['property_image'.$i]==NULL)
$count++;
}
echo "empty columns number is :-".($count);
}
code is not tested.
let me know if it doesnt work

Code executed despite false if statement

I have the following code:
if ($Type != "DEA" and $VA != "Allowed" and $VolSess != 1) {
$max_rows = max($CMSReg_num_rows);
if ($max_rows == 0) {
mail($to, $subject, $body);
header('Location: '.bloginfo('home_url').'/profile');
}
}
The problem I have is that that an email is sent despite the if-statement being false, and only an email is sent. The rest of the code is not executed, i.e. no redirect. And when I comment out the mail() function, it does not send the email.
And when I add this code:
if ($VA == "Allowed") {
echo "VA = " . $VA;
}
if ($VolSess == 1) {
echo "VolSess = " . $VolSess;
}
I get this output:
VA = Allowed VolSess = 1
So I know that the condition in the if statement is false.
AND has a different order of precedence compared to &&. So your expression does not evaluate as you expect it to.
("$Type" != "DEA" and $VA != "Allowed" and $VolSess != 1)
should be
(("$Type" != "DEA") and ($VA != "Allowed") and ($VolSess != 1))
or
("$Type" != "DEA" && $VA != "Allowed" && $VolSess != 1)
for it to work as you expect it. This is one of those tiny mistakes/bugs that's easy to overlook.
try do an else after...
elseif($VA == "Allowed"){}
Try using the WordPress wp_mail().
die; after header() and also add 302 as a second argument to the header() function.
Enable error reporting with ini_set('display_errors', true); error_reporting(-1); on top of your PHP code.
Tell us what you see after making these changes.
Try:
if ($Type != 'DEA' && $VA != 'Allowed' && $VolSess != 1)
{
$max_rows = max($CMSReg_num_rows);
if ($max_rows === 0)
{
mail($to, $subject, $body);
header('Location: ' . bloginfo('home_url') . '/profile');
}
}
EDIT
The above works, but so does the oringal question code... The problem is elsewhere.
<?php
$Type = 'foo';
$VA = 'Allowed';
$VolSess = 1;
if ($Type != 'DEA' and $VA != 'Allowed' and $VolSess != 1)
{
$max_rows = 0;
if ($max_rows === 0)
{
echo 'Orig True';
}
}
else
{
echo 'fine?';
}
if ($Type != 'DEA' && $VA != 'Allowed' && $VolSess != 1)
{
$max_rows = 0;
if ($max_rows === 0)
{
echo 'Second True';
}
}
else
{
echo 'fine?';
}
?>
Both print 'fine?' Implying your error is elsewhere in your code.

My function doesn't work on || tags

I've written a function to make menu roll down if the page is the same as I declare.
The function looks like this
function menu_current()
{
$current = basename($_SERVER['REQUEST_URI']);
if ($current === "index?p=config" || "index?p=maintenance")
echo "class=\"nav-top-item suballowed current\" ";
else
echo "class=\"nav-top-item suballowed\" ";
}
It works perfectly if I only declare 1 page
if ($current === "index?p=config")
but not more. How to solve that solution? And is there a way to declare all websites between || tags in one variable instead of writing them like I did?
Replace this
if ($current === "index?p=config" || "index?p=maintenance")
with
if ($current === "index?p=config" || $current === "index?p=maintenance")
otherwise PHP doesn't know what should be equal to index?p=maintenance
You can use your approach if you set both sides of the equality check every time:
if ($current === "index?p=config" || $current === "index?p=maintenance") { ...
Perhaps a more "readable" solution:
if (in_array($current, array( 'index?p=config', 'index?p=maintenance' )) { ...
Another option would be to use a switch statement with a default.
function menu_current()
{
$current = basename($_SERVER['REQUEST_URI']);
switch($current) {
case "index?p=config":
case "index?p=maintenance":
echo "class=\"nav-top-item suballowed current\" ";
break;
default:
echo "class=\"nav-top-item suballowed\" ";
}
}
Here is the correct code
function menu_current()
{
$current = basename($_SERVER['REQUEST_URI']);
if ($current == "index?p=config" || $current == "index?p=maintenance")
echo "class=\"nav-top-item suballowed current\" ";
else
echo "class=\"nav-top-item suballowed\" ";
}

PHP quiz script answer highlight

I have a PHP script and MSSQL tables, I got the answer key in a variable stored in $right_answer and user selected answers in $user_answer_select THe format is something like this
5+10?
A) 10
B) 15
C) 20
D) 25
E) 50
Answer key: B
what I want to do is put a check mark next to B if its correct and a X if it is wrong, how would I make the if else statements here?
This is the code I currently have
if(($user_answer_select == $right_answer) && $user_answer_select == 'a') $a_sel = "<img src=\"tick_icon.gif\">";
else if(($user_answer_select == $right_answer) && $user_answer_select == 'b') $b_sel = "<img src=\"tick_icon.gif\">";
else if(($user_answer_select == $right_answer) && $user_answer_select == 'c') $c_sel = "<img src=\"tick_icon.gif\">";
else if(($user_answer_select == $right_answer) && $user_answer_select == 'd') $d_sel = "<img src=\"tick_icon.gif\">";
else if(($user_answer_select == $right_answer) && $user_answer_select == 'e') $e_sel = "<img src=\"tick_icon.gif\">";
This is wrong because some of the questions that don't have answers for are highlighted as true. What's the way to do this?
$answers = array ( "A"=>10, "B"=>15, "C"=>20, "D"=>25, "E"=>50 );
$right_answer = "B";
$user_selected_answer = "A";
echo "5+10?<br/>";
foreach ($answers as $key => $value) {
echo $key.") ".$value;
if ($value === $user_selected_answer) {
if ($value === $right_answer){ echo "check!"; }
else { echo "X"; }
}
echo "<br/>";
}
echo "Answer key: $right_answer";
if ( $user_answer_select == $right_answer ) {
$correct = true;
} else {
$correct = false;
}
Then in the correct answer on the form:
<?php echo $correct == true ? 'x' : ''; ?>

Categories