Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically creating checkboxes

Tags:

html

sql

php

mysql

i am new to php.I want to dynamically create check boxes upon the result fetched from MySQL.If i have 10 records in employee Table so it must create 10 check boxes with employee name as value.I had seen several tutorials to make array of check boxes etc but could not fix the problem.Please anyone there to help!!!

like image 229
require_once Avatar asked Dec 05 '22 20:12

require_once


1 Answers

Try this out:

<?php 
    //Create the query
    $sql = "SELECT `name` FROM Employees";

    //Run the query
    $query_resource = mysql_query($sql);

    //Iterate over the results that you've gotten from the database (hopefully MySQL)
    while( $employee = mysql_fetch_assoc($query_resource) ):
?>

    <span><?php echo $employee['name']; ?></span>
    <input type="checkbox" name="employees[]" value="<?php echo $employee['name']; ?> /><br />

<?php endwhile; ?>

The example you see above relies on two things to actually function properly:

  1. You're using MySQL
  2. Your SQL-query must retrieve the employees' names (so that you can use them in the loop
like image 142
karllindmark Avatar answered Dec 07 '22 08:12

karllindmark