我目前正在学习一门非常基本的网络编程主题,但我遇到了完成项目的问题。
下面的代码显示了我创建的所有超链接,当我点击链接时,它会根据我想要显示的信息显示不同的信息(代码在hyerlink代码之后)。 我使用过if-else命令,但无论如何我可以使用Array来存储信息吗?我已经尝试过这么多,但我似乎没有为Array做正确的事。
此代码是超链接页面。
<body>
<h1 style="text-align: center">Welcome to School of IIT</h1>
<p>Please choose a diploma you are interested in:</p>
<ul>
<li><a href="dipListShow.php?dip_id=1">Diploma in IT</a></li>
<li><a href="dipListShow.php?dip_id=2">Diploma in Internet and Multimedia Design</a></li>
<li><a href="dipListShow.php?dip_id=3">Diploma in Mobile and Wireless</a></li>
<li><a href="dipListShow.php?dip_id=4">Diploma in Financial Business Informatics</a></li>
<li><a href="dipListShow.php?dip_id=5">Diploma in Cyber and Digital Security</a></li>
<li><a href="dipListShow.php?dip_id=6">Diploma in Gaming and Entertainment Technology</a></li>
</ul>
</body>
此代码是php页面:
<body>
<h1 style="text-align: center">Welcome to School of Informatics and IT School</h1>
<?php
$dip_id = $_GET['dip_id'];
if ($dip_id == "1")
{
$dipname = "Diploma in Information Technology";
$imgfile= "dip_it.jpg";
echo "<font color = red>You have selected $dipname</font></p>";
}
else if($dip_id == "2")
{
$dipname = "Diploma in Interactive Multimedia Informatics";
$imgfile= "dip_imd.jpg";
echo "<font color = green>You have selected $dipname</font></p>";
}
else if($dip_id == "3")
{
$dipname = "Diploma in Mobile and Wireless Technology";
$imgfile= "dip_mwc.jpg";
echo "<font color = blue>You have selected $dipname</font></p>";
}
else if($dip_id == "4")
{
$dipname = "Diploma in Financial Business Informatics";
$imgfile= "dip_fbi.jpg";
echo "<font color = purple>You have selected $dipname</font></p>";
}
else if($dip_id == "5")
{
$dipname = "Diploma in Cyber and Digital Security";
$imgfile= "dip_cds.jpg";
echo "<font color = orange>You have selected $dipname</font></p>";
}
else if($dip_id == "6")
{
$dipname = "Diploma in Gaming and Entertainment Technology";
$imgfile= "dip_get.jpg";
echo "<font color = grey>You have selected $dipname</font></p>";
}
echo "<img src='images/$imgfile'/>";
?>
</body>
请指导我如何在Array中完成此操作,而不是使用if-else以及如何在两个页面之间进行链接。谢谢:))
答案 0 :(得分:0)
以下代码仅在您的毕业证书是连续的时才有效。
$data = array (
array (
'dipName' => 'Diploma in Information Technology',
'img' => 'dip_it.jpg',
'color' => 'red'
),
array (
'dipName' => 'Diploma in Interactive Multimedia Informatics',
'img' => 'dip_imd.jpg',
'color' => 'green'
),
array (
'dipName' => 'Diploma in Mobile and Wireless Technology',
'img' => 'dip_mwc.jpg',
'color' => 'blue'
),
array (
'dipName' => 'Diploma in Interactive Multimedia Informatics',
'img' => 'dip_imd.jpg',
'color' => 'red'
)
);
echo '<font color = "'.$data[$dip_id]['color'].'">You have selected '.$data[$dip_id]['dipName'];
echo '<img src="images/'.$data[$dip_id]['img'].'"/>';
答案 1 :(得分:0)
有很多方法可以做到这一点。如果你想要一个简单的数组,只需要这样做:
$dip_ids = array(
'1' => array(
'dipname' => "Diploma in Information Technology",
'imgfile' => "dip_it.jpg",
'msg' =>"<font color = red>You have selected $dipname</font></p>"
),
'2' => array(
'dipname' => "Diploma in Interactive Multimedia Informatics",
'imgfile' => "dip_imd.jpg",
'msg' =>"<font color = green>You have selected $dipname</font></p>"
),
'3' => array(
'dipname' => "Diploma in Mobile and Wireless Technology",
'imgfile' => "dip_mwc.jpg",
'msg' =>"<font color = blue>You have selected $dipname</font></p>"
),
'4' => array(
'dipname' => "Diploma in Financial Business Informatics",
'imgfile' => "dip_fbi.jpg",
'msg' =>"<font color = purple>You have selected $dipname</font></p>"
),
'5' => array(
'dipname' => "Diploma in Cyber and Digital Security",
'imgfile' => "dip_cds.jpg",
'msg' =>"<font color = orange>You have selected $dipname</font></p>"
),
'6' => array(
'dipname' => "Diploma in Gaming and Entertainment Technology",
'imgfile' => "dip_get.jpg",
'msg' =>"<font color = grey>You have selected $dipname</font></p>",
)
);
然后只需访问$dip_ids[$dip_id]['dipname']
等数据
答案 2 :(得分:0)
您应该使用database.Store在表中存储此信息并通过其id检索它。 数据库结构应该是这样的:
table_name
-id
-tipname
-image
或使用Array(这不是好习惯):
现场演示: https://eval.in/84787
在相应的键中设置正确的值。我有插入测试值
<body>
<h1 style="text-align: center">Welcome to School of Informatics and IT School</h1>
<?php
$dip_id = isset($_GET['dip_id']) ? $_GET['dip_id']: '';
// set your $dip_id for testing purpose
//$dip_id = 3;
$arrData = array("1" => array( "dpname" =>"Diploma in Information Technology","image" =>"dip_it.jpg","color"=>"color1"),"2" => array( "dpname" =>"Diploma in Interactive Multimedia Informatics","image" =>"dip_imd.jpg","color"=>"color1"),"3" => array( "dpname" =>"Diploma in Mobile and Wireless Technology","image" =>"dip_it.jpg","color"=>"color1"),"4" => array( "dpname" =>"set value for 4","image" =>"set value for 4","color"=>"color1"),"5" => array( "dpname" =>"set value for 5","image" =>"set value for 5","color"=>"color5"),"6" => array( "dpname" =>"set value for 4","image" =>"set value for 4","color"=>"color6"));
if(isset($dip_id) || $dip_id){
echo "<font color = '{$arrData[$dip_id]['color']}'>You have selected {$arrData[$dip_id]['dpname']}</font></p>";
$img = $arrData[$dip_id]['image'];
echo "<img src='Assignment_1/$img'/>";
}
?>
</body>