select mysql columns with checkbox and display data in html table,php
<p>I have 5 months working experience in php and mysql. My question is how do I select some columns in my mysql database table with checkbox and display data of those selected columns in an html table in php?
Please see my code below and help me solve this.</p>

<p>The checkbox codes:</p>

<pre><code>&lt;form role="form" id="viewallteachers" method="POST" autocomplete="on" action=""&gt;
                    &lt;h4&gt;Kindly check the items to view in the table. You have a maximum of 10 items to view at a time in the table.&lt;/h4&gt;
                    &lt;hr&gt;
                    &lt;div class="row"&gt;
                        &lt;div class="col-sm-4"&gt;
                            &lt;div class="checkbox"&gt;
                                &lt;label&gt;
                                    &lt;input type="checkbox" name="check_list[]" value="tpicture"&gt;Picture
                                &lt;/label&gt;
                            &lt;/div&gt;
                            &lt;div class="checkbox"&gt;
                                &lt;label&gt;
                                    &lt;input type="checkbox" name="check_list[]" value="teacherID"&gt;Teacher ID
                                &lt;/label&gt;
                            &lt;/div&gt;
                            &lt;div class="checkbox"&gt;
                                &lt;label&gt;
                                    &lt;input type="checkbox" name="check_list[]" value="staffID"&gt;
                                    Staff ID
                                &lt;/label&gt;
                            &lt;/div&gt;
&lt;button type="button" class="btn btn-default" data-dismiss="modal"&gt;Close&lt;/button&gt;
                    &lt;button type="submit" name="btnshowcolumns" class="btn btn-primary"&gt;Save changes&lt;/button&gt;
                    &lt;/form&gt;
</code></pre>

<p>Now below is my code for table to display the selected columns and data per the columns selected.</p>

<pre><code>&lt;?php  if(!empty($_POST['check_list'])) {
 $check_list = $_POST['check_list']; 
 $counter = count($check_list); 
 for($x=0; $x&lt;=$counter; $x++){ 
   if(!empty($check_list[$x])){ 
    $sql = "SELECT `".$check_list[$x]."` FROM teachers";
$db = new PDO('mysql:host=localhost:3306;dbname=gnoc_db', 'root', 'xxxx');
       $select = $db-&gt;prepare($sql);
       $select-&gt;execute();
       while($row = $select-&gt;fetch(PDO::FETCH_ASSOC)) {
       echo '&lt;tr&gt;';
       foreach ($row as $key=&gt;$value) {
            echo '&lt;td&gt;'.$key . ' = ' . $value .'&lt;/td&gt;';
               }
               echo '&lt;/tr&gt;';
                } /* END OF IF */
                 } /* END OF FOR LOOP */
        }
     }
?&gt;
&lt;/tbody&gt;
&lt;/table&gt;
</code></pre>

<p>Below are the images:
<a href="http://i.stack.imgur.com/wRkRP.png" rel="nofollow">image of checkbox selecting the database columns</a>
<a href="http://i.stack.imgur.com/5I6fd.png" rel="nofollow">image of table show data</a></p>