So my knowledge of php arrays is limited. I have read the PHP Arrays - Manual But it did not explain this in enough value for me to grasp it honestly.
I have the following in a if{} else{}
statement with this being the else{}
// Set up an array for the items
while($row = mysqli_fetch_array($result))
{
$source = $row['source'];
}
$items = array(
"id" => $id,
"source" => $source,
);
$_SESSION['items'] = $items;
Let's say I have the following items:
Item A with an ID of 1 and source of foo
Item B with an ID of 2 and source of boo
Item C with an ID of 3 and source of goo
if this function gets called with item A
, the array is created with an id of 1
and source of foo
and that is all that is placed in the array. And the array is dumped out like:
array(2) { ["id"]=> string(2) "25" ["source"]=> string(64) "https://www.alphahq.org/shop/views/assets/images/items/item2.jpg" }
Now what if the function gets called again for item B
right now as it sets, the array will changed to the variables for item B
Correct?
How would I be able to add item A
and item B
to the same array and define them as separate items.
So basically how could I do this:
array {
item A {
id => 1
source => foo
}
item B {
id => 2
source => boo
}
}
And just build the array as an item is added. I am saving the array in a session, could this be helpful in retrieving and adding to the array each time the function is called?
Just for additional help my full shopping-functions.php
file is included below for reference.
<?php
session_start();
require('../../config/database-connect.php');
// Start a new order for a customer
$action = $_GET['action'];
$id = $_GET['id'];
// First make sure the action is not blank
if ($action == '') {
echo 'Please select an action';
}
if ($action == 'add') {
// Check if the id matches one in the database
$result = mysqli_query($con,"SELECT id, source FROM items WHERE id='$id'");
if (mysqli_num_rows($result) == 0) {
echo 'That id is not valid!';
}
else {
// Set up an array for the items
while($row = mysqli_fetch_array($result))
{
$source = $row['source'];
}
$items = array(
"id" => $id,
"source" => $source,
);
var_dump($items);
$_SESSION['items'] = $items;
}
}
?>