我有一个包含用户名的MYSQL表。我想将其放入数组中,并以一系列复选框的形式呈现,供用户选择。
下面的代码适用于表中的第一个值,如果我创建一个数组(不从MYSQL表中提取),则它将显示所有值。我想念什么?
功能:
function checkbox2( $name,$id,$label,$array_name = array() ) {?>
<div class="form-group">
<p><?php echo $label; ?></p>
<?php foreach ( $array_name as $value => $title ) : ?>
<label class="checkbox-inline" for="<?php echo $id; ?>">
<input type="checkbox" name="<?php echo $name; ?>[]" value="<?php echo $title; ?>" <?php isset($_SESSION[$name]) ? checked($title,$_SESSION[$name]) : ''; ?>>
<span class="checkbox-title"><?php echo $title; ?></span>
</label>
<?php endforeach; ?>
</div>
function array_name() {
// Connect to database
$conn = db_connect();
// Prepare and bind
$stmt = $conn->prepare("SELECT user_name FROM usernames");
// Execute and bind result;
$stmt->execute();
$response = $stmt->get_result();
$array_name = $response->fetch_array(MYSQLI_ASSOC);
return $array_name;
}
index.php
<?php $array_name = array_name(); ?>
<form action="pageprocessresults.php" method="post">
<? checkbox2( 'user_name','user_name','Select name',$array_name ); ?>
<? submit('Go To Step 2 »'); ?>