I have the following table in my database:
CREATE TABLE subjects (
subject_id int(11) NOT NULL AUTO_INCREMENT,
subject text,
PRIMARY KEY (subject_id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;
In the table I have already entered some data (subject names).
What I'm trying to do is that for each subject in the table, a checkbox is created with the name of the subject next to it.
So far I have managed to create checkboxes for each subject in the table, but I can not get the name of the subject shown next to the checkbox. Does anyone know how to do it?
I'm doing it this way:
<?php
$sql = "SELECT subject FROM subjects"; /*Select from table name: subjects*/
$result = $conn->query($sql); /*Check connection*/
if($result)
{
foreach($result as $row)
{
echo "<input type='checkbox' name='data[]' value='" . htmlspecialchars($row['subject']) . "' /> <label>Here goes the subject name</label>";
}
}
?>
This is how my table looks like:
And this is what I'm getting in PHP:
Pretty easy actually, just concatenate the $row['subject']
variable in between the label tags:
foreach($result as $row)
{
echo "<input type='checkbox' name='data[]' value='" . htmlspecialchars($row['subject']) . "' /> <label>" .$row['subject'] . "</label>";
}
You can use variables more than once in any output.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.