1

I have the following code that creates a revision timetable in the form of a multidimensional array. Each section represents a day and each hour a timeslot to revise in. Im trying to convert this multidimensional array into a table in mysql so that the timetable is user friendly and can be saved and so on. Here is the code used to generate the timetables and furthermore the the timetable generated.

<?php
if(!isset($_POST['subjects'])) {
    header('Location: index.php');
}
define('DAY_LENGTH', 12);
$timetable = [];
foreach ($_POST as $subject => $n) {  // add in the required amount of subjects
    $timetable = array_merge($timetable, array_fill(0, $n, $subject));
}
$timetable = array_merge($timetable, array_fill(0, DAY_LENGTH * 7 - count($timetable), ''));  // fill the array with empty values
shuffle($timetable);  // shuffle the set
$timetable = array_chunk($timetable, DAY_LENGTH);  // split it into 7 days

echo '<pre>';
var_dump($timetable);
echo '</pre>';

This is the array structure, it goes on for 7 sub arrays but i have only shown the first 2 to save time. Each sub array represents a day of the week ie [0] would be monday and each array within that is the time of the day followed by a subject i.e [8] = 8pm and physics

    array(7) {
  [0]=>
  array(12) {
    [0]=>
    string(0) ""
    [1]=>
    string(9) "Computing"
    [2]=>
    string(7) "Physics"
    [3]=>
    string(7) "Physics"
    [4]=>
    string(7) "Physics"
    [5]=>
    string(5) "Maths"
    [6]=>
    string(7) "Physics"
    [7]=>
    string(7) "Physics"
    [8]=>
    string(7) "Physics"
    [9]=>
    string(7) "Physics"
    [10]=>
    string(7) "Physics"
    [11]=>
    string(7) "Physics"
  }
  [1]=>
  array(12) {
    [0]=>
    string(7) "Physics"
    [1]=>
    string(9) "Computing"
    [2]=>
    string(7) "Physics"
    [3]=>
    string(7) "Physics"
    [4]=>
    string(7) "Physics"
    [5]=>
    string(7) "Physics"
    [6]=>
    string(5) "Maths"
    [7]=>
    string(7) "Physics"
    [8]=>
    string(7) "Physics"
    [9]=>
    string(7) "Physics"
    [10]=>
    string(7) "Physics"
    [11]=>
    string(5) "Maths"
  }
4
  • you need to show what you tried and how your table looks like Commented Jan 19, 2017 at 9:26
  • I really dont know where to start tbh, Im very new to sql and can do very simple functions but I dont know how i would handle this array Commented Jan 19, 2017 at 9:28
  • have you designed/created your database schema? Commented Jan 19, 2017 at 9:32
  • @RamRaider Not really i have a structure ready but it only consists of 2 tables, one that hold the users login system and one that stores the data for a users timetable(id/day/subject/timeslot) with id linking them. Its probably not in third normal form or correct at all tho Commented Jan 19, 2017 at 9:39

1 Answer 1

2

With a double loop, you can have access to the parameters that you want.

For example:

foreach ($timetable as $day => $subTimetable) {
    foreach ($subTimetable as $hour => $subject) {
        $sql = "INSERT INTO `table` (`day`, `hour`, `subject`) VALUES (:day, :hour, :subject);";
        $values = array(
            'day' => $day,
            'hour' => $hour,
            'subject' => $subject
        );
        // Execute insert here
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I Will try to implement this and see if it works , ty

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.