douqian1975 2013-08-01 13:25
浏览 66

如何动态选择复选框

I know this is a basic question that I should know the answer to, but i have never had to even consider it before and I could not find anything through google.

I want to automatically select or leave unselected checkboxes based on information in the database.

for example

I have two pairs of checkboxes each with a mens shoes and a womens shoes option. The user is able to select if they want only mens shoes or only womens shoes or both and the check boxes will be checked accordingly. Then there is another set for jeans which offer the same options.

In the database I have a few columns

id | foreignKeyId | Shoes | Jeans

The id column is auto incremented, the foreignKeyId is populated with the relevant foreignKey, shoes and jeans have been populated with an ENUM '0', '1', '2', '3'

0 = no selection

1 = mens selected

2 = womens selected

3 = both selected

I have then pulled the data from the database but I cant work out how to apply the information. I am assuming that I need to redesign my database.

What would be the best way to store and apply this information?

  • 写回答

2条回答 默认 最新

  • doumu8911 2013-08-01 13:36
    关注

    If you want to keep this structure in the db then:

    after querying your db get into a variable the Jeans column's value.

    $jeans = $row["Jeans"];
    

    and then find out what you have to display to the user

    $mens='';
    $womens= '';
    
    switch($jeans){
    case "1": $mens=' checked="checked"'; break;
    case "2": $womens=' checked="checked"'; break;
    case "3": $mens=' checked="checked"'; $womens=' checked="checked"'; break;
    }
    
    echo "<input type='checkbox' name='mens' ".$mens." /> Mens";
    echo "<input type='checkbox' name='womens' ".$womens." /> Womens";
    
    评论

报告相同问题?

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序