-2

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

0

2 Answers 2

1

.= is not how you add to an array, it's used for appending to a string.

Use

foreach ($equipTitles as $equipTitle) {
    $titles[$equipTitle['equipment_category_id']] = $equipTitle['equipment_name'];
}
Sign up to request clarification or add additional context in comments.

5 Comments

I was going by the example $titles array, not the output code.
That's what he showed: 1 => Audio, 2 => Video, ... 1 and 2 are the category IDs.
I get it now, the hardcoded $titles array was a test variable (not to be used in the final code).
It's the variable that the loop is assigning to, so it was showing what the final result should look like.
@Barmar Thank you for your help. This cleared up my issue.
0

I don't see any reason to re-store your result set as a second/redundant variable. Just use the result set data in your loop.

This would allow you to completely skip the foreach ($equipTitles as $equipTitle) {...} portion of your script -- which is a good thing.

Your code can look something like this:

<?php
function getEquipCat($db)  {
    return $db->query("SELECT equipment_category_id,
                              equipment_name  
                       FROM equip_category
                       ORDER BY equipment_category_id")
              ->fetchAll(PDO::FETCH_ASSOC);
}

foreach (getEquipCat($db) as $cat_row) {
    echo "<h4>{$cat_row['equipment_name']}</h4>";
    foreach ($allAudioEquipment[$cat_row['equipment_category_id']] as $audioEquipment) {
        // ...
    }
}

Notice that I am not using a prepared statement to query the database because there are no variables to bind in your query. If your project is using variables in the query, then keep the prepared statement syntax.

1 Comment

Thank you @mickmackusa I'm new to php and MySQL. I appreciate you showing me how to write code more efficient.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.