dongtanjian9310 2013-02-25 17:50
浏览 50

在PHP中接近多个类别和选择[关闭]

I just wanted to have some input on what the best solution - efficient would be for the problem that follows below.

For instance, say I have 6 different categories. Within these categories, you have 5 to 6 selections. Based on what you choose in each category, a different result is produced. Would you solve this by doing lots of if logic, arrays, or some other method?

My thoughts were to do switch statements or if logic. Is this the best way?

  • 写回答

1条回答 默认 最新

  • dra11767 2013-02-25 19:05
    关注

    If you did a layout such as this:

    switch($cat1){
      case 1:
        switch($cat2){
          case 1:
            ...
            break;
        }
        break;
      case 2:
        ...
        break;
    }
    

    or this:

    if($cat1 == 1){
      if($cat2 == 1){
        ...
      }elseif($cat2 == 2){
        ...
      }
    }elseif($cat1 == 2){
      ...
    }
    

    it would take you well over 100,000 lines of code to handle the logic either way. Yikes. But if you have 6 categories each with 6 selections, that gives you a total of 46,656 different combinations. 6^6. If your site is hooked up to a database you could do something like this to help you out.

    $layout = $cat1 . $cat2 . $cat3 . $cat4 . $cat5 . $cat6;
    //making $layout 315632 or whatever based on what they select.
    

    And in your database, you could create a table where the id matches up with $layout and you save your display information in rows in the database.

     -------------------------------------
    |   id   | name | make | year | model |
    |--------|------|------|------|-------|
    | 111111 | Aero |  S2  | 2011 | M-110 |
    | 111112 | .... | .... | .... | ..... |
    | 111113 | .... | .... | .... | ..... |
     -------------------------------------
    

    Then use a simple row lookup to get your results in php. Of course it would take quite a while to put all of your information into a database but its gonna take quite a while to create the logic in the php file anyways. This would be the fastest way as far as when the user is actually using your site, and it will make it easier for you to debug.

    If you aren't able to do this, the next fastest bet (if you can keep it all straight) is to have a 6 nested if/elseif statement. The most checks this would give you would be 36 but it could be less depending on what the user selects. A switch-case would also work but they're a little messier than an if/elseif statement.

    评论

报告相同问题?

悬赏问题

  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败