如何在PHP中获取信息/不同的变量?
更新日期2015-04-12 我在PHP中遇到了一个问题,我不知道如何正确使用信息。代码如下:如何在PHP中获取信息/不同的变量?,php,mysql,Php,Mysql,更新日期2015-04-12 我在PHP中遇到了一个问题,我不知道如何正确使用信息。代码如下: $info = array(); $plus = 0; $item = mysql_query("SELECT * FROM items WHERE vardas='$ID' AND uzdetas='1' ORDER BY id"); while ($row = mysql_fetch_array($it
$info = array();
$plus = 0;
$item = mysql_query("SELECT * FROM items WHERE vardas='$ID' AND uzdetas='1' ORDER BY id");
while ($row = mysql_fetch_array($item)) {
$info[$plus] = mysql_fetch_array(mysql_query("SELECT * FROM daiktai WHERE id='".$row['daiktas']."'"));
$plus = $plus + 1;
}
$empty = "block.png";
if ($info[0]['tipass'] == 1) {
$helm = "".$info[0]['img']."";
} else {
$helm = "helm.png";
}
if ($info[1]['tipass'] == 9) {
$amulet = "".$info[1]['img']."";
} else {
$amulet = "amulet.png";
}
if ($info[2]['tipass'] == 8) {
$sword = "".$info[2]['img']."";
} else {
$sword = "sword.png";
}
if ($info[3]['tipass'] == 3) {
$platebody = "".$info[3]['img']."";
} else {
$platebody = "platebody.png";
}
if ($info[4]['tipass'] == 3) {
$shield = "".$info[4]['img']."";
} else {
$shield = "shield.png";
}
if ($info[5]['tipass'] == 7) {
$platelegs = "".$info[5]['img']."";
} else {
$platelegs = "platelegs.png";
}
if ($info[6]['tipass'] == 6) {
$gloves = "".$info[6]['img']."";
} else {
$gloves = "gloves.png";
}
if ($info[7]['tipass'] == 7) {
$boots = "".$info[7]['img']."";
} else {
$boots = "boots.png";
}
if ($info[8]['tipass'] == 1) {
$ring = "".$info[8]['img']."";
} else {
$ring = "ring.png";
}
echo "<div class='equipment'>
<img src='img/daiktai/".$helm."' alt='' width='30' class='head'>
<img src='img/daiktai/".$empty."' alt='' width='30' class='cape'>
<img src='img/daiktai/".$amulet."' alt='' width='30' class='amulet'>
<img src='img/daiktai/".$empty."' alt='' width='30' class='arrows'>
<img src='img/daiktai/".$sword."' alt='' width='30' class='sword'>
<img src='img/daiktai/".$platebody."' alt='' width='30' class='platebody'>
<img src='img/daiktai/".$shield."' alt='' width='30' class='shield'>
<img src='img/daiktai/".$platelegs."' alt='' width='30' class='platelegs'>
<img src='img/daiktai/".$gloves."' alt='' width='30' class='gloves'>
<img src='img/daiktai/".$boots."' alt='' width='30' class='boots'>
<img src='img/daiktai/".$ring."' alt='' width='30' class='ring'>
</div>";
如果类型为2,则变量也必须为:
if ($info[1]['tipass'] == 9) {
$amulet = "".$info[1]['img']."";
} else {
$amulet = "amulet.png";
}
因此,我想向他们展示:
<img src='img/daiktai/".$helm."' alt='' width='30' class='head'>
<img src='img/daiktai/".$amulet."' alt='' width='30' class='amulet'>
..
..
等等。问题是代码正确地显示了头盔和护身符,但人类也戴着剑、靴子等。请更正我的代码,谢谢 每次循环运行时都会覆盖
$information
变量。您需要将$information
设置为一个数组,并以不同的索引将值推入其中
$information = array();
$index = 0;
while ($row = mysql_fetch_array($query)) {
$information[$index] = mysql_fetch_assoc(mysql_query("SELECT * FROM items WHERE id='".$row['item_name']."'"));
// Increment our index every time the loop runs
$index = $index + 1;
}
您可以使用循环从该数组生成HTML图像列表
for($information as $image){
echo "<img src=$image['item_image']";
echo "<br>";
}
for($information as$image){
回声“;
}
我不这么认为,因为我得到了第一个图像类型信息。也许你有一个如何纠正它的建议?Stack的语法高亮显示不会说谎。另外,我知道它会抛出/导致解析错误。那么,也许你知道我的问题的决定?在打开PHP标记后立即在文件顶部添加错误报告,例如,谢谢你的帮助,但是现在如何显示信息呢?因为$information['name']
不起作用。@程序员您使用$information['name']
时没有列出索引。您需要调用$information[0]['name']
。显示数组中与索引关联的每个图像的更简洁的方法是运行循环。请看上面编辑的答案。真的是阵列..忘了。。缺乏知识。。非常感谢!:)更新了问题,请帮助。:)@程序员:您需要使用循环,以便显示所有指示值
for($information as $image){
echo "<img src=$image['item_image']";
echo "<br>";
}