How to compare value in bidimensional array Laravel - php

I receive the information with this form correctly.
$findNIF = User::where('nif','=',$nif)->get();
$findEmail = User::where('email','=',$email)->get();
I need to compare if it is the same value the "nif" and "email" of the $findNIF and $findEmail.
$findNIF["0"]["nif"];
$findNIF["0"]["email"];
$findEmail["0"]["nif"];
$findEmail["0"]["email"];
I use the following sentence but I always receive "OK"
if ($findNIF["0"]["nif"] && $findNIF["0"]["email"] && $findEmail["0"]["nif"] && $findEmail["0"]["email"]){
echo "OK";
} else {
echo "NO";
}

Here
if ($findNIF["0"]["nif"] && $findNIF["0"]["email"] && $findEmail["0"]["nif"] && $findEmail["0"]["email"]){
echo "OK";
} else {
echo "NO";
}
in your condition if you get values in both of your $findNIF and $findEmail variables then it always return true.
If you want to compare the nif of $findNIF & $findEmail and email of $findNIF & $findEmail both then this would be like this
if (($findNIF["0"]["nif"] == $findEmail["0"]["nif"]) && ($findNIF["0"]["email"] == $findEmail["0"]["email"])){
echo "OK";
} else {
echo "NO";
}
Please elaborate more about what you need.

Related

PHP unreachable if statement in foreach

I have this code I have set up that is supposed to read from JSON and output each array. Except when I use my foreach loop, I have an unreachable if statement. It's supposed to reach it if "type" is "rawbr".
I have confirmed that this has nothing to do with foreach by placing the same message in a row.
I wish to output this:
UnknownUser3: hey hxor? [To you]
Welcome to chat!
Here is my code:
innerchat.php:
<?php
session_start();
function tf($oz){
if($oz == 0){
return false;
} else if($oz == 1){
return true;
}
}
if(isset($_GET["room"]) && file_exists("data/".$_GET["room"].".json")){
$jsonF = file_get_contents("data/".$_GET["room"].".json");
$jsonD = json_decode($jsonF, true);
echo count($jsonD["msg"]);
// echo $jsonD["msg"][1]["type"];
foreach($jsonD["msg"] as $key => $message){
if($message["visibility"] !== "all"){
if(isset($_SESSION["ts_user"]) && $_SESSION["ts_user"] == $message["visibility"] && $message["type"] != "rawbr"){
echo "<font color='".$message["color"]."'><b><u>".$message["from"].":</u></b></font> ".htmlspecialchars($message["cont"])." [To you]<br />";
} else if($message["type"] === "message" && $message["visibility"] === "all"){
echo "<font color='".$message["color"]."'><b><u>".$message["from"].":</u></b></font> ".htmlspecialchars($message["cont"])." [normal message]<br />";
} else if($message["type"] === "rawbr" && $message["visibility"] === "all"){
echo $message["cont"]."<br />";
}
}
}
}
kb6k.json (the room we're working with)
{"name":"KillerBot 6000","desc":"A room with very harsh moderation. Proceed with caution!","max":600,"color":"#e0e0e0","whispersenabled":true,"forbiddenCommands":["/milk", "/bal"],"msg":[{"cont":"hey, hxor?","time":1,"color":"black","type":"message","visibility":"HxOr1337","from":"UnknownUser1"},{"cont":"Welcome to the chat!","time":0,"type":"message","color":"black","visibility":"HxOr1337","from":"Test"}]}
I know it couldn't possibly do anything to do with the JSON itself, since the other values are nearly identical apart from "visibility"
Ok, so I figured out that I put those if statements in $message["visibility"] !== "all"
The code:
<?php
session_start();
if(isset($_GET["room"]) && file_exists("data/".$_GET["room"].".json")){
$jsonF = file_get_contents("data/".$_GET["room"].".json");
$jsonD = json_decode($jsonF, true);
// echo $jsonD["msg"][1]["type"];
foreach($jsonD["msg"] as $key => $message){
if($message["visibility"] !== "all"){
if(isset($_SESSION["ts_user"]) && $_SESSION["ts_user"] == $message["visibility"] && $message["type"] != "rawbr"){
echo "<font color='".$message["color"]."'><b><u>".$message["from"].":</u></b></font> ".htmlspecialchars($message["cont"])." [To you]<br />";
}
} else {
if($message["type"] === "message" && $message["visibility"] === "all"){
echo "<font color='".$message["color"]."'><b><u>".$message["from"].":</u></b></font> ".htmlspecialchars($message["cont"])." [normal message]<br />";
} else if($message["type"] === "rawbr" && $message["visibility"] === "all"){
echo $message["cont"]."<br />";
}
}
}
}

php If statement inside html table wrong output

i need to use if statement inside my table
the value of the row is "Released" but i want to show Pending on my html table. how can i achieve it. this is my current code. what's wrong with my if statement?
if (strlen($row['signed']) == "Released") {
echo "Pending";
}
else{
echo $row['signed'];
}
strlen checks for string length. first check either signed is set in the array and then check if value is equal
if (isset($row['signed']) && $row['signed'] == "Released") {
echo "Pending";
}
else{
echo $row['signed'];
}
strlen() returns the length of the argument, so it returns an integer. You can check if value is equals to the string which you want something like this:
if ($row['signed'] == "Released") {
echo "Pending";
} else {
echo "Released";
}
strlen() is used to count the length of the string, to check if it matches "Released", just use == to compare:
if ($row['signed'] == "Released") {
echo "Pending";
} else {
echo $row['signed'];
}
To find out is $row['signed'] is set, just use isset():
if (isset($row['signed'])) {
echo "Pending";
} else {
echo $row['signed'];
}
More information on isset(): http://php.net/manual/en/function.isset.php
More information on PHP operators: http://php.net/manual/en/language.operators.php
Try this:
if ($row['signed'] == "Released") {
$value = "Pending";
} else {
$value = "Released"
}
And add <?php echo $value; ?> in your table

$_GET['variable'] == 'text'

I want to compare if a get variable is a specific text. But I cannot get into the if-statemeant. The echo "invalid user" is not written.
code:
<?php
if ($_GET['status'] && $_GET['status'] == "login_fail") {
echo "invalid user";
}
?>
when I check the variable status before (ando also after) the if-statemeant with:
echo $_GET['status'];
or with
print_r($_GET['status']);
the result is in both
login_fail
So the variable status is there and has "login_fail".
But "invalid user" is not written!
I tried it also with === or with ' instead " and much much more.
Use isset().
`
<?php
if (isset($_GET['status']) && $_GET['status'] == "login_fail") {
echo "invalid useenter code herer";
}
`
Try this :
<?php
if (!empty($_GET['status']) && $_GET['status'] == "login_fail") {
echo "invalid user";
}
?>
I think your code don't have any problem.
if your facing a problem till now you can add this line
before your if condition script.
$_GET['status'] = trim($_GET['status']);
In order to avoid errors when $_GET has nothing, use this:
if (!empty($_GET['status']) && ($_GET['status'] == "login_fail")) {
echo "invalid user";
}
Change to test if 'status' is set in the $_GET variable first and then check its value.
if (isset($_GET['status']) && $_GET['status'] == "login_fail")
{
echo "invalid user";
}
That way you will not run into any problems if it's not set.
You should use isset() function to check whether a variable has any value:
if(isset($_GET['status']) && $_GET['status'] == 'login_fail')
{
echo "invalid user";
}

PHP form check box issue

I have a simple form that I'm trying to add a checkbox to, I have everything on the form setup correctly but when I try to handle the check box I'm only able to make echos work. I'm trying set whether the box is checked as a yes or no and store that yes/no in a variable, here is what I have in my handle form for the checkbox:
if(isset($_POST['race']) &&
$_POST['race'] == 'Yes')
{
$race1 == "yes";
}
else
{
$race1 == "No";
}
You need to use the single equal sign when assigning values. Double equals does a comparison.
if(isset($_POST['race']) && $_POST['race'] == 'Yes')
{
$race1 = "yes";
}
else
{
$race1 = "No";
}
== is a comparison operator. You need to use attribution operator =
if (isset($_POST['race']) &&
strtolower($_POST['race']) == 'yes')
{
$race1 = 'yes';
}
else
{
$race1 = 'No';
}

PHP form keeps giving me back same response

very short simple quiz app. browser keeps returning Correct as a response, even though it is not correct.
<?php
$answer = (isset($_POST['answer']));`
if ($answer == "Dagny Taggart")`
{
echo "Correct";
} else {
echo "wrong";
}
?>
Try this. You're currently setting $answer to isset($_POST['answer'], meaning as long as $_POST['answer'] is set, you're $answer is going to be TRUE. I would check if it's set and then set it to the $_POST value.
<?php
if(isset($_POST['answer'])) {
$answer = $_POST['answer'];
if ($answer == "Dagny Taggart")
{
echo "Correct";
} else {
echo "wrong";
}
} else {
// Do something?
}
?>
isset returns a boolean (true or false) value. So your test will always fail because true != 'Dagny Taggart'
Add this ternary and it will work
$answer = (isset($_POST['answer'])) ? $_POST['answer'] : '';

Categories