How to Use Two Foreach to Insert Data in Database

How to use two foreach to insert data in database

Hope this will help you :

Use insert_batch instead of insert like this :

public function saveMessage($seat_ids, $seat_labels, $busNumber, $bookingDate, $reportingTime, $departureTime)
{

foreach($seat_ids as $key => $seat_id )
{
$record[] = array(
'seatNumber' => $seat_id,
'seatLabel' => $seat_labels[$key],
'bookingDate' => $bookingDate,
'reportingTime' => $reportingTime,
'departureTime' => $departureTime,
'busNumber' => $busNumber,
'seatUse' => 'Enabled',
'seatStatus' => 'Available');
}
$this->db->insert_batch('schedule', $record);
}

For more :https://www.codeigniter.com/user_guide/database/query_builder.html#inserting-data

How to loop through multiple foreach loops and insert data into the database

Assuming that your users array is indexed and your row data is an indexed array of associative arrays, you can marry-up/sync the data with a single loop using the shared index ($i).

$users = User::all();
$rows = $data->toArray();
foreach ($users as $i => $user) {
if (!$user->admin) {
$Data[]=
[
'user_id' => $user->id,
'avatar' => 'avatar.png',
'about' => isset($rows[$i]['description']) ? $rows[$i]['description'] : 'default'
];
}
}
profile::insert($Data);

p.s. It may be advisable (depending on your data) to check that the corresponding $row data exists with isset() before trying to access the value.

Laravel multiple foreach for inserting data into database

Not sure from where date will get but for other data first change form design

<form action="{{route("test")}}" method="post">
@csrf
<table id="example2" class="presence-table table table-bordered table-hover">
<thead>
<tr>
<th class="align-middle" rowspan="2">#</th>
<th class="align-middle" rowspan="2">Name</th>
<th class="align-middle" colspan="6">Date</th>
</tr>
<tr>
<th class="align-middle">1 Jan</th>
<th class="align-middle">2 Jan</th>
<th class="align-middle">3 Jan</th>
<th class="align-middle">4 Jan</th>
<th class="align-middle">5 Jan</th>
<th class="align-middle">6 Jan</th>
</tr>
</thead>
<tbody>

@php

$employee=["Gunawan","Roy"];
@endphp
@foreach($employee as $key=>$value)
<tr class="data-row">
<td class="align-middle iteration"></td>
<input type="hidden" name="row[{{$key}}][employee_name]" value="{{$value}}">
<td>Gunawan</td>
<td>
<div class="form-group">
<select class="form-control" name="row[{{$key}}][presence][1]" required>
<option value="">-- Select --</option>
<option value="1">Full</option>
<option value="0.5">Half</option>
<option value="0">Zero</option>
</select>
</div>
</td>
<td>
<div class="form-group">
<select class="form-control" name="row[{{$key}}][presence][2]" required>
<option value="">-- Select --</option>
<option value="1">Full</option>
<option value="0.5">Half</option>
<option value="0">Zero</option>
</select>
</div>
</td>
<td>
<div class="form-group">
<select class="form-control" name="row[{{$key}}][presence][3]" required>
<option value="">-- Select --</option>
<option value="1">Full</option>
<option value="0.5">Half</option>
<option value="0">Zero</option>
</select>
</div>
</td>
<td>
<div class="form-group">
<select class="form-control" name="row[{{$key}}][presence][4]" required>
<option value="">-- Select --</option>
<option value="1">Full</option>
<option value="0.5">Half</option>
<option value="0">Zero</option>
</select>
</div>
</td>
<td>
<div class="form-group">
<select class="form-control" name="row[{{$key}}][presence][5]" required>
<option value="">-- Select --</option>
<option value="1">Full</option>
<option value="0.5">Half</option>
<option value="0">Zero</option>
</select>
</div>
</td>
<td>
<div class="form-group">
<select class="form-control" name="row[{{$key}}][presence][6]" required>
<option value="">-- Select --</option>
<option value="1">Full</option>
<option value="0.5">Half</option>
<option value="0">Zero</option>
</select>
</div>
</td>
</tr>
@endforeach
</tbody>
</table>
<button type="submit" class="float-right btn btn-info">Save</button>
</form>

and in controller

 $data= collect($request->row)->map(function ($row,$key)use($request){

return collect($row['presence'])->map(function ($presence)use($row){

return ['name'=>$row['employee_name'],'value'=>$presence];
});
})->flatten(1)->toArray();

and for insert to database

ModelName::insert($data);

Updated

You can do somethink like below for each date

<td>
<div class="form-group">
<select class="form-control" name="row[{{$key}}][presence][1]['presenceType']" required>
<option value="">-- Select --</option>
<option value="1">Full</option>
<option value="0.5">Half</option>
<option value="0">Zero</option>
</select>
</div>
</td>
<td>
<div class="form-group">
<input type="text" name="row[{{$key}}][presence][1]['presencedate']" value="">
</div>
</td>

and in controller

  return ['name'=>$row['employee_name'],'value'=>$presence['presenceType'],'dae'=>$presence['presencedate']];

Update2

<form action="{{route("test")}}" method="post">
@csrf
<table id="example2" class="presence-table table table-bordered table-hover">
<thead>
<tr>
<th class="align-middle" rowspan="2">#</th>
<th class="align-middle" rowspan="2">Name</th>
<th class="align-middle" colspan="6">Date</th>
</tr>
<tr>
<th class="align-middle">1 Jan</th>
<th class="align-middle">date</th>
<th class="align-middle">2 Jan</th>
<th class="align-middle">3 Jan</th>
<th class="align-middle">4 Jan</th>
<th class="align-middle">5 Jan</th>
<th class="align-middle">6 Jan</th>
</tr>
</thead>
<tbody>

@php

$employee=["Gunawan","Roy"];
$dates = ["1 Jan", "2 Jan", "3 Jan", "4 Jan", "5 Jan", "6 Jan"];
@endphp
@foreach($employee as $key=>$value)
<tr class="data-row">
<td class="align-middle iteration"></td>
<input type="hidden" name="row[{{$key}}][employee_name]" value="{{$value}}">
<td>Gunawan</td>
<td>
<div class="form-group">
<select class="form-control" name="row[{{$key}}][presence][1][presenceValue]" required>
<option value="">-- Select --</option>
<option value="1">Full</option>
<option value="0.5">Half</option>
<option value="0">Zero</option>
</select>
</div>
</td>
<td>
<div class="form-group">
<select class="form-control" name="row[{{$key}}][presence][1][presenceDate]" required>

<option value="">-- Select --</option>
@foreach($dates as $date)
<option value="{{$date}}">{{$date}}</option>
@endforeach

</select>
</div>
</td>
<td>
<div class="form-group">
<select class="form-control" name="row[{{$key}}][presence][2][presenceValue]" required>
<option value="">-- Select --</option>
<option value="1">Full</option>
<option value="0.5">Half</option>
<option value="0">Zero</option>
</select>
</div>
</td>
<td>
<div class="form-group">
<select class="form-control" name="row[{{$key}}][presence][2][presenceDate]" required>

<option value="">-- Select --</option>
@foreach($dates as $date)
<option value="{{$date}}">{{$date}}</option>
@endforeach

</select>
</div>
</td>
<td>
<div class="form-group">
<select class="form-control" name="row[{{$key}}][presence][3][presenceValue]" required>
<option value="">-- Select --</option>
<option value="1">Full</option>
<option value="0.5">Half</option>
<option value="0">Zero</option>
</select>
</div>
</td>
<td>
<div class="form-group">
<select class="form-control" name="row[{{$key}}][presence][3][presenceDate]" required>

<option value="">-- Select --</option>
@foreach($dates as $date)
<option value="{{$date}}">{{$date}}</option>
@endforeach

</select>
</div>
</td>
<td>
<div class="form-group">
<select class="form-control" name="row[{{$key}}][presence][4][presenceValue]" required>
<option value="">-- Select --</option>
<option value="1">Full</option>
<option value="0.5">Half</option>
<option value="0">Zero</option>
</select>
</div>
</td>
<td>
<div class="form-group">
<select class="form-control" name="row[{{$key}}][presence][4][presenceDate]" required>

<option value="">-- Select --</option>
@foreach($dates as $date)
<option value="{{$date}}">{{$date}}</option>
@endforeach

</select>
</div>
</td>
<td>
<div class="form-group">
<select class="form-control" name="row[{{$key}}][presence][5][presenceValue]" required>
<option value="">-- Select --</option>
<option value="1">Full</option>
<option value="0.5">Half</option>
<option value="0">Zero</option>
</select>
</div>
</td>
<td>
<div class="form-group">
<select class="form-control" name="row[{{$key}}][presence][5][presenceDate]" required>

<option value="">-- Select --</option>
@foreach($dates as $date)
<option value="{{$date}}">{{$date}}</option>
@endforeach

</select>
</div>
</td>
<td>
<div class="form-group">
<select class="form-control" name="row[{{$key}}][presence][6][presenceValue]" required>
<option value="">-- Select --</option>
<option value="1">Full</option>
<option value="0.5">Half</option>
<option value="0">Zero</option>
</select>
</div>
</td>
<td>
<div class="form-group">
<select class="form-control" name="row[{{$key}}][presence][6][presenceDate]" required>

<option value="">-- Select --</option>
@foreach($dates as $date)
<option value="{{$date}}">{{$date}}</option>
@endforeach

</select>
</div>
</td>
</tr>
@endforeach
</tbody>
</table>
<button type="submit" class="float-right btn btn-info">Save</button>
</form>

and in controller

$data= collect($request->row)->map(function ($row,$key)use($request){

return collect($row['presence'])->map(function ($presence)use($row){

return ['name'=>$row['employee_name'],'value'=>$presence['presenceValue'],'date'=>$presence['presenceDate']];
});
})->flatten(1)->toArray();
dd($data);

ForEach loop data insert into two database table

You don't need $imagesarray[] if this is for a single product.

Say

 $data[5] = "Arizona Diamondbacks.png&arizona-cardinals.png&Atlanta Braves.jpg";
$imagesarray = explode('&',$data[5]);

Will create an array like

Array
(
[0] => Arizona Diamondbacks.png
[1] => arizona-cardinals.png
[2] => Atlanta Braves.jpg
)

Then using single variable assign the query to perform

foreach($imagesarray as $key => $image){

if($key == 0 )
{
$query = "UPDATE " . DB_PREFIX . "product SET image = 'media/" . $image . "' WHERE product_id = '" . (int)$product_id . "'";
} else {
$query = "INSERT INTO " . DB_PREFIX . "product_image SET image = 'media/" . $image . "' , product_id = '" . (int)$product_id . "'";
}
$this->db->query($query);
}

Insert data of multiple form fields by foreach loop

First, see here How can I prevent SQL injection in PHP? Do your query differently or you're screwed.

Since name and email are indexed the same, just loop one and reference the other by key:

foreach($_POST['name'] as $key => $val) {
$name = $val;
$email = $_POST['email'][$key];

// prepared statement query
}

Or you could do inputs like this to get arrays more like database rows:

Name  : <input type="text" name="data[0][name]"><br>
Email : <input type="text" name="data[0][email]"><br>

Then loop it easily:

foreach($_POST['data'] as $val) {
$name = $val['name'];
$email = $val['email'];
}

Insert data to database using foreach loop php

There are two things wrong which i can see at a glance.

  1. mysql is deprecated, use mysqli instead. Reference
  2. Since you will have to switch over to mysqli you will need to reference your database connection every time you want to do a mysql database related operation, unless you do some PHP magic (classes or methods).

Other than these two i guess there is no problem in your code, the for loop should work perfectly fine.



Related Topics



Leave a reply



Submit