I'm just experimenting a little with PHP and PDO working with a MySQL database and I'm a little stumped as to why after getting the results, storing them correctly in a multi-dimensional array and looping through them it outputs one of the array data twice.
Essentially here's the query to grab the data:
SELECT b.Price, b.ImgURL, m.Name, f.ID, f.Family, f.URL FROM Products AS b INNER JOIN Manufacturers AS m ON m.ID = b.Manufacturer INNER JOIN FamilyLookUp AS l ON l.Product = b.ID INNER JOIN Families AS f ON f.ID = l.Family GROUP BY f.ID ORDER BY b.Price ASC
I was hoping with this to get 1 row returned for each Family, which it works correctly both in the PHPMyAdmin query and also when print_r() the results.
I then store in:
$families[] = array('ID' => $f['ID'], 'Manufacturer' => $f['Name'], 'Family' => $f['Family'], 'URL' => $f['URL'], 'IMG' => $f['ImgURL'], 'Price' => $f['Price'], 'ScentCount' => 0);
Which also works correctly when doing a print_r() and when just looping through with a foreach loop echoing out the ID for each entry it returns 1234567 (all 7 Family IDs)
Then I run another query:
try{
$sqlCmd = "SELECT COUNT(*) FROM FamilyLookUp WHERE Family=:fID";
$s = $pdo->prepare($sqlCmd);
foreach($families as &$fam){
$s->bindValue(':fID', $fam['ID']);
$s->execute();
$fam['ScentCount'] = $s->fetchColumn();
}
}
This also gets the correct counts and properly stores them in the array for the number of items within each family. So all good up to now.
The problem occurs when I:
foreach($families as $fam):
?>
<div class="product-listing">
<?php echo $fam['ID']; ?>
<div class="product-listing-image">
<a href="<?php echo $fam['URL']; ?>"><img alt="" src="<?php echo $fam['IMG']; ?>"></a>
</div>
<div class="product-listing-details">
<a href="<?php echo $fam['URL']; ?>"><h3><?php echo strtoupper($fam['Manufacturer']); if($fam['Family'] != ""){ echo strtoupper(' - ' . $fam['Family']);} ?></h3></a>
<?php if($fam['ScentCount'] == 1): ?>
<span class="product-scent-count"><?php echo $fam['ScentCount']; ?> Scent</span>
<span class="product-price-value">£<?php echo $fam['Price']/100; ?></span>
<?php elseif($fam['ScentCount']>1): ?>
<span class="product-scent-count"><?php echo $fam['ScentCount']; ?> Scents</span>
<span class="product-price-value">From £<?php echo $fam['Price']/100; ?></span>
<?php endif;?>
</div>
</div>
<?php
endforeach;
?>
After doing this, it outputs correctly for the first 6 families of data, but for some reason it outputs a duplicate of the 6th instead of the actual 7th. When doing a print_r of all the data in the line before the start of the foreach loop, it returns all the correct data and yet within the foreach loop there becomes 1 duplicate array in the place of the 7th originally correct array.
Any advice would be awesome.
Edit for Kohloth's answer(the print_r followed directly by foreach vardump):
Array
(
[0] => Array
(
[ID] => 1
)
[1] => Array
(
[ID] => 7
)
[2] => Array
(
[ID] => 2
)
[3] => Array
(
[ID] => 3
)
[4] => Array
(
[ID] => 4
)
[5] => Array
(
[ID] => 6
)
[6] => Array
(
[ID] => 5
)
)
array(7) {
["ID"]=>
string(1) "1"
}
array(7) {
["ID"]=>
string(1) "7"
}
array(7) {
["ID"]=>
string(1) "2"
}
array(7) {
["ID"]=>
string(1) "3"
}
array(7) {
["ID"]=>
string(1) "4"
}
array(7) {
["ID"]=>
string(1) "6"
}
array(7) {
["ID"]=>
string(1) "6"
}