douzhuo2002 2017-05-17 17:22
浏览 39
已采纳

CUSTOM检查输入类型按钮是否已选中PHP

I have a little problem with one of my script:

    <label><input type="radio" name="tshirt-size" value="giraffe" checked="checked" ><img src="./images/giraffe.png"></label>
    <label><input type="radio" name="tshirt-size" value="bear"><img src="./images/bear.png"></label>
    <label><input type="radio" name="tshirt-size" value="monkey"><img src="./images/monkey.png"></label>

and the selected code:

      <?php
            $sql="SELECT distinct size FROM total where left_quantity > 0 order by ID"; 
            echo "<select name='size'>"; 
            echo "<option disabled>SELECT SIZE</option>"; 
            foreach ($dbo->query($sql) as $row){
                echo "<option value = $row[size]> $row[size]</option>"; 
            }
            echo "</select>";
    ?>

table database:

CREATE TABLE stocuri_tricouri ( id int(100) NOT NULL, type varchar(255) NOT NULL, size varchar(255) NOT NULL, left_quantity int(100) NOT NULL, stoc_ramas int(100) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `stocuri_tricouri` (`id`, `type`, `size`, `initial_quantity`, `left_quantity`) VALUES (1, 'giraffe', 'S', 250, 250), (2, 'giraffe', 'M', 250, 250), (3, 'giraffe', 'L', 250, 250), (4, 'bear', 'S', 250, 250), (5, 'bear', 'M', 150, 0), (6, 'bear', 'L', 250, 250), (7, 'monkey', 'S', 250, 250), (8, 'monkey', 'M', 250, 250), (9, 'monkey', 'L', 250, 250);

I want to select the size for each radio button, not for all, if i delete the distinct the select will be with all data... and i want date from each input.

with distinct: size are: S, M, L

without distinct: S,M,L,S,M,L,S,M,L

with distinct

without distinct

  • 写回答

1条回答 默认 最新

  • dqf2015 2017-05-17 17:31
    关注

    I'm using this code, customize it as per your requirements.

        for ($i=0;$i <count($result);$i++) {
            $htmltext = $result[$i]['firstname'].' '.$result[$i]['lastname'];
            $id = $result[$i]['id'];
            $IsSelected = ($id==$selected ? 'selected' : '');
            ?>
            <option value="<?=$id?>" <?=$IsSelected?>><?=$htmltext?></option>
            <?php
        }
    

    What I've done here is added condition for selected option value if it match then print selected as attribute of option. $selected is ID which you need to select, it could be anything.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图