I am trying this code:
My Code snippet
public function beforeFind($query) {
$query = parent::beforeFind($query);
if(!isset($query['conditions'])){
$query['conditions'] = array();
}
if(!$this->Authake->isAdmin()){
if(empty($query['conditions']) || is_array($query['conditions'])){
$query['conditions'] = array('organ_id' => $this->Organ->Group->getUserBranches());
}
}
return $query;
}
But I am getting this error
Error
Warning (2): Cannot use a scalar value as an array [APP\Model\Ticket.php, line 56]
Warning (2): Cannot use a scalar value as an array [APP\Model\Ticket.php, line 56]
As said by commentor that $query is not an array, and beforeFind is returning boolean. So the following line is having problem.
$query = parent::beforeFind($query);
The correction is
There is no use of calling parent method as it is overridden it to implement own logic.
so the correct code is
public function beforeFind($query) {
if(!isset($query['conditions'])){
$query['conditions'] = array();
}
if(!$this->Authake->isAdmin()){
if(empty($query['conditions']) || is_array($query['conditions'])){
$query['conditions'] = array('organ_id' => $this->Organ->Group->getUserBranches());
}
}
return $query;
}
Related
This is my array
$sub = array("English"=>"12","Hindi"=>"12","History"=>"12","Geography"=>"12","Mathematics"=>"12","Physics"=>"12","Chemistry"=>"12","Biology"=>"12");
Want to pass this entire array as the parameter of a function & want to sum up the marks(array values) using the function
function sum_marks($sub){--Function body--
}
I don't know if this is the proper syntax for passing an array to a function, help!!
Is this you are looking for?
$mySum = array_sum($sub);
Yes, it is the appropriate syntax for passing an array as an argument to a function.
However, you might consider adding a type declaration for the $sub argument:
function sum_marks(array $sub)
{
return array_sum($sub);
}
Type declarations allow functions to require that parameters are of a certain type at call time. If the given value is of the incorrect type, then an error is generated: in PHP 5, this will be a recoverable fatal error, while PHP 7 will throw a TypeError exception.
However, you really probably just want to use array_sum() directly.
For reference, see:
http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration
http://php.net/manual/en/function.array-sum.php
Try this. It will create a function that has a reference to your array. When you change the array you can call the product of the function, and it will recalculate the sum.
$array = ['English' => '12', 'Swedish' => '12'];
function arraySumCb(&$subject) {
return function () use (&$subject) {
return array_sum($subject);
};
}
$sum = arraySumCb($array);
echo $sum(); // 24
$array['Swedish'] = '15';
echo $sum(); // 27
$array['Swedish'] = '10';
echo $sum(); // 22
Edit: This is how I would do it.
$array = ['English' => '12', 'Swedish' => '12'];
class SumMarks {
private $_subject;
public function __construct(array &$subject = []) {
$this->_subject = &$subject;
}
public function __toString() {
return "" . array_sum($this->_subject);
}
}
$sum = new SumMarks($array);
echo $sum; // 24
$array['Swedish'] = '10';
echo $sum; // 22
Edit: Proper use of PHP anonymous functions
I dont understand your question, please ask with specific question. .
But maybe this what are you want :
function sum_marks($sub){
$result = array_sum($sub);
retrun $result;
}
I have the following codes.
<?php
class Reg {
private $pros = array();
public function __set($key,$val) {
$this->pros($key)= $val;
}
public function __get($key) {
return $this->pros($key);
}
}
$reg= new Reg;
$reg->tst="tst";
echo $reg->tst;
?>
But when executing this script I got the error following.
Fatal error : can't use method return value in write context in line 5
I believe that to add an element to array is possible like the above.
$array = array();
$array('key')='value';
Please make clear that I was wrong.
Thanks
The is because of you are trying to set a functions return value. $this->pros($key) means calling pros($key) function. Not setting a value to $pros array.
The syntax is wrong. Setting values to array be -
$array['index'] = 'value';
Change
$this->pros($key)= $val; -> $this->pros[$key]= $val;
and
return $this->pros[$key];
Working code
$this->pros[$key] = $value;
OR
$keys = array($key);
$this->pros = array_fill_keys($keys,$value);
The array_fill_keys() function fills an array with values, specifying keys.
Syntax:
array_fill_keys(keys,value);
i create my own function with count of data.
Here is my function.
function Test($data){
global $acc;
$count = $acc->get_var("SELECT Count(*) FROM _users WHERE ID='$data'");
if($count == 0)
{
return true;
}else{
return false;
}
}
But i got This is error : "PHP Catchable fatal error: Object of class stdClass could not be converted to string
In This is Line $count = $acc->get_var("SELECT Count(*) FROM _users WHERE ID='$data'");
I try var_dump in function and Other ezSQL methods (query,num_rows) but i got every same error.
--
Edit:
Problem solved. I wrong posted $data
Please make sure that
$data
is not an array
its better practice to pass $acc by reference
function Test($data, &$acc){
$count = $acc->query("SELECT Count(*) FROM _users WHERE ID='$data'");
if($count == 0)
{
return true;
}else{
return false;
}
}
I'm trying to call a function and pass an array and other variables to it. this is the code I use to do that :
function fnGetElementsById($ArrCols, $tableName, $id) {
while($arrCols[$i])
{
if($i != 0)
{
$sql = $sql.',';
}
$sql = $sql.$arrCols[$i].' ';
$i++;
} }
the while line is the error or notice line and when I test with var_dump, the array is empty.
the calling code :
$arrCols = array(
0=>'marque',
1=>'prix'
);
$CDB->fnGetElementsById($arrCols, 'Portables', $_POST['id1']);
thank you
You haven't defined $i before the while loop is invoked. Therefore you are basically trying this:
while($arrCols[null]) {
Also your parameter is $ArrCols and the variable name in the while conditional is $arrCols (lowercase first letter).
You need to fix both of these.
You have a case issue.
function fnGetElementsById($ArrCols, $tableName, $id) {
while($arrCols[$i])
Should be
function fnGetElementsById($arrCols, $tableName, $id) {
while($arrCols[$i])
Notice change from ArrCols to arrCols.
Also you did not initialize your $i variable.
Please set it to 0 before using.
What is the correct way to push into an array that is a property of an object?
Error:
Fatal error: Cannot use [] for reading in ... on line ..
Code:
class StreamResponse
{
public $messages ;
function __construct($post)
{
$strippost = stripslashes_deep( $_POST );
foreach ($strippost['messages'] as $message)
{
$m = json_decode($message, true);
$this->$messages[] = $m; //<-- This line is the line with the issue
}
}
}
Don't use the dollar sign before messages when you are using $this
$this->messages[] = $m;