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
Related
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
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
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
}
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.
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);