This is my code
foreach($orders as $order) {
array_push($list, array($order['name'],$order['email'],$order['financial_status']));
}
Now i got the value of financial_status is 1, I want to get value as true. I want to add condition before the foreach loop.
Can anyone help me? Thanks in advance!.
You need this maybe:
foreach($orders as $order) {
if ($order['financial_status'] == 1){
$financial_status = true;
} else {
$financial_status = false;
}
array_push($list, array($order['name'],$order['email'],$financial_status));
}
It will put boolean in array.
foreach($orders as $order) {
if ($order['financial_status'] == 1){
return true
} else {
return false
}
array_push($list, array($order['name'],$order['email'],$financial_status));
}
check this edit
make sure to comparing with financial_status in table
if you want financial_status to return true when its value is 1 in sql and if true value required to be a different value change ==1 to that value
Related
I have next code:
$list = SparePartApplicationPositionProvider::where('app_id',$app_id)->with(['provider','application_position'])->orderBy('apos_id')
->get();
It display providers by position, all works is good.
I would like change some value and in this case it works:
foreach ($list as $value) {
if(($value->id > 3)){
$value->enter_price=3141592;
};
}
But in this case it's doesnt work
if($value->id > 3){
if($value->application_position->id == 26){
$value->application_position->name_detail='test';
}
};
It doesnt "see" ($value->id > 3) condition, work only ($value->application_position->id == 26) condition. How to fix?
application_position is a collection of objects. You'll need to iterate through it to find the one with the id of 26.
if($value->id > 3){
foreach($value->application_position as $position) {
if($position->id == 26){
$position->name_detail='test';
$position->save(); // Don't forget to save, or else it won't update
}
}
};
In this case,
$value->application_position;
will try to access application_position from $value via magic. which will return null and therefor your if function will return false.
So what you need to do is call it like a function and access the id when you are looping through the collection
$value->application_position();
so your code should be like this
if($value->id > 3){
$positions = $value->application_position();
foreach($positions as $position){
if($position->id == 26){
$position->name_detail = 'test';
}
}
};
How would I write the following foreach with some conditions using array_filter?
foreach ($categories as $category) {
if ($this->request->getParam('category_id')) {
if ($category->getCategoryId() == $this->request->getParam('category_id')) {
$selectedCategory = $category;
break;
}
} else {
No category id in request. Select the first one.
if (array_key_exists(0, $categoryTree) &&
$category->getCategoryId() == $categoryTree[0]['id']
) {
$selectedCategory = $category;
break;
}
}
}
First off, using array_filter isn't helpful in this case as it reduces an array instead of selecting an element. But to show its principles, you could rewrite the code to something like this.
if($this->request->getParam('category_id')){
$filteredCategories = array_filter($categories, function ($category) use ($this){
return $category->getCategoryId() == $this->request->getParam('category_id');
});
if(count($filteredCategories)>0){
return $filteredCategories[0];
}
} else {
[...]
}
I think you want an intersection function and not an array filter.
function key_compare_func($key1, $key2)
{
if ($key1 == $key2->getCategoryId()) {
return 1;
} else {
return 0;
}
}
$selectedCategory = array_intersect_ukey(
$this>request>getParam('category_id'),
$categories,
'key_compare_func'
)
For more information on the different array functions you can look at the PHP manual
I'd created an if statement "if ($user_roles == 3) " and this $user_roles has a value of "3" the condition is supposed to be true but the result is always false.
here is my code below:
public function ViewSponsorInfo($sponsor_id)
{
$id = $sponsor_id;
$user_id = User::where('id','=',$id)->get();
$user_roles = [];
foreach ($user_id as $id) {
array_push($user_roles, $id->role);
}/*
dd($user_roles);*/
if ($user_roles == 3) {
$orga = Organization::where('orga_id','=',$sponsor_id)->get();
dd($orga);
return view('pages.Ngo.View-Sponsor-Information',compact('orga'));
}else{
$indi = Individual::where('indi_id','=',$sponsor_id)->get();
dd($indi);
return view('pages.Ngo.View-Sponsor-Information',compact('indi'));
}
}
$user_rolesis not 3 and can never be. it's an array.
its contents, however, can be three.
try:
if(in_array(3, $user_roles)) { ...}
for reference: in_array
$user_roles is an array. So your if statement is always false.
I think, try this one.
foreach ($user_id as $id) {
array_push($user_roles, array('role' => $id->role));
}/*
dd($user_roles);*/
foreach ($user_roles as $user_role) {
if ($user_role['role'] == 3) {
$orga = Organization::where('orga_id','=',$sponsor_id)->get();
dd($orga);
return view('pages.Ngo.View-Sponsor-Information',compact('orga'));
}else{
$indi = Individual::where('indi_id','=',$sponsor_id)->get();
dd($indi);
return view('pages.Ngo.View-Sponsor-Information',compact('indi'));
}
}
or
foreach ($user_id as $id) {
array_push($user_roles, array('role' => $id->role));
}/*
dd($user_roles);*/
foreach ($user_roles as $user_role) {
if ($user_role->role == 3) {
$orga = Organization::where('orga_id','=',$sponsor_id)->get();
dd($orga);
return view('pages.Ngo.View-Sponsor-Information',compact('orga'));
}else{
$indi = Individual::where('indi_id','=',$sponsor_id)->get();
dd($indi);
return view('pages.Ngo.View-Sponsor-Information',compact('indi'));
}
}
Please said something upon it after tried this one.
Judging from what you show your function ViewSponsorInfo() by its very name is only interested in detecting if this user has the sole role '3' in your db.
I mean the outcome is binary isn't it? Either they are a 3 or they are not, no need to go looping thru results.
$user_id = User::where('id','=',$id)->get();
if($user_id[0] !== 3 ) { //***
doSponsorLink();
}else{
doNonSponsorLink();
}
I'm not familiar with Laravel or the db layer your are using, so maybe that is even simply if($user_id).
untested, and here I am assuming user can only have 1 single role, and we do not seem to be making allowances for the user not existing in the db.
I am trying to print "Backed" when user id is in array.
Here is my code:
$backId = app()->user->model()->id;
$backers = MProjectBacker::model()->findAll('projectId=:pid', array(':pid' => $data->id));
foreach ($backers as $b) {
if (in_array($backId, $b, true)) {
echo "Backed!";
} else {
echo "Not Backed!";
}
}
But no result and no error.
"If the third parameter strict is set to TRUE then the in_array() function will also check the types of the needle in the haystack."
But why use in_array function to this?
Why dont you just check the field where you store the $backId?
foreach ($backers as $b) {
if ( (int) $b['fieldNameWhereYouStoreTheBackId'] == (int) $backId ) {
echo "Backed!";
}
else {
echo "Not Backed!";
}
}
I'm generating a random code, and I need to check to be sure that the code isn't already in the database. I'm assuming this requires some type of a loop. I have my query all setup and I need it to run a block of code again if mysql_num_rows == 0.
Use a do...while loop:
do {
// Your logic
} while (condition);
$key = true;
while($key){
// Do stuff
if(mysql_num_rows($result) > 0) $key = false;
}
Simple upgrade of James L. script - loop script for test if exist login ID in database. If exist, will add +1 after login:
$key = true;
$a = 1;
$login_test_origin=$login_test;
while($key){
$query_test="SELECT count(*) as 'all' FROM user WHERE login='$login_test'";
$row_test=mysql_fetch_array(mysql_query($query_test));
$error=$row_test[all];
if($error > 0) {
$key = true;
$login_test=$login_test_origin.$a;
$a++;
}
else {
$key = false;
$login=$login_test;
}
}
echo"Used login ID: $login";
Here is a quite different route:
while(true){
if(/* Your logic which you expect to be true */){
break;
}
}