I obtain data from the model in an array format, I would like to know why I can't print_r the result without using exit() function right after it ?
this works:
function someview()
{
$result=$this->User->getdatafrommodel();
print_r($result);
exit();
if(!empty($result))
{
//do something
}
else
{
$this->redirect(array('action'=>'usernotexist'));
}
}
function usernotexist()
{
$this->loadSkin();
}
this prints an empty Array.
function someview()
{
$result=$this->User->getdatafrommodel();
print_r($result);
if(!empty($result))
{
//do something
}
else
{
$this->redirect(array('action'=>'usernotexist'));
}
}
function usernotexist()
{
$this->loadSkin();
}
Could someone tell me why so it is ?
In the second block of code, as results is empty this line is called in the else block:
$this->redirect(array('action'=>'usernotexist'));
So your output would appear to be a different page without the exit() statement.
Related
hey guys i have function in php and i am trying to find a way to remove all function contents if the result is empty or false
here is an example:
function myfunction($result){
?>
<div><a>Hello</a></div>
<?php
if(empty($result)) {
return false;
}
}
my problem is when the return is false, the function output is
Hello
and i want to remove it if return is false.
You have to return false before outputting anything
function myfunction($result){
if(empty($result)) {
return false;
}
?>
<div><a>Hello</a></div>
<?php
}
You can return empty/false response when it is empty and return the HTML when it is not, like below:
<?php
function myfunction($result){
return empty($result) ? false : "<div><a>Hello</a></div>";
}
<?php
function myfunction($result){
if(empty($result)) {
return '<div></div>';
}else{
return "<div><a>Hello</a></div>";
}
}
echo myfunction($result);
I am getting null value for function getRecommendation($matrix,$getName), the data obtained are working fine
still learining php
<?php
require('connection.inc.php');
require('recommend.php');
$userID=$_SESSION['USER_ID'];
$reco=mysqli_query($con,"select userrating.*,product.id,product.productName from userrating,product where userrating.userID='$userID' and product.id=userrating.productID");
$matrix=array();
while($rec=mysqli_fetch_array($reco))
{
$users=mysqli_query($con,"select users.name from users where users.id=$rec[userID]");
$username=mysqli_fetch_array($users);
$matrix[$username['name']][$rec['productName']]=$rec['rating'];
$getName= $username['name'];
}
var_dump(getRecommendation($matrix,$getName));
?>
Here is the function code as well, it is for a recommendor system , fur now i jst need this code not to return null value
$value)
{
if(array_key_exists($key,$matrix[$product2]))
{
$similar[$key]=1;
}
}
if($similar==0)
{
return 0;
}
foreach($matrix[$product1] as $key=>$value)
{
if(array_key_exists($key,$matrix[$product2]))
{
$sum=$sum+pow($value - $matrix[$product2][$key],2);
}
}
return 1/(1+sqrt($sum));
}
function getRecommendation($matrix,$prod)
{
foreach($matrix as $otherProduct=>$value)
{
if($otherProduct!=$prod){
$sim=similarityDistance($matrix,$prod,$otherProduct);
var_dump($sim);
}
}
}
?>
You have to actually return something from the function getRecommendation. (That's why you get NULL now. The functions isn't returning anything).
function getRecommendation($matrix,$prod)
{
foreach($matrix as $otherProduct=>$value)
{
if($otherProduct!=$prod){
$sim=similarityDistance($matrix,$prod,$otherProduct);
var_dump($sim);
}
}
//I don't what you want to return. Maybe this, but it has to be something..
return $sim;
}
I am trying to return a db value using the following function in case of failure the function is supposed to return an error object. I have separated the code block with If/else statement. The if condition satisfies and the if block is executed. But for some reason the return statement in the if block do not execute and the code execution continues to next statement. I tried the two options on both cases the results are same.
First Code
public function is_defined_headtype($head_id)
{
if ($is_defined = $this->ci->account_head_model->is_defined_headtype($head_id))
{
echo "Im here<br>";
return $this->_success($is_defined, ACC_SUCCESS);
}
echo "Im here also";
// General Error Occurred
return $this->_general_error();
}
Second Code
public function is_defined_headtype($head_id)
{
if ($is_defined = $this->ci->account_head_model->is_defined_headtype($head_id))
{
echo "Im here<br>";
return $this->_success($is_defined, ACC_SUCCESS);
}
else
{
echo "Im here also";
// General Error Occurred
return $this->_general_error();
}
}
In both cases I am getting the following output.
Im here
Im here also{"err":1,"code":1,"msg":"Error"}
Calling function definition
public function add_eam($user_id, $eamname, $entityid, $headid, $eamdescription, $eamisfraction, $eampercentage, $eamamount, $affectedheadid)
{
if (/* $eam_id = $this->ci->account_eam_model->insert_eam($user_id, $eamname, $entityid, $headid) */ true)
{
$is_defined = $this->is_defined_headtype(/* $headid */1);
echo json_encode($is_defined);
if($is_defined == 1)
{
echo "Im here";
exit();
if ($entity_id = $this->ci->account_eam_definition_model->insert_eam_definition(/* $user_id */ 1, $eam_id, $eamdescription, $eamisfraction, $eampercentage, $eamamount, $affectedheadid))
{
return $this->_success($eam_id, ACC_ENTITY_ADDED);
}
else
{
return $this->_general_error();
}
}
echo "Im not here";
exit();
return $this->_success($eam_id, ACC_ENTITY_ADDED);
}
// General Error Occurred
return $this->_general_error();
}
hi i think the error is here
you are doing an if statement with only one '=' this is to set a value but you want to comapre so that should be:
'==' to compare that two values are the same
'!=' to see if the two values are different
public function is_defined_headtype($head_id)
{
if ($is_defined = $this->ci->account_head_model->is_defined_headtype($head_id))
{
echo "Im here<br>";
return $this->_success($is_defined, ACC_SUCCESS);
}
echo "Im here also";
// General Error Occurred
return $this->_general_error();
}
In If statement try Type Juggling (int) or use === for strong compare with both side data.
So, I'm working on a dynamic forum signatures script and I alredy got it working. Now I will to make it so that only a specific user group can a specific design.
This is the function I made.
function userGroup($rank)
{
if ($rank == 38)
{
return true;
}
else
{
return false;
}
}
And used it like this.
if ($userGroup == true)
{
...
}
else echo('User with that ID doesn\'t exist in the database');
}
else echo('This user hasn\'t in the specific user group');
But it wont work like that.
Regards,
Lazar!
You made a function function userGroup($rank) and you are trying to call it as if its a variable $userGroup. What happened to the parameter? Your if-statement should be something like:
if (userGroup($var) == true) { ... }
You don't actually need the == true either:
if (userGroup($var)) { ... }
you can not use a function like this. you should have a function call with valid argument like this:
if (userGroup($intUserRank))
{
...
}
else echo('User with that ID doesn\'t exist in the database');
assume $intUserRank containing user rank.
You function could just be like this:
function userGroup($rank)
{
return ($rank == 38);
}
Then you would use it like this:
if (userGroup($some_rank))
{
...
}
Please check this below code. After for loop remaining code is not executing. It suppose to print "Helo", but it is not printing any thing.
for($i=0;$i<10;$i++)
{
$minrate=$obj_iScripts->min_avg_rate($roomnumber[$id_array[$i]], $amount_ary[$id_array[$i]], $totalrooms);
$all_min_price[]=$minrate;
if($_SESSION['star'][$id_array[$i]]>=1 && $_SESSION['star'][$id_array[$i]]<=5)
{
//include 'searchresult_table.php';
}
}
echo "Helo";
code:
public function min_avg_rate($roomnumber,$rates,$totalrooms)
{
$ary_name='iArray';
$total=0;
for($i=1;$i<=$totalrooms;$i++)
{
${$ary_name.$i}=array();
$temp=max($rates);
for($j=0;$j<count($roomnumber);$j++)
{
if($roomnumber[$j]==$i)
{
if($temp>$rates[$j])
$temp=$rates[$j];
${$ary_name.$i}=$temp;
}
}
$total=$total+${$ary_name.$i};
}
return $total/$totalrooms;
}
From what code you have posted -the min_avg_rate() function within your class would seem to be malfunctioning