I'm trying to get MySQL results into an array for the purpose of creating the title for the related products. I've run into an issue with this particular code. I don't quite understand the error but I'm assuming it has to to with formatting and structure in the array.
foreach ($equipTitles as $equipTitle) {
$titles .= [$equipTitle['equipment_category_id']=>$equipTitle['equipment_name']];
//echo $titles;
}
If I substitute the code in question with an actual array, it works as expected.
$titles = ["1"=>"Audio",
"2"=>"Video",
"3"=>"Lighting",
"4"=>"Other"];
Code snippets
function getEquipCat() {
global $db;
$query = "SELECT equipment_category_id, equipment_name
FROM equip_category ORDER BY equipment_category_id";
//lets prep to execute
$statement = $db->prepare($query);
$statement->execute();
$equipCat = $statement->fetchAll(PDO::FETCH_ASSOC);
$statement->closeCursor();
return $equipCat;
}
.....
$equipTitles = getEquipCat();
.....
<?php
foreach ($equipTitles as $equipTitle) {
$titles .= [$equipTitle['equipment_category_id']=>$equipTitle['equipment_name']];
//echo $titles;
}
var_dump($titles);
?>
<?php foreach ($titles as $titleV => $title) :?>
<?php echo "<h4>$title</h4>";?>
<?php foreach ($allAudioEquipment[$titleV] as $audioEquipment) :?>
<div class="divTableBody">
<div class="divTableRow">
<div class="divTableCellEquipCode"><?php echo '<input type="hidden" id="'.$audioEquipment['equip_id'].'" name="equip_id" value="'.$audioEquipment['equip_id'].'">'.$audioEquipment['equip_id']; ?> </div>
<div class="divTableCellEquipDesc"><?php echo $audioEquipment['equip_name']; ?></div>
<div class="divTableCellEquipAdd"><input class="addToFlows" type="submit" value="+" /></div>
</div><!-- End Table Row -->
</div><!-- End Table Body -->
<?php endforeach; ?>
<?php endforeach; ?>
</div><!-- End Table -->
</div>
error: Undefined variable: titles & Notice: Array to string conversion