Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

How do you insert data in the database using checkbox inside table in every table data using PHP?

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you insert data in the database using checkbox inside table in every table data using PHP?

Function to get the time range

function create_time_range($start, $end, $interval='30 mins', $format='12'){
$startTime=strtotime($start);
$endTime=strtotime($end);
$returnTimeFormat=($format=='24')?'g:i':'G:i';

$current=time();
$addTime=strtotime('+'.$interval,$current);
$diff=$addTime-$current;

$times=array();
while ($startTime<$endTime) {
$times[]=date($returnTimeFormat,$startTime);
$startTime+=$diff;
}
$times[]=date($returnTimeFormat,$startTime);
return $times;
}

Here is my code in table

<table class="table table-bordered table-hoverable table-striped" id="tblClassScheduler">
                   <thead>
                      <tr>
                        <?php
                         $days=array("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday");
                         for ($i=0;$i<COUNT($days);$i++) {
                          echo "<th>".$days[$i]."</th>";
                         }
                      ?>
                      </tr>
                   </thead>
                   <tbody>
                    <form method="POST">
                    <?php
                    $class_time_schedule=create_time_range("08:00","22:00", $interval='30 mins', $format='12');
                    foreach ($class_time_schedule as $time_schedule) {
                    echo "<tr>";
                    for ($i=0;$i<COUNT($days);$i++) { ?>
                    <td><?php echo $time_schedule; ?><br>
                    <input type="checkbox" name="class_time[]" value="<?php echo $time_schedule; ?> ">
                    <input type="text" name="class_day[]" value="<?php echo $days[$i]; ?>" hidden>
                    </td>
                   
                    <?php  }
                    echo "</tr>";
                    }
                    ?>
                    <button type="submit" class="btn btn-primary" name="save_schedule">Save</button>
                    </form>
                   </tbody>
               </table>

My PHP code to insert data in the database

$days=array("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday");
if (isset($_POST['save_schedule'])) {
$class_time=$_POST['class_time'];
$class_day=$_POST['class_day'];
for ($x=0;$x<COUNT($class_time);$x++) {
$class_time_sched=$class_time[$x];
$class_day_sched=$class_day[$x];
insertQuery("INSERT INTO acadsoc_class_schedule(username,class_time,class_day,date_created) VALUES (?,?,?,?)",
array($_SESSION['username'],$class_time_sched, $class_day_sched, date("Y-m-d")));
}
}

class_scheduler2.PNG
[Thumbnail for class_scheduler2.PNG]
 
I have a knack for fixing things like this ... um ... sorry ... here is a consilitory tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic