PHP HTML Dynamic Radio From Database
From w3cyberlearnings
Contents |
PHP Dynamic Radio Buttons From Database
Make HTML Radio Button from database.
Table: student
CREATE TABLE student ( student_id INT NOT NULL AUTO_INCREMENT, first_name VARCHAR(30) NOT NULL, last_name VARCHAR(30) NOT NULL, PRIMARY KEY(student_id) );
Records
INSERT INTO `student` (`student_id`,`first_name`,`last_name`) VALUES (1,'Johny','Zhar'); INSERT INTO `student` (`student_id`,`first_name`,`last_name`) VALUES (2,'Jacob','Milli'); INSERT INTO `student` (`student_id`,`first_name`,`last_name`) VALUES (3,'lili','lee'); INSERT INTO `student` (`student_id`,`first_name`,`last_name`) VALUES (4,'Michael','Vieva'); INSERT INTO `student` (`student_id`,`first_name`,`last_name`) VALUES (5,'Emma','lee'); INSERT INTO `student` (`student_id`,`first_name`,`last_name`) VALUES (6,'Peeou','Zhou'); INSERT INTO `student` (`student_id`,`first_name`,`last_name`) VALUES (7,'Isabel','Peom'); INSERT INTO `student` (`student_id`,`first_name`,`last_name`) VALUES (8,'Sophal','Tang'); INSERT INTO `student` (`student_id`,`first_name`,`last_name`) VALUES (9,'Chong','Mok'); INSERT INTO `student` (`student_id`,`first_name`,`last_name`) VALUES (10,'Ebo','Kaun'); INSERT INTO `student` (`student_id`,`first_name`,`last_name`) VALUES (11,'salina','lee'); INSERT INTO `student` (`student_id`,`first_name`,`last_name`) VALUES (12,'pheank','so'); INSERT INTO `student` (`student_id`,`first_name`,`last_name`) VALUES (13,'lyekwan','lee'); INSERT INTO `student` (`student_id`,`first_name`,`last_name`) VALUES (14,'kiloma','otam');
Example 1
<?php define('HOST', 'localhost'); define('USER', 'root'); define('PASS', 'yeething'); define('DBNAME', 'woowood'); $db = new mysqli(HOST, USER, PASS, DBNAME); if ($db->connect_errno) { echo "Failed to connect to MySQL: (" . $db->connect_errno . ") " . $db->connect_error; } else { $sql = "SELECT student_id, concat(first_name,' ',last_name) as name FROM student"; $result_db = $db->query($sql); if (!$result_db) { echo $db->error . ' Error perform query!'; } else { while ($row = $result_db->fetch_object()) { echo "<input type=\"radio\" name=\"student\" value=\"{$row->student_id}\" id=\"id{$row->student_id}\"/>"; echo "<label for=\"id{$row->student_id}\">$row->name</label><br/>"; } } } $db->close(); ?>
Output: HTML Source
<input type="radio" name="student" value="1" id="id1"/><label for="id1">Johny Zhar</label><br/> <input type="radio" name="student" value="2" id="id2"/><label for="id2">Jacob Milli</label><br/> <input type="radio" name="student" value="3" id="id3"/><label for="id3">lili lee</label><br/> <input type="radio" name="student" value="4" id="id4"/><label for="id4">Michael Vieva</label><br/> <input type="radio" name="student" value="5" id="id5"/><label for="id5">Emma lee</label><br/> <input type="radio" name="student" value="6" id="id6"/><label for="id6">Peeou Zhou</label><br/> <input type="radio" name="student" value="7" id="id7"/><label for="id7">Isabel Peom</label><br/> <input type="radio" name="student" value="8" id="id8"/><label for="id8">Sophal Tang</label><br/> <input type="radio" name="student" value="9" id="id9"/><label for="id9">Chong Mok</label><br/> <input type="radio" name="student" value="10" id="id10"/><label for="id10">Ebo Kaun</label><br/> <input type="radio" name="student" value="11" id="id11"/><label for="id11">salina lee</label><br/> <input type="radio" name="student" value="12" id="id12"/><label for="id12">pheank so</label><br/> <input type="radio" name="student" value="13" id="id13"/><label for="id13">lyekwan lee</label><br/> <input type="radio" name="student" value="14" id="id14"/><label for="id14">kiloma otam</label><br/>
Example 2: Use select function
- Use myradio() function.
<?php require_once 'myradio.php'; define('HOST', 'localhost'); define('USER', 'root'); define('PASS', 'yeething'); define('DBNAME', 'woowood'); $db = new mysqli(HOST, USER, PASS, DBNAME); if ($db->connect_errno) { echo "Failed to connect to MySQL: (" . $db->connect_errno . ") " . $db->connect_error; } else { $sql = "SELECT student_id, concat(first_name,' ',last_name) as name FROM student"; $result_db = $db->query($sql); if (!$result_db) { echo $db->error . ' Error perform query!'; } else { $aa_student = array(); while ($row = $result_db->fetch_object()) { $aa_student[$row->student_id] = $row->name; } myradio($aa_student, 10, 'STUDENT_RADIO', 0, 2); } } $db->close();
Output: HTML Source
<input type="radio" name="STUDENT_RADIO" value="1" id="id1"/> <label for="id1">Johny Zhar</label> <input type="radio" name="STUDENT_RADIO" value="2" id="id2"/> <label for="id2">Jacob Milli</label> <input type="radio" name="STUDENT_RADIO" value="3" id="id3"/> <label for="id3">lili lee</label> <input type="radio" name="STUDENT_RADIO" value="4" id="id4"/> <label for="id4">Michael Vieva</label> <input type="radio" name="STUDENT_RADIO" value="5" id="id5"/> <label for="id5">Emma lee</label> <input type="radio" name="STUDENT_RADIO" value="6" id="id6"/> <label for="id6">Peeou Zhou</label> <input type="radio" name="STUDENT_RADIO" value="7" id="id7"/> <label for="id7">Isabel Peom</label> <input type="radio" name="STUDENT_RADIO" value="8" id="id8"/> <label for="id8">Sophal Tang</label> <input type="radio" name="STUDENT_RADIO" value="9" id="id9"/> <label for="id9">Chong Mok</label> <input type="radio" name="STUDENT_RADIO" value="10" id="id10" checked="checked"/> <label for="id10">Ebo Kaun</label> <input type="radio" name="STUDENT_RADIO" value="11" id="id11"/> <label for="id11">salina lee</label> <input type="radio" name="STUDENT_RADIO" value="12" id="id12"/> <label for="id12">pheank so</label> <input type="radio" name="STUDENT_RADIO" value="13" id="id13"/> <label for="id13">lyekwan lee</label> <input type="radio" name="STUDENT_RADIO" value="14" id="id14"/> <label for="id14">kiloma otam</label>
Related Links
Dynamic HTML From Array
Dynamic HTML From Database
- Dynamic Drop Down List with Database
- Dynamic List from Database
- Dynamic Radio from Database
- Dynamic Checkbox from database
Dynamic HTML with Ajax
Others Related