0

Im making a revision timetable generating program for a school project. I have tried to create a number of functions to do this using a 2d-array but I receive many errors and think its to do with the library function 'array_count_values' that I have used within a function checking if the subject is available for use.

here is the code that checks the subject.

function check_subject_availability($subjects, $timetable, $subject)
{
$count = 0;
foreach ($timetable as $day) {
    $count += array_count_values($day)[$subject];
}
if ($count < $subjects[$subject]) {
    return True;
} else {
    return false;
}
}

I think this is the root of the problem but here is the rest of my code that may be causing the issue

$subjects = $_POST;

function pick_random_subject($subjects, $timetable)
{
$available = FALSE;
while ($available == FALSE) {
    $subject = array_rand($subjects);
    if (check_subject_availability($subjects, $timetable, $subject)) {
        $available = TRUE;
    }
}
return $subject;
}



function check_subject_availability($subjects, $timetable, $subject)
{
$count = 0;
foreach ($timetable as $day) {
    $count += array_count_values($day)[$subject];
}

if ($count < $subjects[$subject]) {
    return True;
} else {
    return false;
}
}

function verify_available_slot($timetable, $day, $slot)
{
if ($timetable[$day][$slot] == null) {
    return true;
} else {
    return false;
}
}

function pick_random_slot($timetable)
{

$available = FALSE;
while ($available == FALSE) {
    $day = rand(0, 6);
    $hour = rand(0, 23);

    $available = verify_available_slot($timetable, $day, $hour);
}
return [$day, $hour];
}

function Check_end($subjects, $timetable)
{
$finished = FALSE;
foreach ($subjects as $subject) {
    if (!check_subject_availability($subjects, $timetable, $subject)) {
        $finished = TRUE;
        break;
    }
}
return $finished;
}

if (isset($_POST)) {
while (Check_end($subjects, $timetable == TRUE)) {

    $subject = pick_random_subject($subjects, $timetable);
    $slot = pick_random_slot($subject);
    $day = $slot[0];
    $hour = $slot[1];
    $timetable[$day][$hour] = $subject;
}
} else {
header('http://localhost/timetable/TimetableAlgorithmn.php');
}
var_dump($timetable)
?>
<pre>
<? print $timetable ?>
<pre>

The code is supposed to take the values of subjects given through post as a 2d-array,ie maths 2, physics 3. and assign the values maths and physics to the array until they have each been used the specified amount of times. The 'check_subject_availability' function is meant to see if the subject has been used the specified amount of times and return true of false.Sorry in advance for the badly formatted question and badly written code.

Here is a screenshot of the errors I encounter when the code is run: screenshot

The lines refer to these pieces of code

line 29

foreach ($timetable as $day) {

Line 30

$count += array_count_values($day)[$subject];

Line 33

if ($count < $subjects[$subject]) {

Line 42

if ($timetable[$day][$slot] == null) {
0

2 Answers 2

2

You probably want count() in line 30, but also note that if $day[$subject] is an array there is another syntax error there.

$count = count($day[$subject]);

Also, get rid of the warning on line 29 like this:

if (is_array($timetable)) {
  foreach ($timetable as $day) {
    if (!empty($day[$subject])) {
      $count += count($day[$subject]);
    }
  }
}
Sign up to request clarification or add additional context in comments.

5 Comments

It would probably be better to test if is_array rather than empty. Empty could return false for a non array value that would cause the foreach to still throw an error where as if is_array is true and the array is empty, the foreach will loop exactly zero times, which is harmless in this case.
@WEBjuju Thank you for this, it removed a few of the errors and now i am just getting undefined offset errors.
there you go - figure out why they are undefined, and either code around them being undefined or get them defined.
you may need to update your question with your latest code - or mark a solution here and then ask a new question
@WEBjuju ok, ill probably mark this one and ask a question on fixing the next error if needed thank you
2

while (Check_end($subjects, $timetable == TRUE)) {, you are comparing $timetable == TRUE as the second argument to the Check_end function meaning you are passing in a boolean, not an array. You likely want to move the closing parenthesis from the end of the line to after $timetable so you pass in timetable and compare the return value to true like while (Check_end($subjects, $timetable) == TRUE) {

Reading over the errors again, this seems like it would fix all the issues which stem from $timetable not being an array. You can't foreach loop over a true/false, you can't access Maths and it is a boolean so there is no offset 2 or 4, even if coerced to a string.

3 Comments

I have changed the code as you suggested but it only seemed to fix the error on line 29
Reading the code again. Without sample data for $subjects and $timetable to test with everything is just a guess. As for array_count_values, from reading I'm guessing that you are using it wrong. It is used for something like this, if you passed in an array like ['a', 'a', 'a', 'b', 'c'], that would return another array where each value from the original array is the key and a count of times that value exists in the array is the value. With that example you would get something like ['a'=>3,'b'=>1,'c'=>1]. $subject would need to be a value in the $day array and you want that count.
I was trying to use 'array_count_values' to check the number of times a chosen subject had been used and hence if that subject should be used again. $subjects is a 2D-array passed from a previous page that has the subjects the user selected along with a value. The function is meant to go through whatever subject is passed from the 'pick_random_subject' function within $subjects and check if its been used the correct amount of times. Sry for the lack of knowledge im very inexperienced. For example English 2 , and Maths 3 are set as the subjects through POST Then Pick_random picks one at random

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.