Reputation: 159
I seem to be having a problem with my foreach loops. I am trying to insert records into database tables using foreach loops, but it is not working correctly. Instead of 3 records being added to each table, 1 record is being added to each table. Also, each record that is being added into each table, should all be in the 1st table.
Here is the data from the tableinfo database table I am using to create the tables.
displayname dbname category distance distance_id
Male 40 to 49 10KM M40_Male40to4910KM M40 10KM 3
Male 40 to 49 21KM M40_Male40to4921KM M40 21KM 2
Male 40 to 49 42KM M40_Male40to4942KM M40 42KM 1
And here is the code I am trying to get to work:
$tabledata = $this->db->get('tableinfo');
$i = 0;
$_temp = array();
foreach($tabledata->result() as $row) {
$data[$i] = $this->callAPI("lateststandings&records=3&category=" . $row->category . "&distance=" . $row->distance_id);
array_push($_temp, $row->dbname);
$i++;
}
$j = 0;
foreach($data[$j]['Records']['Record'] as $record) {
$record['FirstName'] .= " " . $record['LastName'];
$this->db->replace($_temp[$j], $record);
$j++;
}
I am sure the problem I am having is with the $i and $j counter variables but I am not sure where I am going wrong. It seems that the counter variables do not increment correctly.
Upvotes: 2
Views: 134
Reputation: 405
Lets try to do it in this way
$tabledata = $this->db->get('tableinfo');
$data = [];
$_temp = array();
foreach($tabledata->result() as $row) {
$data[] = $this->callAPI("lateststandings&records=3&category=" . $row->category . "&distance=" . $row->distance_id);
array_push($_temp, $row->dbname);
}
foreach($data as $key => $arr) {
foreach ($arr['Records'] as $record)
{
$record['FirstName'] .= " " . $record['LastName'];
$this->db->replace($_temp[$key], $record);
}
}
Upvotes: 2