如何在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>
答
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);
}