Jump to content

Math Array with Limit


neobolt
Go to solution Solved by Barand,

Recommended Posts

Hello,

I not to confident with arrays but I'm sure one could be used instead of all the code I've written below.
Any help or direction is appreciated.

My database records have unique id's.
Each id has a frequency in days in which they are due on a repeating basis.
I would like to limit the amount of due dates displayed. Currently I am showing 8.
Is is possible to limit the due dates shown based on a future date?
Example: Show tasks due within the next 100 days.
Some tasks with shorter frequencies(10 days) would show 10 due dates but tasks with say a 90 day frequency would only show one date.
 

Here is what I have come up with so far:

// Get all the data from the "mss" table
$result = mysql_query("SELECT * FROM mss ORDER BY id ASC") 
or die(mysql_error());

while($row = mysql_fetch_array($result)){
// Calculates when the task is next due
// If there is a last completion date then show next due date from this completion date.
$calc_date = $row['last_completed'];
//If there is no date for last_completion, then use the start_date to find next due date
if ($calc_date == '0000-00-00') {
$calc_date = $row['start_date'];
}

$due = date('F d, Y', strtotime($calc_date .' + '.$row['frequency'].' days'));
$due2 = date('F d, Y', strtotime($due .' + '.$row['frequency'].' days'));
$due3 = date('F d, Y', strtotime($due2 .' + '.$row['frequency'].' days'));
$due4 = date('F d, Y', strtotime($due3 .' + '.$row['frequency'].' days'));
$due5 = date('F d, Y', strtotime($due4 .' + '.$row['frequency'].' days'));
$due6 = date('F d, Y', strtotime($due5 .' + '.$row['frequency'].' days'));
$due7 = date('F d, Y', strtotime($due6 .' + '.$row['frequency'].' days'));
$due8 = date('F d, Y', strtotime($due7 .' + '.$row['frequency'].' days'));


$line = $row['id']. " - <strong>Next Due Dates: ". $due ."  |  ". $due2 ."  |  ". $due3 ."  |  ". $due4 ."  |  ". $due5 ."  |  ". $due6 ."  |  ". $due7 ."  |  ". $due8 ."</strong> <br/><br/>";
   echo "$line";
}
?>

Thanks
John

Link to comment
Share on other sites

  • Solution

This will list all due dates within 100 days of the last_completed date

$db = new mysqli(HOST,USERNAME,PASSWORD,'test');

$sql = "SELECT id, start_date, frequency, 
    IF(last_completed='0000-00-00', start_date, last_completed) as last_completed
    FROM mss
    ORDER BY start_date";
    
echo query2table($db, $sql);

$maxDates = 5;      // max no of dates to show

$res = $db->query($sql);
echo '<table border="" cellspacing="5">';
while (list($id, $sd, $freq, $lcd) = $res->fetch_row()) {
    $dt = new DateTime($lcd);
    $dt100 = new DateTime($lcd);
    $dt100->add(new DateInterval('P101D'));
    $di = new DateInterval("P{$freq}D");
    $dp = new DatePeriod($dt, $di, $dt100);
    echo "<tr><td>$id</td><td>$freq</td>";
    foreach($dp as $due) {
        echo '<td>' . $due->format('F d Y') . '</td>';
    }
    echo '</tr>';
}
echo '</table>';
Link to comment
Share on other sites

Thanks Barand,

I got your solution to work perfectly but I had to remove one line of code: echo query2table($db, $sql);

When I left this in the code I got an error: Call to undefined function.

Thanks again.

 

$db = new mysqli(HOST,USERNAME,PASSWORD,'test');

$sql = "SELECT id, start_date, frequency, 
    IF(last_completed='0000-00-00', start_date, last_completed) as last_completed
    FROM mss
    ORDER BY start_date";
    
echo query2table($db, $sql);

$maxDates = 5;      // max no of dates to show

$res = $db->query($sql);
echo '<table border="" cellspacing="5">';
while (list($id, $sd, $freq, $lcd) = $res->fetch_row()) {
    $dt = new DateTime($lcd);
    $dt100 = new DateTime($lcd);
    $dt100->add(new DateInterval('P101D'));
    $di = new DateInterval("P{$freq}D");
    $dp = new DatePeriod($dt, $di, $dt100);
    echo "<tr><td>$id</td><td>$freq</td>";
    foreach($dp as $due) {
        echo '<td>' . $due->format('F d Y') . '</td>';
    }
    echo '</tr>';
}
echo '</table>';
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.