if statement not returning proper result - php

I have created a function that checks if the image is empty or image variable has no value or the image is not found then it returns default image, but on some products it gives results but not on all of them..
function image_check($image)
{
$no_image = "noimagefound.jpg";
if(isset($image) || !empty($image) || $image != " ")
{
if(file_exists('uploads/store/products/'.$image))
{
return 'uploads/store/products/'.$image;
}
else
{
return 'uploads/web_service/'.$no_image;
}
}
else
{
return 'uploads/web_service/'.$no_image;
}
}
Can anyone make it work properly? What am I missing?

function image_check($image)
{
$no_image = "noimagefound.jpg";
if( !empty($image) && file_exists('uploads/store/products/'.$image) )
{
return 'uploads/store/products/'.$image;
}
return 'uploads/web_service/'.$no_image;
}
As they pointed out in the comments, your condition failed because you were checking if it was empty, not if it wasn't empty. isset() and !empty() are redundant in this case.
You also don't need all of those else checks. Be careful complicating your code more than you need to. You only need one check, if that fails, then return your $no_image.

Related

isset() returning nothing for NULL

I have written this php code
//check if user has active plan and search keyword is not empty
if (!empty($request['advertisername']) && ($userSubscriptionType == env('AMEMBER_STD_PLAN_CODE') || $userSubscriptionType == env('AMEMBER_PRE_PLAN_CODE'))) {
$advertisername = 'LOWER(post_owner) like "%' . strtolower($request["advertisername"]) . '%"';
} else {
//if search keyword is null, means page is loaded for first time
if (!isset($request['advertisername'])) {
$advertisername = "true";
} else {//if search keyword is not null, and user has not active plan
$response->code = 400;
$response->msg = "Permission issues";
$response->data = "";
return json_encode($response);
}
}
Here $request['advertisername']=null coming from fronend. So this condition will be true if (!isset($request['advertisername'])) {. But unfortunately it's always going to last else block.
If i save $request['advertisername'] in a variable like this. Then this condition if (!isset($advertiserName)) { becomes true.
//check if user has active plan and search keyword is not empty
if (!empty($request['advertisername']) && ($userSubscriptionType == env('AMEMBER_STD_PLAN_CODE') || $userSubscriptionType == env('AMEMBER_PRE_PLAN_CODE'))) {
$advertisername = 'LOWER(post_owner) like "%' . strtolower($request["advertisername"]) . '%"';
} else {
//if search keyword is null, means page is loaded for first time
$advertiserName=$request['advertisername'];
if (!isset($advertiserName)) {
$advertisername = "true";
} else {//if search keyword is not null, and user has not active plan
$response->code = 400;
$response->msg = "Permission issues";
$response->data = "";
return json_encode($response);
}
}
I checked the condition via var_dump(). Anyone can explain why it is happening?
you can use is_null() instead of !isset().
You can use array_key_exists() instead:
if (!array_key_exists('advertisername', $request)) {
Please note the PHP manual for isset() says:
isset — Determine if a variable is set and is not NULL
A good way would be to check if the variable is not empty as it checks both isset and null
eg if(!empty($your_variable) { /** your code here **/ }

SugarCRM v6.5 anything after $bean->save stops working

I have this code:
function saveField($field, $id, $module, $value)
{
$bean = BeanFactory::getBean($module, $id);
if (is_object($bean) && $bean->id != "") {
if ($bean->field_defs[$field]['type'] == "multienum") {
$bean->$field = encodeMultienumValue($value);
}else if ($bean->field_defs[$field]['type'] == "relate" || $bean->field_defs[$field]['type'] == 'parent'){
$save_field = $bean->field_defs[$field]['id_name'];
$bean->$save_field = $value;
if ($bean->field_defs[$field]['type'] == 'parent') {
$bean->parent_type = $_REQUEST['parent_type'];
$bean->fill_in_additional_parent_fields(); // get up to date parent info as need it to display name
}
}else{
$bean->$field = $value;
}
//return here will work
$bean->save(); //this works
//nothing works here
return getDisplayValue($bean, $field);
} else {
return false;
}
}
The problem here is that anything under
$bean->save()
will not work. But I know that save is working as the values are being updated. So how can I debug this problem?
I already tried:
return var_dump($bean->save());
return print_r($bean->save());
if($bean->save()){
return "1";
}else{
return "2";
}
And none of those in the above worked I still get nothing in my return.
There is likely something such as an after_save logic hook that is executing and either causing a fatal error or doing an exit.
Try using xdebug, it should allow you to investigate further into the save method that fails.

PHP isset() method not returning true

At the model of MVC pattern of codeigniter, I make some codes that is same at below.
function getBoardNum($option){
foreach($option as $key=>$content){
$this->db->where($key,$content);
//echo "$key $content";
}
if(isset($this->db->get('parsing_tb')->row()->num)){
$num = $this->db->get('parsing_tb')->row()->num;
return $num;
} else {
return false;
}
}
The problem is the isset at the if condition seems to not work.
To find out what is wrong, I used var_dump and such like confirming the $option data. But everything is okay...
Please let me know what is wrong! Thank you for reading.
Idk why isset() is not working, but you can try this instead :
//...
$query = $this->db->get('parsing_tb');
if ($query->num_rows() > 0)
{
$num = $this->db->get('parsing_tb')->row()->num;
return $num;
}
else
return false;
You should use isset to check if some variable is initialized - that means you check if it has any value.
even this case will yield TRUE:
$var = "";
if( isset( $var )){
echo TRUE;
}
In your code it looks like you are first trying to initialize it and assign some value to it and you use isset. You should use a different method for whatever you are trying to accomplish.

How to check if php mysqli_fetch_array is empty before while loop

I wanted to make sure there are results before running the while loop but all the methods I am trying seem to remove the first result.
$nearbyResult = mysqli_query($con,$sqlNearby);
if(mysqli_fetch_array($nearbyResult) == 0) {
echo '<p>No results found, Add your property here.</p>';
} else {
while($rowNearby = mysqli_fetch_array($nearbyResult)) {
}
}
This line will take the first row of your result set and chuck it in the bin:
if(mysqli_fetch_array($nearbyResult) == 0) {
Change to:
if( ! mysqli_num_rows($nearbyResult) ) {
And check your freakin function returns:
if( ! $nearbyResult = mysqli_query($con,$sqlNearby) ) {
echo "Mysql error: " . mysqli_error($con);
}
You can use the mysql_row_count method to count how many rows are returned in your query http://php.net/manual/en/mysqli-result.num-rows.php
Try assigning the results to a variable as part of your if statement. If mysqli_fetch_array() has no result set to work with it will return false.
if($rowNearby = mysqli_fetch_array($nearbyResult)) {
//There was a result, work with it here
doStuffWith($rowNearby);
} else {
//No records in your result set, handle as desired
}

php get variables

I'm new to php and need some help with "GET" variables.
Here an extraction of my Code for index.php:
$array = array("section","view","sub","cat","point");
$i = 0;
$check = true;
foreach ($_GET as $position => $wert) {
if ($position != $array[$i]) {
//if GET doesnt exist in the array set check to false
$check = false;
break;
}
$i++;
}
//if GET variables exists
if ($check == true) {
if (isset($_GET['section'], $_GET['view'], $_GET['sub'], $_GET['cat'], $_GET['point'], $point[$_GET['point']])) {
$path = $path_dynamic.$_GET['section']."/".$_GET['view']."/".$_GET['sub']."/".$_GET['cat']."/".$point[$_GET['point']];
check($path);
} else if (isset($_GET['section'], $_GET['view'], $_GET['sub'], $_GET['cat'], $cat[$_GET['cat']])) {
$path = $path_dynamic.$_GET['section']."/".$_GET['view']."/".$_GET['sub']."/".$cat[$_GET['cat']];
check($path);
} else if (isset($_GET['section'], $_GET['view'], $_GET['sub'], $sub[$_GET['sub']])) {
$path = $path_dynamic.$_GET['section']."/".$_GET['view']."/".$sub[$_GET['sub']];
check($path);
} else if (isset($_GET['section'], $_GET['view'], $view[$_GET['view']])) {
$path = $path_dynamic.$_GET['section']."/".$view[$_GET['view']];
check($path);
} else if (isset($_GET['section'], $section[$_GET['section']])) {
$path = $path_dynamic.$section[$_GET['section']];
check($path);
//if section isn't set
} else if (!isset($_GET['section'])) {
include ($path_dynamic.$section['news']);
}
} else {
echo "GET doesn't exist";
include ($path_static.$section['error']);
}
//check if GET exists
function check($path) {
if (file_exists($path)) {
echo "File imported<br />";
include ($path);
} else {
echo "GET set correct but file doens't exist";
include ('include/static/fehler.html');
}
}
Example of section.php (view, sub, cat and point is the same):
$section = array();
$section['error'] = 'fehler.html';
My problem is that if i set this link:
index.php?section=verein&view=vorstande
"vorstande" doesn't exist in my view array. So the code checks for the section "verein" and include "verein". But it should give an error.
So it seems that this code
} else if (isset($_GET['section'], $_GET['view'], $view[$_GET['view']])) {
is ignored and it jumps to
} else if (isset($_GET['section'], $section[$_GET['section']])) {
Also if I change my url to this:
index.php?section=vereine&view=vorstande
nothing happens. I even don't know where the code is right now.
But if I change the url to this:
index.php?section=vereine&view=vorstand
everything works fine.
So "verein" and "vorstand" is defined by me. "vereine" and "vorstande" doens't exist.
Any suggestions? Sry for comments in german. The echo only gives me a hint where the code is right now!
Link to my HP:
Edit:
- translated comments for better conversation
- deleted all "$...[$_GET['...']]" structures to show the error I will get instead.
"vorstande" doesn't exist in my view array. So the code checks for the
section "verein" and include "verein". But it should give an error.
By "give an error" you mean $check = false;?
} else if (isset($_GET['section'], $_GET['view'], $view[$_GET['view']])) {
If in your $view the key vorstande does not exist, this the whole condition will evaluate to false and next condition will be checked:
} else if (isset($_GET['section'], $section[$_GET['section']])) {
Edit:
Your code:
else if (isset($_GET['section'], $_GET['view'], $view[$_GET['view']])) {
$path = $path_dynamic.$_GET['section']."/".$view[$_GET['view']];
check($path);
}
Your requirement:
if (!isset($view[$_GET['view']]))
check(/* something invalid to display fehler.html */ false);
What actually happens:
isset($_GET['section']) // true
isset($_GET['view']) // true
isset($view[$_GET['view']]) // FALSE
=> isset($_GET['section'], $_GET['view'], $view[$_GET['view']]) // FALSE
If the $_GET['view'] does not exist in $view, the block which would call check is not executed. If you want it to be executed regardless, simply remove the condition isset($view[$_GET['view']]):
else if (isset($_GET['section'], $_GET['view'])) {
$path = $path_dynamic.$_GET['section']."/".#$view[$_GET['view']]; // # to suppress errors from accessing
check($path);
}
If you don't like this approach, work on your cases. You have one case for section isset AND view isset AND view is valid. The next case ignores the view parameter. So if your view parameter is not valid, your code handles it like it was not set. The requirement though is to have a case for section isset AND view isset AND view is invalid:
else if (isset($_GET['section'], $_GET['view']) && !isset($view[$_GET['view']])) {
check(false);
}
This is of course pretty much redundant checking so just nest it to something like:
else if (isset($_GET['section'], $_GET['view']))
{
// section and view have been passed
if (isset($view[$_GET['view']])
// view is actually valid
$path = $path_dynamic.$_GET['section']."/".$view[$_GET['view']];
else
// view has been passed but is invalid. show fehler.html
$path = false;
check($path);
}
Alternate Example
If I understand you correctly, you have the following requirement: If the user passes a parameter section, view, sub, cat or point, you want that this value is also in your list of valid values. If it isn't, you want to display fehler.html.
We now first ensure that if the parameter is set, it is also valid:
foreach ($array as $param)
{
// example for $param == "view":
// !isset( $view[$_GET["view"]] )
if (!isset( ${$param}[$_GET[$param]] ))
{
$check = false;
break;
}
}
We then check all your combinations of parameters and build a $path
$path = false;
if ($check)
{
// your long if isset else if isset block where
// isset($_GET['view']) also implies isset($view[$_GET['view']])
// so you don't have to check for it.
// just set the $path variable with some string.
// we are going to check it later
}
If now the initial $check failed or we built an invalid $path, display fehler.html
if ($check === false || !file_exists($path))
{
// display fehler.html
}
else
include($path);
The problem is here:
} else if (isset($_GET['section'], $_GET['view'], $view[$_GET['view']])) {
Each parameter of isset must be true for the statement to return true. In your example, $_GET['section'] and $_GET['view'] are set, but $view[$_GET['view']] is not, so execution continues on the next else if line.
To fix the problem, either set $view[$_GET['view']] previously, or remove that parameter.

Categories