2014-01-06 33 views
0

检索它们例如,我有使用数组来存储信息和用PHP

<a href="FashionDetails.php?menu_id=1"></a>
<a href="FashionDetails.php?menu_id=2"></a>
<a href="FashionDetails.php?menu_id=3"></a>
<a href="FashionDetails.php?menu_id=4"></a>

和进程页: '$ fashion_id = $ _GET [ 'menu_id'];

if ($menu_id == "1") { 
    $id = "1"; 
    $menu_name ="Business Fashion"; 
    $img_file ="images/mushroomsoup.jpg"; 
    $description ="mushroom soup"; 
    $color ="red"; 
} 
elseif($menu_id =="2") { 
    $id ="2"; 
    $menu_name ="minestrone soup"; 
    $img_file ="images/mine.jpg"; 
    $description ="minestrone soup"; 
    $color ="blue"; 
} 
elseif($menu_id =="3") { 
    $id="3"; 
    $menu_name ="carrot soup"; 
    $img_file ="images/carrot.jpg"; 
    $description ="carrot soup."; 
    $color ="green"; 
} 
elseif ($menu_id =="4"){ 
    $id="4"; 
    $menu_name ="shark fin"; 
    $img_file ="images/sharkfin.jpg"; 
    $description ="sharkfin."; 
    $color ="gray"; 
} 



    echo "<h1 style='text-align: center'>Welcome to my restaurant!</h1>"; 
    echo "<font color=$color>You have selected $menu_name</font>. Menu ID($id)<br/>"; 
    echo "<img src='$img_file'></img><br/>"; 
    echo "$description"; 

    ?> 

' 比方说,我想用的,而不是使用if-else语句Array()函数,我该怎么办呢?我明白有一个更简单的方法通过使用SQL语句,但我还没有教过SQL,现在我只能使用PHP。 在此先感谢!

+0

你有没有听说过'病例'? – Peon

+0

没有办法使用数组()代替if。 – superphonic

+0

@superphonic肯定存在,如果if else将等于数值常量进行比较,则只需要在该数组槽中调用输出数据/动作/函数的数组即可。 – Dan

回答

3

这应该让你开始:

$menus = array(
    1 => array(
     'menu_name' => "Business Fashion", 
     'img_file' => "images/mushroomsoup.jpg", 
     'description' => "mushroom soup", 
     'color'  => "red" 
    ), 
    2 => array(
     'menu_name' => "minestrone soup", 
     'img_file' => "images/mine.jpg", 
     'description' => "minestrone soup", 
     'color'  => "blue" 
    ), 
); 

// Print out the menu name for menu ID #1 
echo $menus[$menu_id]['menu_name']; // Business Fashion 

基本上,你把你的菜单项在阵列(阵列),然后使用您从查询字符串获取菜单ID访问它们。

要把它放到一个表,你可以使用一个简单的循环:

foreach ($menus as $id => $menu) { 
?> 
    <tr> 
     <td><?php echo $menu['menu_name']; ?></td> 
     <td><?php echo $menu['img_file']; ?></td> 
     <td><?php echo $menu['description']; ?></td> 
     <td><?php echo $menu['color']; ?></td> 
    </tr> 
<?php 
} 

你显然需要改变这种以满足您的需求,但应该给你的想法。如果你需要菜单ID,你只需要输出$id

+0

这正是我所说的。 –

+1

虽然这是OP所要求的,但我仍然建议与[Amal Murali](http://stackoverflow.com/a/20952433/1488915)一起提出建议。 – Peon

+0

@BartFriederichs不幸的是,当你编写答案时,我无法阅读你的想法并编写它。 –

3

您可能正在寻找switch()构造。

从PHP手册文档:

switch语句类似于一系列IF同一表达的语句。在许多情况下,您可能想要将相同的变量(或表达式)与许多不同的值进行比较,并根据其值等于的值执行不同的代码段。这正是switch语句的目的。

所以,你的代码如下:

switch ($menu_id) { 
    case '1': 
     # code 
     break; 
    case '2': 
     #code 
     break; 

    ... 

    default: 
     # code... 
     break; 
} 
2

你可以做这样的事情:

$menu = array(
    "1"=>array(
     "id"=>1, 
     "menu_name"=>"shark fin"; 
     ... 
    ) 
    ... 
) 

然后使用$entry = $menu[$menu_id]得到的条目,$id = $entry["id"],等拿到部分。不要忘记确保$menu_id实际上是有效的。您可以轻松输入<a href="FashionDetails.php?menu_id=6"> </a>。 (奇怪的时尚,BTW。)

祝你好运学习PHP!