这是我第一次在这里发帖。我学习PHP并遇到了问题。我多次尝试解决它,但我无法弄清楚我做错了什么。这是我拥有的数组。现在我需要调用其中的每个产品
$productItems = array(
"hardware" => array(
"hardware1" => array(
"title" => "Graphics Card",
"price" => 11,
"blurb" => "Hardware Ipsum",
"img1" => "IMG9",
),
"hardware2" => array(
"title" => "Graphics Card",
"price" => 11,
"blurb" => "Hardware Ipsum",
"img1" => "IMG9",
),
"hardware3" => array(
"title" => "Graphics Card",
"price" => 11,
"blurb" => "Hardware Ipsum",
"img1" => "IMG9",
),
),
"software" => array(
"software1" => array(
"title" => "Office",
"price" => 11,
"blurb" => "Software Ipsum",
"img1" => "IMG9",
),
"software2" => array(
"title" => "Office",
"price" => 11,
"blurb" => "Software Ipsum",
"img1" => "IMG9",
),
"software3" => array(
"title" => "Office",
"price" => 11,
"blurb" => "Software Ipsum",
"img1" => "IMG9",
),
),
"peripherals" => array(
"peripheral1" => array(
"title" => "Gaming Mice",
"price" => 11,
"blurb" => "Peripheral Ipsum",
"img1" => "IMG9",
),
"peripheral2" => array(
"title" => "Gaming Mice",
"price" => 11,
"blurb" => "Peripheral Ipsum",
"img1" => "IMG9",
),
"peripheral3" => array(
"title" => "Gaming Mice",
"price" => 11,
"blurb" => "Peripheral Ipsum",
"img1" => "IMG9",
),
),
);
这是我以前在menu.php页面中调用整个列表的内容。到此为止一切正常。问题出现在以下代码中。
<?php
foreach ($productItems as $products => $items) {
echo ucwords($products).'<p style="margin-bottom: 0px;"> </p>';
foreach ($items as $product => $item) { ?>
<li><a href="product.php?item=<?php echo $item; ?>" style = "text-align: center">
<?php echo $item["title"]; ?></a> <sup>€</sup><?php echo $item["price"].'<br><br />';?></li>
<?php }} ?>"
并列出我使用此代码的每个产品的详细信息,但我有错误说未定义
if (isset($_GET['items'])) {
$productItem = strip_bad_chars($_GET['items']);
$products = $productItems[$productItem];
}
if (isset($_GET['item'])) {
$menuItem = strip_bad_chars($_GET['item']);
$product = $products[$menuItem];
}
<h1><img src="img1/<?php echo $product["img1"]; ?>.jpg" alt="<?php echo $product["title"];?>"> <br>
<?php echo $product["title"]; ?> <span class="price"><sup>€</sup><?php echo $product["price"]; ?></span></h1>
<p><?php echo $product["blurb"]; ?></p>
<br>
</div><!--product-->
谁能理解我做错了什么?谢谢。
答案 0 :(得分:0)
<强>更新强>
这可能是strip_bad_chars()
引入的问题。 id来自哪里?
这可能是一个不必要的功能,具体取决于提交的变量的来源。
我进行了以下测试:
$productItems = array("hardware" => array(
"hardware1" => array("title" => "Graphics Card","price" => 11,"blurb" => "Hardware Ipsum","img1" => "IMG9", ),
"hardware2" => array("title" => "Graphics Card","price" => 11,"blurb" => "Hardware Ipsum","img1" => "IMG9", ),
"hardware3" => array("title" => "Graphics Card","price" => 11,"blurb" => "Hardware Ipsum","img1" => "IMG9", )),
"software" => array("software1" => array(
"title" => "Office","price" => 11,"blurb" => "Software Ipsum","img1" => "IMG9",),
"software2" => array("title" => "Office","price" => 11,"blurb" => "Software Ipsum","img1" => "IMG9", ),
"software3" => array("title" => "Office","price" => 11,"blurb" => "Software Ipsum","img1" => "IMG9",), ),
"peripherals" => array("peripheral1" => array("title" => "Gaming Mice","price" => 11,"blurb" => "Peripheral Ipsum","img1" => "IMG9", ),
"peripheral2" => array("title" => "Gaming Mice","price" => 11,"blurb" => "Peripheral Ipsum","img1" => "IMG9", ),
"peripheral3" => array("title" => "Gaming Mice","price" => 11,"blurb" => "Peripheral Ipsum","img1" => "IMG9", )));
$products = $productItems['hardware'];
$product = $products['hardware1'];
echo $product["img1"] . "\n" . $product["title"] . "\n" . $product["title"]. "\n" . $product["price"]. "\n" .$product["blurb"];
IMG9
显卡
显卡
11
现在复制上面的代码并运行它。
然后添加strip_bad_chars()并再次运行。
$products = $productItems[strip_bad_chars('hardware')];
$product = $products[strip_bad_chars('hardware1')];
echo $product["img1"] . "\n" . $product["title"] . "\n" . $product["title"]. "\n" . $product["price"]. "\n" .$product["blurb"];
答案 1 :(得分:0)
可能存在概念错误:
您是否检查$ _GET [&#39; items&#39;]是否存在,但您从未向其传递值。
在您创建链接的代码部分中,您还需要传递项目ID
<a href="product.php?item=<?php echo $item; ?>&items=<?=$products?>"
你可以改进的是逻辑检查部分:
if (isset($_GET['item'])) {
$menuItem = strip_bad_chars($_GET['item']);
$product = $products[$menuItem];
}
这部分代码只有在$ _GET [&#39; items&#39;] isset
时才能执行因此该部分的代码应该是这样的:
$product = null;
if (isset($_GET['items'] && isset($_GET['item'])) {
$productItem = strip_bad_chars($_GET['items']);
$menuItem = strip_bad_chars($_GET['item']);
$product = $productItems[$productItem][$menuItem];
}
答案 2 :(得分:0)
首先检查您在$_GET['items']
发送的内容,然后在清理strip_bad_chars($_GET['items']);
在您的foreach的每个周期中交替打印,以查看您在阵列中存储的内容。
编辑:
这个$ _GET错了
if (isset($_GET['items'])) {
$productItem = strip_bad_chars($_GET['items']);
$products = $productItems[$productItem];
}
你正在打电话&#34;项目&#34;当你定义&#34; item&#34;没有&#34; s&#34;