I'm very new to PHP and Laravel, I'm getting an eror that I can't make head or tail of.
public function filtered($col, $sort = null, $search = null, $ordering='desc')
{
$field = $this->table . '.' . $col ;
Log::info('BaseModel::filtered->' . $field) ;
$data = $this;
// check if search variable not empty
if ($search != null)
{
$data = $data->where(function ($query) use ($search){
return $query->where($field,'like','%'.$search.'%') ;
});
// check if sort variable not empty
if ($sort != null)
{
$sorts = explode('|', $sort);
$data = $data->orderBy($sorts[0],$sorts[1]);
}
}
...
The code above is giving the error: Undefined variable: field. From the Log output I get this:
[2017-06-21 06:32:25] local.INFO: BaseModel::filtered->organisation.name
I've tried calling the field variable by $this->field as well, also fails. I also get the same error if i reference the $col parameter directly. Yet both $search and $sort are fine.
$data = $data->where(function ($query) use ($search, $field) { //Add extra parameters inside your use same as search.
return $query->where($field,'like','%'.$search.'%') ;
});
Related
I'm new to CodeIgniter, and trying to find the best way to pass variable parameters to $ci->db->select()
Sometimes I need one field back, sometimes I need 3, but I'd like one function for this. Here's my get function:
function db_get($table, $what, $where) {
$this->db->where($where);
if( $what ) {
$this->db->select($what);
}
return $this->db->get($table)->row_array();
}
Can I call it like:
$table = 'table_name';
$what = array('field_1', 'field_2');
$where = array('field_3' => 'value');
$result = $ci->import_db->db_get($table, $what, $where);
Or do I need to implode it first? like:
$what = implode(",", $what);
I get some data externally but gets this error because the variable is "empty":
Undefined property: stdClass::$summary in /Applications/MAMP/htdocs/jobportal/functions.php on line 68
I have tried to build a function to help me:
$summary = convert_empty($user->summary);
function convert_empty($data) {
if(isset($data)) {
return $data;
} else {
return ".";
}
}
But the error is still there. I have tried isset, empty, and defined. I think I miss another point here - since none of it is working.
That means that the object $user doesn't have a summary member variable defined.
$summary = isset($user->summary) ? convert_empty($user->summary) : NULL;
Or
$summary = isset($user->summary) ? convert_empty(isset($user->summary) ? $user->summary : NULL);
Now you won't see the warning and $summary will be set to NULL, assuming that you're expecting $summary to be NULL in this situation in which $user->summary is undefined.
The second one allows your convert_empty to figure it out.
The issue is not in your function, but how you call it. The error is that you're trying to access ->summary but doesn't exists. You could use something like this:
$summary = convert_empty($user, 'summary');
function convert_empty($data, $key) {
if (isset($data->$key))
return $data->$key;
return ".";
}
Note that you should also test if $data is an object too.
if (is_object($data) && isset($data->$key)) { ... }
Or, without a function using conditional ternary operator :
$summary = isset($user->summary) ? $user->summary : '.';
EDIT for a deeper use :
convert_empty($user, 'positions', 'values', $i, 'title');
function convert_empty($obj) {
$error = '.';
$args = func_get_args();
array_shift($args); // remove $obj
$ref = $obj ;
foreach ($args as $arg) {
if (is_array($ref)) {
if (!isset($ref[$arg])) return $error ;
$ref = $ref[$arg] ;
}
elseif (is_object($ref)) {
if (!isset($ref->$arg)) return $error ;
$ref = $ref->$arg ;
}
}
return $ref ;
}
It clear shown that $file_ids is array type but still getting this error, and I used different function to know the data type of variable like gettype() it also return array.please help me to get rid from this headache.
Thanks in advance.
public function getFilesForLink(Request $request)
{
$file_ids = array();
$ids = $request->input('selected_file');
if (count($ids) > 0) {
foreach ($ids as $id => $value) {
$file_ids[] = base64_decode($value);
}
}
$link_id = $this->makeDownloadLink($file_ids, $_POST['password']);
if ($_POST['via'] == 'Email') {
$files = File::find($file_ids);
foreach ($files as $name) {
$files_name[] = $name->name;
}
$this->sendEmail($files_name, $link_id, $_POST['recipient'],
$_POST['subject'], $_POST['body']);
}
In one place you are using $file_ids and in others $files_ids so make sure you are using same variable.
In addition are you sure you have valid values in $file_ids array?
Looking at the comment the problem is:
$downloadLink->file_ids = $file_ids;
inside makeDownloadLink method.
You are doing something like this:
if (count($file_ids) > 1) {
$downloadLink->file_ids = implode(',', $file_ids);
}
$downloadLink->file_ids = $file_ids;
and this will fail when $file_ids is array. You should probably add else here like so:
if (count($file_ids) > 1) {
$downloadLink->file_ids = implode(',', $file_ids);
}
else {
$downloadLink->file_ids = $file_ids;
}
check this pieces of code
$files = File::get($file_ids);
replace get() with find()
$files = File::find($file_ids);
I HAVE already read through the other similar questions on stackoverflow and none of them have helped me to find a solution.
This has be completely confused, what was once working has randomly broken and I have no idea whats wrong, the error doesn't seem logical to me i'll show you what i mean.
public function nGetOverviewAccounts()
{
$results = Account::where('archive', '=', false)->select('id', 'display_name', 'server_ip_address', 'server_password', 'status_type_id', 'account_type_id')->get()->toArray();
$index = 0;
$accounts = [];
foreach($results as $result)
{
$accountType = AccountType::where('id', $result['account_type_id'])->select('name')->get()->toArray();
$statusType = StatusType::where('id', $result['status_type_id'])->select('name')->get()->toArray();
return $accountType[0]['name']; //this works
$accounts[$index]['account_type'] = $accountType[0]['name']; //this doesnt..
$accounts[$index]['status_type'] = $statusType[0]['name'];
$index++;
}
return $accounts;
}
That code is right next to each other in the function. The array $accountType looks like this.
0:{name: "Google"}
Which shows that it has an index of 0 but its not working.
EDIT: PLEASE READ
Im going to clear something up i seems to have put it across wrongly to you guys, the return statement is added by me AFTER i get the undefined index error i only added it to the code to show that it works when i return it but when i try to assign its value to another variable (without the return statement) i get the undefined index error.
Try this:
public function nGetOverviewAccounts()
{
$results = Account::where('archive', '=', false)->select('id', 'display_name', 'server_ip_address', 'server_password', 'status_type_id', 'account_type_id')->get()->toArray();
$accounts = [];
foreach($results as $key => $result)
{
$accountType = AccountType::where('id', $result['account_type_id'])->select('name')->get()->toArray();
$statusType = StatusType::where('id', $result['status_type_id'])->select('name')->get()->toArray();
if(!empty( $accountType[0]['name'])) && !empty( $statusType[0]['name'])){
$accounts[$key]['account_type'] = $accountType[0]['name']; //this doesnt..
$accounts[$key]['status_type'] = $statusType[0]['name'];
}
}
return $accounts;
}
Also if you want the first key of $accountType and $statusType then you can use first() instead of get(). If your keys are different then you can use the $index
Adding some checks never hurt anyone:
public function nGetOverviewAccounts() {
$results = Account::where('archive', '=', false)->select('id', 'display_name', 'server_ip_address', 'server_password', 'status_type_id', 'account_type_id')->get()->toArray();
$index = 0;
$accounts = [];
foreach($results as $result)
{
$accountType = AccountType::where('id', $result['account_type_id'])->select('name')->get()->toArray();
$statusType = StatusType::where('id', $result['status_type_id'])->select('name')->get()->toArray();
$accounts[$index]['account_type'] = (isset($accountType[0]) && isset($accountType[0]['name'])?$accountType[0]['name']:"Unknown";
$accounts[$index]['status_type'] = (isset($statusType[0]) && isset($statusType[0]['name'])?$statusType[0]['name']:"Unknown";
$index++;
}
return $accounts;
}
The problem is your "debug" code only checked if it was working for the 1st iteration of the loop, it may still have not been working for the Nth element.
However if this does fix your problem the next question is why are there accounts associated with invalid account type ids. This may be a symptom of a larger problem.
We are trying to call the same function from within the function but it won't work.
We belive that there is a &$result variable that should be placed somewhere , we have seen it in other parts of our code that another person has written, but we don't know where and how it works.
Could someone please explain?
Here is our code if you want to have a look:
$parentID = $_POST['id'];
$choosenCategory = $_POST['choosenCategory'];
$count = 0;
function count_child($parentID, $choosenCategory){
foreach($parentID as $thisID){
foreach($_SESSION['items'][$thisID]['Children'] as $ChildID){
$DatabaseID = $ChildID;
$ItemCategory = $_SESSION['items'][$DatabaseID]['ItemCategory'];
$ItemName = $_SESSION['items'][$DatabaseID]['ItemName'];
$ItemStatus = $_SESSION['items'][$DatabaseID]['ItemStatus'];
$ParentID = $_SESSION['items'][$thisID]['DatabaseID'];
$Children = $_SESSION['items'][$DatabaseID]['Children'];
$Dependencies = $_SESSION['items'][$DatabaseID]['Dependencies'];
if($ItemCategory == $choosenCategory){
$count++;
}
if($ItemCategory !== "RWP" && $ItemCategory !== "US" && $levels === "all"){
$array = array();
count_child($ChildID, $choosenCategory);
}
}
}
}
count_child($parentID, $choosenCategory);
$json = json_encode($count);
echo $json;
It always output 0, regardless of what input we give.
Try to return $count from the function;
Like this:
$parentID = $_POST['id'];
$choosenCategory = $_POST['choosenCategory'];
function count_child($parentID, $choosenCategory){
$count = 0;
foreach ($parentID as $thisID){
$aChild = &$_SESSION['items'][$thisID]['Children'];
foreach ($aChild as $ChildID){
$DatabaseID = $ChildID;
$ItemCategory = $_SESSION['items'][$DatabaseID]['ItemCategory'];
$ItemName = $_SESSION['items'][$DatabaseID]['ItemName'];
$ItemStatus = $_SESSION['items'][$DatabaseID]['ItemStatus'];
$ParentID = $_SESSION['items'][$thisID]['DatabaseID'];
$Children = $_SESSION['items'][$DatabaseID]['Children'];
$Dependencies = $_SESSION['items'][$DatabaseID]['Dependencies'];
if ($ItemCategory == $choosenCategory){
$count++;
}
if ($ItemCategory !== "RWP" && $ItemCategory !== "US" && $levels === "all"){
$array = array();
paint_child($ChildID, $choosenCategory);
}
}
}
return $count;
}
$count = count_child($parentID, $choosenCategory);
$json = json_encode($count);
echo $json;
Depending on the php version and which datatype you are using, php creates a copy of your data when you call the function. that copy is used within that function. when you are now modify the data then the copy is modify. Later when your call is finished, the copy will be deleted (in each function call in every recursion step). That & operation avoids a copy and sends a reference. No copy will be created. The reference operator can used in function parameters also at return values, but it's more common in function parameters.