Trying to write the cleanest code in Blade of a foreach where in the position 3 (if first and second exists) add a different code.
I'm trying using chunk() but it doesnt allow do non regulars array.
Write position 1 if exists
Write position 2 if exists with different class
Write some content between array position 2 and 3 from different array
Write position 3 if exists
Write position 4 if exists with different class
...
why u use chunk? u can use $key
#foreach($arr as $key => $value)
#if ($key === 2)
do something
#endif
#endforeach
or
#for ($i = 0; $i < count($arr); $i++)
#if ($i === 2)
do something
#endif
#endfor
Related
I have 2 arrays and I want to check those arrays with 2 value matches,
if those 2 value not matches I want to insert into database.
Code I have tried :
$data1 = {['reg_no'=>123,'name'=>'John'],['reg_no'=>1234,'name'=>'Lisa']};
$data2 = {['reg_no'=>123,'name'=>'John'],['reg_no'=>1234,'name'=>'Lisa'],['reg_no'=>12345,'name'=>'Roger']};
foreach($data1 as $d1)
foreach($data2 as $d2)
if($d1['reg_no'] != $d2['reg_no'] && $d1['name'] != $d2['name'])
//insert into database
// this not work because it will enter all data that not match
endif
endforeach
endforeach
you can use FirstOrNew Or FirstOrCreate in laravel.
like insert if not exist
$students = User::firstOrNew(['student_id' => $request->student_id, 'course_code' => $request->course_code]);
$students->foo = $request->foo;
$students->save();
$data1 = {['reg_no'=>123,'name'=>'John'],['reg_no'=>1234,'name'=>'Lisa']};
$data2 = {['reg_no'=>123,'name'=>'John'],['reg_no'=>1234,'name'=>'Lisa'],['reg_no'=>12345,'name'=>'Roger']};
$data = array_uintersect($data1, $data2, "strcasecmp");
if($data['reg_no'] != $data1['reg_no'] && $data2['reg_no']) {
// insert
}
I do a select and get several results in array but I need to get the correct value for each step and set up a condition.
$step = DB::table('records')->where('id_user',$userId)->get();
for($i = 0; $i < count($step); $i++)
{
echo $step[$i]->id_step;
}
Id_step returns me values for each step where on the blade I need to get and see if id_step = 1 is true id_step = 2 is true.
This for is returning me only one value and it has 3 records in the table.
First of all. After a select you get an instance of Eloquent\Collection
Not an array.
So that said to loop do this:
$steps = DB::table('records')->where('id_user',$userId)->get();
foreach($steps as $row) {
echo $row;
}
Since you are familiar with arrays do this:
$steps->toArray();
Now your result is an array
Working insert in view this code.
#for($i = 0; $i < count($step); $i++)
#if($step[$i]->id_step == 2)
working
#else
not working
#endif
#endfor
I have an array that I have counted and it equals 4. That is what it should equal. Now I want the foreach loop inside the for loop to only run 4 times. As it stands, I am getting to many results. Below is my latest attempt that does not work.
$networks = array();
$networks = ! empty( $instance['networks']) ? $instance['networks'] : '';
$size = count($networks); //size equals 4
for($i = 0; $i <= $size; $i++){
foreach ( $this->networks as $key => $value ) {
$network_names[ $key ] = $value['class'];
}
$i++;
}
the networks array is populated from a WordPress widget that has a repeating field section. The section allows the user to set social media icons. I currently have 4 social media icons set. On the front end, the page shows every social media icon available even though there are only 4 set. So I am trying to get the nested foreach loop to only run 4 times.
Currently your loop is executing 5 times..and you want it to run only 4 times... So Change this condition...for($i = 0; $i <= $size; $i++) to either for($i = 0; $i <$size; $i++) or for($i = 1; $i <= $size; $i++) like i.e.
$networks = array();
$networks = ! empty( $instance['networks']) ? $instance['networks'] : '';
$size = count($networks); //size equals 4
for($i = 0; $i < $size; $i++){
foreach ( $this->networks as $key => $value ) {
$network_names[ $key ] = $value['class'];
}
}
I have something more interesting for you..
it's a widget parent with children!
You can change field to load new fields from other classes
here's the link:
https://github.com/hishamdalal/parent_widget
I am take all field in for-each loop and take the difference of each two column
enter image description here
The above image 'tech_strt_km' is field name and take the difference of 122-22 and 200-122 how find these difference and store there values in another variyable i am using foreach loop for print these numbers
<?php
foreach($pexpn as $row) {
echo $row->tech_strt_km; ?><br>
} ?>
Any way to find the difference of same column
please help me !!
This should work fine. We add one to the current key to get the next value so we can subtract it from the current value, then we save these differences in an array diff for later usage.
$arr = array('22', '122', '200');
$diff = array();
foreach ($arr as $k => $v) {
if (!isset($arr[$k + 1])) {
// if we don't have a next item we are done
// break from foreach
break;
}
// abs only necessary if we expect negative differences and
// if we don't want that --- store differences in array
// for later usage
$diff[] = abs($arr[$k + 1] - $v);
}
print_r($diff);
I was wondering whether I could do an if statement for each iteration through the array. This is so that I can punctuate each user with either a ',' or an '&' depending on how many users.
#foreach ($copy as $user => $value)
#if ($user == 0)
{{$value->username}}
#endif
#if ($user == 1)
, {{$value->username}}
#endif
#if ($user == 2)
, {{$value->username}}
#endif
#if ($user == 3)
& {{$value->username}}
#endif
#endforeach
The code above punctuates correctly if there are 4 users but what if I have less than 4. How would I move the '&' to the 3rd user if there were 3 users for example?
When faced with issues such as this, try to find the most optimal solution. In this case you can determine that you need the comma in all but two places and the ampersand in one. So instead of having a condition for each array index, you should place one for commas and one for the ampersand:
#foreach ($copy as $user => $value)
{{$value->username}}
// Place commas after all but the last two items
#if ($user < count($copy) - 2)
,
#endif
// Place an ampersand before the last item
#if ($user == count($copy) - 2)
&
#endif
#endforeach
This will work with a list of any size.