PHP 配列に関する私の知識は限られています。PHP Arrays - Manualを読みましたが、正直に理解するのに十分な価値のある説明ではありませんでした。
私はif{} else{}
声明で次のことを述べています。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;
次のアイテムがあるとします。
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
この関数が で呼び出されたitem A
場合、配列は の id1
とソースで作成さfoo
れ、配列に配置されるのはそれだけです。そして、配列は次のようにダンプされます。
array(2) { ["id"]=> string(2) "25" ["source"]=> string(64) "https://www.alphahq.org/shop/views/assets/images/items/item2.jpg" }
設定時に関数が再度呼び出された場合item B
、配列はitem B
正しい変数に変更されますか?
同じ配列に とを追加して、それらを個別の項目として定義するにはどうすればよいでしょうitem A
かitem B
?
では、基本的にどうすれば次のことができますか?
array {
item A {
id => 1
source => foo
}
item B {
id => 2
source => boo
}
}
アイテムが追加されると、配列を構築するだけです。セッションで配列を保存していますが、関数が呼び出されるたびに配列を取得して追加するのに役立ちますか?
追加のヘルプとして、参照用に私の完全なshopping-functions.php
ファイルを以下に示します。
<?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;
}
}
?>