IF else & else if in Codeigniter - php

please check my script.
if($namachief != NULL)
{
echo $namachief;
}
else if ($namarm != NULL)
{
echo $namarm;
}
else
{
echo "Something wrong. Please contact US";
}
My condition isn't working when in this condition => $namarm != NULL, I only get white page but its normal when in this condition => $namachief != NULL.
It's fine when i do this
echo $namachief; & echo $namarm;
if () {
}
else if (){
}
else if(){
------------------------ My Script Here -----------------------------
}
Both answer below are right. My problem lies here. In my form I did this
<option value="none">None</option> Then i change it to <option value="">None</option>

You try like this
if($namachief != NULL || $namachief != "")
{
echo $namachief;
}
else if ($namarm != NULL || $namarm != "")
{
echo $namarm;
}
else
{
echo "Something wrong. Please contact US";
}

Write your condition as below:-
if(isset($namachief) && !empty($namachief)){
echo $namachief;
}
else if (isset($namarm) && !empty($namarm)){
echo $namarm;
}
else{
echo "Something wrong. Please contact US";
}
If you want to print both variables if both are set and have valid values then,
if(empty($namarm) && empty($namachief)){
echo "Something wrong. Please contact US";
}
else if (!empty($namachief) && !empty($namarm)){
echo $namarm;
echo $namachief;
}
else if (isset($namarm) && !empty($namarm)){
echo $namarm;
}
else if(isset($namachief) && !empty($namachief)){
echo $namachief;
}
else{
// else stuff
}

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 />";
}
}
}
}

How to compare value in bidimensional array Laravel

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.

Allow access only member's group and administrator

I'm trying to allow access for certain content only for specific member's user and administration.
With the following code I can't view the content when I'm logged as admin:
<?php
if (is_category(4)) {
if (!current_user_can('my-group') || !current_user_can('add_users')) {
echo '<h1 class="entry-title">You can not access</h1><p>This content is only for Administrator and group\'s members.</p>';
}
} else {
?>
Also I tryed each condition individually, but as admin I can't view the content.
I tryed this example code:
<?php if (is_category(8)) {
if (!current_user_can('mio-gruppo') || !user_can('administrator')) {
echo 'you can not pass';
}
} else {
echo 'you pass';
}
?>
But the two conditional statement goes in conflict. If I try both separately works right
I guess it should be && instead of ||:
<?php
if (is_category(4)) {
if (!current_user_can('my-group') && !current_user_can('add_users')) {
echo '<h1 class="entry-title">You can not access</h1><p>This content is only for Administrator and group\'s members.</p>';
}
} else {
?>
Alternatively make it the other way round:
<?php
if (is_category(4)) {
if (current_user_can('my-group') || current_user_can('add_users')) {
echo 'access granted';
} else {
echo 'no access';
}
}
?>
This is what I need:
<?php
if (is_category(8) && (current_user_can(array(1,2,3,4,5,6,7)) || !is_user_logged_in())) {
if (!current_user_can('mio-gruppo')) {
echo 'you can not pass';
}
} else {
echo ' you can pass';
}
?>

PHP if statement errmsg

I have this if statment
if(!empty($URL) && ($safe===true)){
//lots of code
}
Is it possible to show different error messages depending on what condition failed?
For example if $URL is empty echo "URL empty";
and if $safe===false echo "GTFO";
Just add this to your code
else if(empty($URL)
{
echo "url empty";
}
else if($safe===false)
echo "Get Out"; // be polite ;)
if (empty($url))
{
echo "URL empty";
}
elseif ($safe === false)
{
echo "GTFO";
}
else
{
//lots of code
}
} else {
if($safe === false){
die("GTFO");
}
if (empty($url)){
echo "URL Empty.";
}
}
Yes; you could make use of an else if statement.
if (!empty($URL) && ($safe===true)) {
//lots of code
} else if (empty($URL)) {
// report that url is empty
} else if ($safe === false) {
// report that safe is false
}
Alternatively, you could just use an else statement to report that the if condition was false.
I propose the following solution. It will allow you to show multiple errors and set each condition only once (instead of having so many conditions and anti-conditions as other solutions proposed).
$errors = array();
if(empty($URL) {
$errors[] = 'URL empty';
}
if($safe !== true) {
$errors[] = 'GTFO';
}
if(empty($errors)) {
//lots of code
} else {
echo '<ul>';
foreach($errors as $error_message) {
echo '<li>' . $error_message . '</li>';
}
echo '</ul>';
}

PHP File Input Validation

How can I create a function in PHP that checks if a file input is selected? The following is what I have but its not working.
Thank you!
function mcFileUp($mcFile){
if(empty($_FILES[$mcFile]['name'])){
echo 'empty';
}
else{
echo 'Ok!';
}
}
I figured it out:
function mcFileUp($mcFile){
if(empty($mcFile))
{
echo 'empty';
}
else
{
echo 'Ok!';
}
}
mcFileUp($_FILES['mcFileUpload']['name']);
Here is a possible solution
function mcFileUp($mcFile)
{
if(strlen($_FILES[$mcFile]['name'])==0)
{
echo 'empty';
}
else
{
echo 'Ok!';
}
}
if(isset($_FILES["uploaded_file"]))
{
if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0))
{
............
}
else
{
echo "Error: File ".$_FILES["uploaded_file"]["name"]." already exists";
}
}
else
{
echo "Error: No file uploaded";
}
Check THIS page if you want to know props and corns about the superglobal array "$_FILE". Also, you can have a look # THIS tutorial. Hope it helps you...

Categories