combat womble
combat womble

Reputation: 50

PHP undefined offset (not an error) try catch

I would like to list (upto) six of the remaining assignments due for the acedemic year onto a webpage. The data for the remaining assignments comes from a mysql db. When there are six or more assignments left, the webpage displays the info correctly. If there are less than six left, then i get a undefined offset error (and i understand why). Basically, i am trying to programmatically ignore the offset error.

I've tried looking and the following: try/catches in the

php. tried using (in the foreach loop) if($row['name'] == null){$row['name'] = "";} etc.

index.php

include('getdata.php');

$nextAssArray0 = getNextAssessments($courseInfoArray[0], 0);
$nextAssArray1 = getNextAssessments($courseInfoArray[0], 1);
$nextAssArray2 = getNextAssessments($courseInfoArray[0], 2);
$nextAssArray3 = getNextAssessments($courseInfoArray[0], 3);
$nextAssArray4 = getNextAssessments($courseInfoArray[0], 4);
$nextAssArray5 = getNextAssessments($courseInfoArray[0], 5);

<div class="col-sm-4">
<h3>You have <?php echo '' ?> assignments left</h3>
 <p>Assignment: <?php echo $nextAssArray0[0]; ?> is due on <?php echo $nextAssArray0[1]; ?></p>
 <p>Assignment: <?php echo $nextAssArray1[0]; ?> is due on <?php echo $nextAssArray1[1]; ?></p>
 <p>Assignment: <?php echo $nextAssArray2[0]; ?> is due on <?php echo $nextAssArray2[1]; ?></p>
 <p>Assignment: <?php echo $nextAssArray3[0]; ?> is due on <?php echo $nextAssArray3[1]; ?></p>
 <p>Assignment: <?php echo $nextAssArray4[0]; ?> is due on <?php echo $nextAssArray4[1]; ?></p>
 <p>Assignment: <?php echo $nextAssArray5[0]; ?> is due on <?php echo $nextAssArray5[1]; ?>
 </p>
</div>

getdata.php

function getNextAssessments($courseID, $offset)
{
try
{
    include('dbconn.php');
    $array = array();
    $stm = $conn->prepare("CALL getUpcomingAssignments(:courseID, :offset)");
    $stm->bindParam(':courseID', $courseID);
    $stm->bindParam(':offset', $offset);
    $stm->execute();
    foreach ($stm->fetchALL() as $row)
    {
        array_push($array, $row['name']);
        array_push($array, $row['due_date']);
        array_push($array, $row['tem']);
    }
}
catch (Exception $e)
{
    $array = array('','','');
}

return $array;

}

I am after the following functionality: 1) if there is only three assignments left - then only three

display. 2) if there is ten assignments left - then only six are displayed. 3) if there are no assignments left - then nothing is displayed in those

Upvotes: 0

Views: 339

Answers (1)

Patryk Cysarz
Patryk Cysarz

Reputation: 134

First of all, it is not a good idea to make SQL query for every database record. Remove limit from query, and put all your assesments in array. Then do something like this:

<h3>You have <?php echo count($assesments) ?> assignments left</h3>
<?php
foreach($assesments as $assesment){
 echo "<p>Assignment: {$assesment[0]} is due on {$assesment[1]} ?></p>"
}
?>

Also I would recommend changing your code so you have array like $assesment['dueDate'] instead of $assesment[1].

Upvotes: 1

Related Questions