I have 2 groups of banner ads and 2 banner ads. The database is like this.
groups_ads
| id | name | max_places
1 group1 3
2 group2 2
banner_ads
| id | group_id | url_images
1 1 https://example.com/banner.png
2 1 https://example.com/images.png
this code is to show all the banners_ads based on their group.
$res_arr = array();
$stmt = $conn->prepare("SELECT * FROM groups_ads");
$stmt->execute();
$stmt->setFetchMode(PDO::FETCH_ASSOC);
while ($res = $stmt->fetch())
{
$result = array();
$max_slot = $res['max_places'];
$count = 0;
$banners = array();
$stmts = $conn->prepare('
SELECT * FROM banner_ads
WHERE group_id = "'.$res['id'].'"
');
$stmts->execute();
$stmts->setFetchMode(PDO::FETCH_ASSOC);
while ($count < $max_slot && $banner = $stmts->fetch()){
$count++;
if($banner){
$banners = array(
"type" => 2,
"url" => $banner['url_images'],
"count" => $count
);
}else{
$banners = array(
"type" => "default",
"count" => $count
);
}
$result[$count] = $banners;
}
$res_arr[$res['id']] = $result;
}
the result of this code is this
Array
(
[1] => Array
(
[1] => Array
(
[type] => 2
[url] => https://example.com/banner.png
[count] => 1
)
[2] => Array
(
[type] => 2
[url] => https://example.com/images.png
[count] => 2
)
)
[2] => Array
(
)
)
Because I have 2 banner ads in my database which is in group 1.
The question is, how to fill the empty row based on max_places ? in database groups_ads, max_places for group 1 is 3. But in database banner_ads only have 2 banner. So how can I fill the empty places to my default string? so the result will be like this.
Array
(
[1] => Array
(
[1] => Array
(
[type] => 2
[url] => https://example.com/banner.png
[count] => 1
)
[2] => Array
(
[type] => 2
[url] => https://example.com/images.png
[count] => 2
)
[3] => Array
(
[type] => default
[count] => 3
)
)
[2] => Array
(
)
)
Thanks for help.