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.
Related
I have an small piece of PHP code that needs to put every file in the current directory into an array.
I have done this by making reading the dir with glob() and when it meets another dir it will loop.
My code I have as of now:
<?php
$find = '*';
$result = array();
function find($find)
{
foreach (glob($find) as $entry)
{
$result[] = $entry;
echo $entry.'<br>';
if (is_dir($entry)){
$zoek = ''.$entry.'/*';
find($zoek);
}
}
return $result;
}
print_r(find($find));
?>
When I execute the code the echo print exactly what I want. But the printed array doesn't give me the values I want, it only gives the values in the first dir it will come by then it seems to stop adding the value in the array.
What am I doing wrong?
You need to actually preserve the results you produce in the recursive callings to your function:
<?php
function listNodesInFolder($find) {
$result = [];
foreach (glob($find) as $entry) {
$result[] = $entry;
if (is_dir($entry)) {
$result = array_merge($result, find($entry.'/*'));
}
}
return $result;
}
print_r(find('*'));
Once on it I also fixes a few other issues with your code:
$result should be declared as an array inside your function, that that even if it does not loop you still return an array and not something undefined.
indentation and location of brackets got adjusted to general coding standards, that makes reading your code much easier for others. Get used to those standards, it pays out, you will see.
no need for an extra variable for the search pattern inside the conditional.
a speaking name for the function that tells what it actually does.
you should not name variables and functions alike ("find").
You need to add the result of find() to the array
Edit added array_merge - Cid's idea
<?php
$find = '*';
function find($find)
{
$result = array();
foreach (glob($find) as $entry)
{
$result[] = $entry;
echo $entry.'<br>';
if (is_dir($entry)){
$zoek = ''.$entry.'/*';
$result = array_merge($result, find($zoek));
}
}
return $result;
}
print_r(find($find));
?>
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'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.'%') ;
});
i have an array with conditions i have already prepared to pass it to the query:
array:
('u.registered = 1','u.active = 0', 'u.gender = M')
when i pass to the query, it works with the number comparison but not with the varchar which is M. The error appears in "gender", it says it is a semantical error. I assume is because i am not using expr()->literal('M'), but i can't do this because the query is "already built"..
Is there an alternative way so i don't have to code all over again?
this is the code:
public function customR($data){
// var_dump($data);die();
$this->qb = $this->em->createQueryBuilder();
$andX = $this->qb->expr()->andX();
$this->qb->select('u')
->from('models\User','u');
foreach ($data as $value){
$andX->add($value);
}
$this->qb->add('where', $andX);
$query = $this->qb->getQuery();
// var_dump($query);die();
$obj = $query->getResult();
var_dump($obj);die();
if (!empty($obj)){
return $obj;
return false;
}
}
I found no way to do this, so i just changed it a little bit.
I send an array with some elements, just to have the reference of what exists and what doesn't.
So, in my Data Service I've created a function and a snippet of that function to solutionate my question was to do this:
if($key == 'gender'){
foreach($value as $key=>&$v){
$condition = ('u.gender = '. $this->qb->expr()->literal($v));
$orX->add($condition);
}
}
I'm new to OOP in PHP, is that to seems correct ?
class whatever {
Function Maths() {
$this->sql->query($requete);
$i = 0;
while($val = mysql_fetch_array($this)) {
$tab[i][average] = $val['average'];
$tab[i][randomData] = $val['sum'];
$i=$i+1;
}
return $tab;
}
I want to access the data contained in the array
$foo = new whatever();
$foo->Maths();
for ($i, $i <= endOfTheArray; i++) {
echo Maths->tab[i][average];
echo Maths->tab[i][randomData];
}
Thank you ;)
EDIT: i want to output the result of the SQL query as an array, so i can access it from outside the class
In the interest of helping you out, here are some modifications. Please hear this, though: a lot of this might not make sense without a good background in PHP or OOP in general. You should look at #webbiedave's link.
class whatever {
static function maths() {
$tabs = array();
$results = $this->sql->query($requete);
while($val = mysql_fetch_array($this)) {
$tabs = $val;
}
return $tabs;
}
This fixes syntax errors and logic errors (for instance, the creation of the $results variable to hold the SQL query run).
I made the maths method static, since there's really no need to instantiate a whatever() object with this current example.
Here are some modifications to how this would be used:
$results = whatever::maths();
foreacho ($results as $result) {
echo $result['average'];
echo $result['randomData'];
}
Since maths() returns something, you need to store that in a variable; simply calling it, as you did previously, doesn't do anything.
That convoluted for loop can be replaced with a foreach loop.
Please check out PHP OOP basics:
http://www.php.net/manual/en/language.oop5.basic.php
Edit: Thanks for cleaning up the code. Try something along the lines of:
$tabs = array();
while($val = mysql_fetch_assoc($result)) {
$tabs[] = $val;
}
And:
$foo = new whatever();
$tabs = $foo->Maths();
for ($tabs as $tab) {
echo $tab['average'];
echo $tab['randomData'];
}
http://www.php.net/manual/en/language.oop5.basic.php