how can I run for loop 4 times - for-loop

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

Related

see value according to the array

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

generate more than one random code in laravel and save it to database

I'm a newbie in laravel. I have to generate multiple random characters at once in my laravel project and then save them to database. Any example or advise that quite easy to understand? thank you
A for loop may be appropriate for you in this case. If you don't have an object for the codes, you can do something like this:
$total_wanted = 100;
for ( $i = 0; $i < $total_wanted; $i++ ) {
DB::table('codes')->insert(['code' => uniqid("prefix")]);
}
If you do have a Code object, you can do it like this instead:
$total_wanted = 100;
for ( $i = 0; $i < $total_wanted; $i++ ) {
$code = new Code;
$code->code = uniqid("prefix");
$code->save();
}
echo uniqid('code_', true);
You generate a random, unique string based on current timestamp, so it can not be a duplicate.
Then simple store it in the database as a varchar
More about this function here

how to increment day by one to find the next desired day in laravel

I've an array of day name like ['Wednesday','Sunday','Monday']
now I want to find the next available dates That matched with the array from today's date. I want date. I tried for so long but none of them were successful. My code is given below
$datesAvailable = array();
$count = 0;
$dateToday = Carbon::now()->toDateTimeString();
//$avlDays is the array of day names
$avlDays = DB::table('doctor_schedules')
->select('available_days')
->where('department',$receivedDepartment)
->Where('doctor_id', $receivedDoctor)
->get();
for($k=5; $k > 0; $k++)
{
$dateToday = $dateToday->endOfWeek();
// $parsedDate = Carbon::parse($dateToday);
$dateTodayFormated = new Carbon($dateToday);
$nextDayName = $dateTodayFormated->englishDayOfWeek;
for($i = 0; $i <= 2; $i++)
{
if($avlDays === $nextDayName)
{
$datesAvailable[$count] = $nextDayName;
$count++;
}
}
}
return (['availableDates' => $dateToday]);
Solved
For the current date use
now()
https://laravel.com/docs/6.x/helpers#method-now
Carbon does this.
foreach ($avlDays as $day) {
$nextDay = Carbon::parse("next $day");
// do something with $nextDay...
}
So, assuming $avlDays is returning you a Carbon date range:
$avlDays = DB::table('doctor_schedules')
->select('available_days')
->where('department',$receivedDepartment)
->Where('doctor_id', $receivedDoctor)
->get()
->toArray();
The code below assumes that the array value from ['monday', 'tuesday' ...] is saved as $weekday.
If you are trying to find a specific date within a range that falls on a certain weekday, you can do something like:
$availableAppointments = [];
foreach ($avlDays as $day) {
if ($day->isDayOfWeek($weekday)) {
$availableAppointments[] = $day;
}
Then you can use $availableAppointments to list out the available dates on that day of the week.

Eloquent Laravel : creating a counter with foreach loop

using Laravel's Eloquent, I'm trying to add an incremental counter to certain rows based on their 'product_id'.
I thought I could do it like this :
$foos = GanttTask::where('product_id',1)->orderBy('date', 'ASC');
$counter = 1;
foreach($foos as $foo){
$foo->custom_counter = $counter;
$counter +=1;
}
return $foos->get();
But this is of no effect on my data, custom_counter column doesn't change (but I get no error).
I tried to add $foo->save() within my loop with no effect.
Get first all record and then modify single record using save method like this:
$foos = GanttTask::where('product_id',1)->orderBy('date', 'ASC')->get(); // Get All records
$counter = 1;
foreach($foos as $foo){
$foo->custom_counter = $counter;
$counter +=1;
$foo->save(); // Update each record
}
Good Luck !!!
To actually update the database, I reckon you need to persist each data object foo individually not the collection foos as a whole.
$foos = GanttTask::where('product_id',1)->orderBy('date', 'ASC');
$counter = 1;
foreach($foos as $foo){
$foo->custom_counter = $counter;
$counter +=1;
$foo->save(); // Persisting data here
}

Saving issue in laravel 5.4

I'm trying to save multipliable ids in the database using loop but it only save the last id 3 times so I made for loop to test it
for ($i = 0; $i < 10; $i ++)
{
$this->product_id = $i;
$this->shop_name = $shop;
$this->save();
}
it saves number 9 three times and stops ????
This issue never happened before
You need to create a new instance with each iteration, for example:
for ($i = 0; $i < 10; $i++) {
$this->create(['product_id' => $i, 'shop_name' => $shop]);
}
Your code updates a single instance multiple times.

Resources