0

I've been working on a website for a business that I own and I'm a bit rusty on my PHP and SQL. I was able to create a mock up shop / inventory array of what I want in PHP, but I am hung up on attempting to insert table data from a MySQL database into my PHP array. What I am trying to do is duplicate my mock up, however, rather than pulling from a defined array (s) in my inventoryArray.php, I'd like to pull data from a table I created in MySQL.

The mock up is as follows:

shop.php (current):

<?php foreach ($inventory as $handgun) {?>
    <div class="column">
        <h5><?php echo "$handgun[model]"?></h5>
        <img class="thumbnail"
            src="assets/style/images/inventory/pistols/<?php echo "$handgun[img]" ?>.png">
        <table class="shopTables">
            <tr>
                <th>MPN:</th>
                <td><?php echo "$handgun[mpn]"?></td>
            </tr>
            <tr>
                <th>UPC:</th>
                <td><?php echo "$handgun[upc]"?></td>
            </tr>
            <tr>
                <th>Accessories:</th>
                <td><?php echo "$handgun[accessories]"?></td>
            </tr>
            <tr>
                <th>Description:</th>
                <td><?php echo "$handgun[description]"?></td>
            </tr>
        </table>
        <a href="product-page.php" class="button expanded">View</a>
    </div>
<?php } ?>

inventoryArray.php (before):

    <?php
    $inventory = array(
         array(
             'action'        => "Striker-fired",
             'category'      => "Pistols",
             'cal_ga'        => "9mm",
             'manufacturer'  => "FN Herstal",
             'model'         => "FN 509 Midsize",
             'UPC'           => "845737010010",
             'img'           => "FN509M",
             'price'         => "$649"
         ),
         array(
             'action'        => "SA/DA",
             'category'      => "Pistols",
             'cal_ga'        => "9mm",
             'manufacturer'  => "CZ USA",
             'model'         => "CZ P01",
             'UPC'           => "806703911991",
             'img'           => "CZP01",
             'price'         => "$627"
         )
     );
    ?>

I did not change anything from shop.php, the following is my attempt at using data from my MySQL table in my inventoryArray.php file:

inventoryArray.php (after):

require_once ('../mysqli_connect.php');
$query = "SELECT * FROM firearms";

$response = @mysqli_query($dbc, $query);
while ($row= mysqli_fetch_array($response)) {
    $inventory = array(
        array(
            'model' => $row['model'],
            'img' => $row['img'],
            'mpn' => $row['mpn'],
            'upc' => $row['upc'],
            'accessories' => $row['accessories'],
            'description' => $row['description']
        )
    );
}

mysqli_close($dbc);

Though this inserts data from my table and into my array, inserting new inventory into my SQL table will not create a newly nested array similar to my mock-up. Instead, it seems to overwrite the original array.

1
  • 2
    $inventory = array( That overwrites $inventory every time. Commented Jan 23, 2019 at 20:44

2 Answers 2

4

Just fetch an associative array of the row into a new array and dynamically append to it:

while ($row = mysqli_fetch_assoc($response)) {
    $inventory[] = $row;
}

Really you can do just this:

while ($inventory[] = mysqli_fetch_assoc($response));

If there are more columns in the table and if you don't want them (doesn't matter) then select only the ones you want:

$query = "SELECT model, img, mpn, upc, accessories, description FROM firearms";
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Worked out perfectly and suits my need :)
1

It is overwriting, as you are defining the variable on each loop. Instead of

while ($row= mysqli_fetch_array($response)) {
    $inventory = array(
        array(
            'model' => $row['model'],
            'img' => $row['img'],
            'mpn' => $row['mpn'],
            'upc' => $row['upc'],
            'accessories' => $row['accessories'],
            'description' => $row['description']
        )
    );
}

Define $inventory before your loop, and then add to it using $inventory[]

$inventory = array();
while ($row= mysqli_fetch_array($response)) {
    $inventory[] =
        array(
            'model' => $row['model'],
            'img' => $row['img'],
            'mpn' => $row['mpn'],
            'upc' => $row['upc'],
            'accessories' => $row['accessories'],
            'description' => $row['description']
        );
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.