dongxiaoyan4388 2017-05-18 08:06
浏览 67
已采纳

如何在php中创建动态按钮并为数据库中的按钮设置值

I have created dynamic buttons in php but i want to fetch value from databse and set it to the buttons but it didnt fetch and set.give any solution for this. In databse there is 5 values and that value i want to set to the buttons

following code i have tried.

<?php

function dash()
{
    include 'config.php';
    $sql = "SELECT  roomno FROM roombook";
    if($result = mysqli_query($db, $sql)){
        if(mysqli_num_rows($result) > 0){

            while($row = mysqli_fetch_array($result)){
                $str='';
                $roomno=array($row['roomno']) ;
                // $aa=array($roomno);
                //echo "$arr";
                while(list($k,$v)=each($roomno)) {
                    $str.='<input type="submit" name="btn_'.$k.'" value="'.$v.'" id="btn_'.$k.'"/>';
                }
                return $str;
            }

            // Free result set
            mysqli_free_result($result);
        } else{
            echo "No records matching your query were found.";
        }
    } else{
        echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
    }

    // Close connection
    mysqli_close($db);
    //$btn=array(1=>'hjck',2=>'102',3=>'104');
}
?>
<!Doctype html>
<html>
<body>
<div id="bpanel" >
<?php echo dash();?>
</div>
</body>
</html>
  • 写回答

1条回答 默认 最新

  • 普通网友 2017-05-18 08:29
    关注

    As roonno is a comma delimited list an explode() is what you need to do here

    $sql = "SELECT  roomno FROM roombook";
    if($result = mysqli_query($db, $sql)){
        $str = '';
        while($row = mysqli_fetch_array($result)){
            // generate array from comma delimited list
            $rooms = explode(',', $row['roomno']);
    
            foreach ( $rooms as $k=>$v ) {
                $str .= '<input type="submit" name="btn_'.$k.'" value="'.$v.'" id="btn_'.$k.'"/>';
            }
        }
        return $str;
    } else {
        //echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
        // fixed? $link shoudl be $db
        echo "ERROR: Could not able to execute $sql. " . mysqli_error($db);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教