How to get the result of elseif in php to jquery - php

PHP
$name = karlaxis;
if ($name == "") {
return false;
} elseif ($name != karlaxis) {
return false;
} else {
return true;
}
jQuery
$.post("#name-form").attr("action").serializearray(), function(result){
if (result == false) {
console.log("result of the IF CONDITION");
} elseif (result == false) { //the result of ELSEIF in php
console.log("result of the ELSEIF CONDITION");
} else {
console.log("result of the ELSE CONDITION");
}
})
Now, how to get the result of the elseif?
How to code that one?

If it is very important to get the result of different conditions, you can do a simple trick with your code. Rather than returning bool you may return int values like 0,1,2 where 0 is for IF, 1 is for ELSE IF, 2 is for ELSE.
php:
$name = karlaxis;
if($name == ""){
return 0;
}elseif($name != karlaxis){
return 1;
}else{
return 2;
}
jquery:
$.post("#name-form").attr("action").serializearray(), function(result){
IF(result == 0){
console.log("result of the IF CONDITION");
}ELSE IF(result == 1){ //the result of ELSEIF in php
console.log("result of the ELSEIF CONDITION");
}ELSE{
console.log("result of the ELSE CONDITION");
}
})

Related

Trying to access array offset on value of type int { DefaultValueBinder.php line 82 }

I have an export to excel button which downloads excel file.
However when i click it shows me error as Trying to access array offset on value of type int
My code is this:
public static function dataTypeForValue($pValue = null)
{
// Match the value against a few data types
if ($pValue === null) {
return PHPExcel_Cell_DataType::TYPE_NULL;
} elseif ($pValue === '') {
return PHPExcel_Cell_DataType::TYPE_STRING;
} elseif ($pValue instanceof PHPExcel_RichText) {
return PHPExcel_Cell_DataType::TYPE_INLINE;
} elseif ($pValue[0] === '=' && strlen($pValue) > 1) { //error comes in this line
return PHPExcel_Cell_DataType::TYPE_FORMULA;
} elseif (is_bool($pValue)) {
return PHPExcel_Cell_DataType::TYPE_BOOL;
} elseif (is_float($pValue) || is_int($pValue)) {
return PHPExcel_Cell_DataType::TYPE_NUMERIC;
} elseif (preg_match('/^[\+\-]?([0-9]+\\.?[0-9]*|[0-9]*\\.?[0-9]+)([Ee][\-\+]?[0-2]?\d{1,3})?$/', $pValue)) {
$tValue = ltrim($pValue, '+-');
if (is_string($pValue) && $tValue[0] === '0' && strlen($tValue) > 1 && $tValue[1] !== '.') {
return PHPExcel_Cell_DataType::TYPE_STRING;
} elseif ((strpos($pValue, '.') === false) && ($pValue > PHP_INT_MAX)) {
return PHPExcel_Cell_DataType::TYPE_STRING;
}
return PHPExcel_Cell_DataType::TYPE_NUMERIC;
} elseif (is_string($pValue) && array_key_exists($pValue, PHPExcel_Cell_DataType::getErrorCodes())) {
return PHPExcel_Cell_DataType::TYPE_ERROR;
}
return PHPExcel_Cell_DataType::TYPE_STRING;
}
}
what can be the solution for this..?
In the PHPExcel file "DefaultValueBinder.php", replace this line 82:
} elseif ($pValue[0] === '=' && strlen($pValue) > 1) {
with the following:
} elseif (0 === strpos($pValue, '=') && strlen($pValue) > 1) {
(!is_int($pValue) && $pValue[0] === '=' && strlen($pValue) > 1) should work. $pValue[0] will not work with integers so check if it is an int or not before continuing.
I update some files in library and It's work fine for me at php 7.4
Please check updated code library :
Google Drive LInk

PHP strpos dont works properly(Path Generator)

I wanted to create a function that generates an image path from an array.The function has one argument and that argument is for you to decide from which folder you want to choose images.
So I have 90 elements in an array and each element is a path.Now I don't want a .png image so I wanted to create something like this(but it didn't work for me):
public function getRandomPath($RoboticOrArenag)
{
if ($RoboticOrArenag == 'Robotic' || $RoboticOrArenag == 'robotic') {
$countofarray = count($this->allImagesRobotic);
} else if ($RoboticOrArenag == 'Arenag' || $RoboticOrArenag == 'arenag') {
$countofarray = count($this->allImagesArenag);
} else {
$RoboticOrArenag = 0;
}
$randomNumber = rand(0, $countofarray);
if ($RoboticOrArenag == 'Robotic' || $RoboticOrArenag == 'robotic') {
if($randomNumber==50){
$randomNumber--;
}
else if ($randomNumber==0){
$randomNumber++;
}
$randomNumberPath = $this->allImagesRobotic[$randomNumber];
} else if ($RoboticOrArenag == 'Arenag' || $RoboticOrArenag == 'arenag') {
if($randomNumber==33){
$randomNumber--;
}
else if ($randomNumber==0){
$randomNumber++;
}
$randomNumberPath = $this->allImagesArenag[$randomNumber];
while(strpos($randomNumberPath, '.png') == true){
$randomNumber = rand(0, $countofarray);
$randomNumberPath = $this->allImagesArenag[$randomNumber];
}
} else {
$randomNumberPath = 0;
}
$randomPath = "/img/$RoboticOrArenag/$randomNumberPath";
return $randomPath;
}

multiple IF statements in a function - OOP

im wanting to check whether a inputted triangle is either a isosceles, equal etc.. the first if statement works, but once i introduce the second one it says unexpected t_else, here is the code...
public function typeOfTriangle()
{
if ($this->sideOneLength == $this->sideTwoLength && $this->sideTwoLength == $this->baseLength) {
echo "<h1>Equilateral Triangle</h1>";
else if ($this->sideOneLength == $this->sideTwoLength OR $this->sideOneLength == $this->baseLength OR $this->sideTwoLength == $this->baseLength) {
echo "<h1>Isosceles Triangle</h1>";
}
}
any ideas?
Maybe this is even better? ( Based on the example from Vlad Preda:
public function getSides()
{
return array($this->sideOneLength, $this->sideTwoLength, $this->sideThreeLength);
}
public function typeOfTriangle()
{
$uniqueSides = count(array_unique($this->getSides()));
switch ($uniqueSides) {
case 1:
return "Equilateral";
break;
case 2:
return "Isosceles";
break;
default:
return "Normal triangle";
break;
}
}
You have a syntax error:
public function typeOfTriangle()
{
if ($this->sideOneLength == $this->sideTwoLength && $this->sideTwoLength == $this->baseLength) {
echo "<h1>Equilateral Triangle</h1>";
}else if ($this->sideOneLength == $this->sideTwoLength OR $this->sideOneLength == $this->baseLength OR $this->sideTwoLength == $this->baseLength) {
echo "<h1>Isosceles Triangle</h1>";
}
}
Need a brace }
if ($this->sideOneLength == $this->sideTwoLength && $this->sideTwoLength == $this->baseLength) {
echo "<h1>Equilateral Triangle</h1>";
}
else if ($this->sideOneLength == $this->sideTwoLength OR $this->sideOneLength == $this->baseLength OR $this->sideTwoLength == $this->baseLength) {
echo "<h1>Isosceles Triangle</h1>";
}
I would suggest a slightly different, more readable approach:
public function getSides()
{
return array($this->sideOneLength, $this->sideTwoLength, $this->sideThreeLength);
}
public function typeOfTriangle()
{
$uniqueSides = count(array_unique($this->getSides()));
if ($uniqueSides == 1) {
return "Equilateral";
} elseif ($uniqueSides == 2) {
return "Isosceles";
} else {
return "Normal triangle";
}
}

If no results exist in mysql table echo something else?

can someone please help me, i am trying to show different images based on the records i have for each user in my table 'permissions'. i am using .
if the records dont exist in the table i want to include a different file path i.e.
At the moment it brings up results if the users permission is set to 1 or 0 but i want to make it so that if no results are found in the table it shows something as a default.
code:
<?php if (logged_in()) {
$account_perms = account_perms();
while ($perms = mysql_fetch_array($account_perms)) {
if ($perms['privellages'] == '1') {
if ( mysql_num_rows( $perms ) > 0 )
include('includes/mod_profile/mod_photos/private.php');
}
}
$account_perms = account_perms();
while ($perms = mysql_fetch_array($account_perms)) {
if ($perms['privellages'] == '0') {
include('includes/mod_profile/mod_photos/private2.php');
}
}
$account_perms = account_perms();
while ($perms = mysql_fetch_array($account_perms)) {
if($perms->num_rows > 0) {
include('includes/mod_profile/mod_photos/private2.php');
}
}
} ?>
Try this:
<?
if (logged_in()) {
$account_perms = account_perms();
if (mysql_num_rows($account_perms)) {
while ($perms = mysql_fetch_array($account_perms)) {
if ($perms['privellages'] == '1') {
if (mysql_num_rows($perms) > 0)
include ('includes/mod_profile/mod_photos/private.php');
}
}
} else {
echo 'No records';
}
$account_perms = account_perms();
if (mysql_num_rows($account_perms)) {
while ($perms = mysql_fetch_array($account_perms)) {
if ($perms['privellages'] == '0') {
include ('includes/mod_profile/mod_photos/private2.php');
}
}
} else {
echo 'No records';
}
$account_perms = account_perms();
if (mysql_num_rows($account_perms)) {
while ($perms = mysql_fetch_array($account_perms)) {
if ($perms -> num_rows > 0) {
include ('includes/mod_profile/mod_photos/private2.php');
}
}
} else {
echo 'No records';
}
}
?>

Else statement never runs

I'm having some trouble where my last else statement never runs even though I know the first if will eventually return false, any ideas?
if ($productType == 'HSweat' || 'T Shirt') {
if ($productType == 'HSweat') {
if (!$queryResult) {
}
} elseif ($productType == 'T Shirt') {
if (!$queryResult) {
}
}
} else {
}
if ($productType == 'HSweat' || 'T Shirt') {
Should be
if ($productType == 'HSweat' || $productType == 'T Shirt') {
'T Shirt' as a string will always evaluate to True
I would recommend to use switch :
switch( $productType ){
case 'HSweat':
if (!$queryResult) {
//Do something
}
break;
case 'T Shirt':
if (!$queryResult) {
//Do something
}
break;
default:
//Do something
break;
}

Categories