我正在尝试创建一个可以填充店主库存的网页。我已经把商店里面的商品放在了数组里面,但我不能让php从数组中输出多个项目
<body>
<div id="container">
<form method="post" action="spell-shop.php">
tier 1 spells in shop<input type="text" name="tier1"><br>
tier 2 spells in shop<input type="text" name="tier2"><br>
tier 3 spells in shop<input type="text" name="tier3"><br>
Master spells in shop<input type="text" name="master"><br>
<input type="submit">
</form>
<?php
$spells1 = array(
'Commune - level 1 - Any' , 'Detect Magic - Level 1 - Any' , 'Devine Favor - Level 1 - Any' , 'Magic Missile - Level 1 - Any' , 'Prestidigation - Level 1 - Any' , 'True Strike - Level 1 - Any' , 'Shield - Level 2 - Any' , 'Locate Object - Level 2 - Any' , 'Prayer of Healing - level 2 - Any' , 'Alarm - Level 1 - Air' , 'Feather Fall - Level 1 - Air' , 'Jump - Level 1 - Air' , 'Longstrider - Level 1 - Air' , 'Shocking Grasp - Level 1 - Air' , 'Tensers Floating Disk - Level 2 - Air' , 'Thunderous Smite Lelvel 2 - Air' , 'Find Traps Level 2 - Air' , 'Gust of Wind Level - 2 - Air' , 'Knock - Level 2 - Air' , 'Levitate - Level 2 - Air' , 'Rope - Trick 2 - Air'
);
if (isset($_POST['tier1'])) {
$name = $_POST['tier1'];
$inventory = array_rand($spells1, $name);
echo "$inventory";
}
?>
</div>
</body>
答案 0 :(得分:-1)
你无法回应出那样的数组。对于array_rand,您提供了一个可供选择的数组,然后第二个参数是您希望随机抽取的项目数。
示例$inventory = array_rand($spells1,2)
;
将$ spells1中的2个元素放入$ inventory
所以现在你需要遍历$ inventory并回显其结果。
foreach($inventory as $item){
echo $item;
}
对于你想要的东西,你需要$ name / $ _POST ['tier1']为数字;