[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 250
  • Last Modified:

Confusion about sequences and how php/mysql handles repeat regions

I am attempting a large php coding project with the hopes of learning more about php, but I'm running into a snag. I think I'm thinking about this the wrong way, and probably have my sequential order wrong in the code. I'm hoping you all can help me straighten this out in my head.

Here's what I want to happen:

A function (date_diff) compares two dates (one from a MySQL table and today's date).
Once it has the difference, I want to only show the table results if the amount of days ($ndays) is less than 15 (two weeks), for record #1.
The action then repeats itself, with record #2.

Currently, it's all or nothing. If one date_diff is less than 15, all entries show.

Sample output:
-----
new_datecar: 2009-07-20
ndays: 66
task: Task A

new_datecar: 2009-05-20
ndays: 5
task: Task K

new_datecar: 2009-10-20
ndays: 158
task: Task M
------

How do I handle a situation like this where I want a statement to only test one table row at a time, or is there a syntax problem that's causing this to happen? I've included my code below to, hopefully, make what I'm asking clearer.
<?php require_once('Connections/mhc.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
 
  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
 
  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}
 
mysql_select_db($database_mhc, $mhc);
$query_taskscar = "SELECT tasks_user_1547.task_id, tasks_user_1547.`update`, tasks_user_1547.show, tasks.cat, tasks.task_id, tasks.task, tasks.freq FROM tasks_user_1547, tasks WHERE tasks_user_1547.show = 'Y' AND tasks.task_id = tasks_user_1547.task_id AND tasks.cat = 'car'";
$taskscar = mysql_query($query_taskscar, $mhc) or die(mysql_error());
$row_taskscar = mysql_fetch_assoc($taskscar);
$totalRows_taskscar = mysql_num_rows($taskscar);
 
 
function date_diff($str_start, $str_end)
{
 
$str_start = strtotime($str_start); // The start date becomes a timestamp
$str_end = strtotime($str_end); // The end date becomes a timestamp
 
$nseconds = $str_end - $str_start; // Number of seconds between the two dates
$ndays = round($nseconds / 86400); // One day has 86400 seconds
$nseconds = $nseconds % 86400; // The remainder from the operation
$nhours = round($nseconds / 3600); // One hour has 3600 seconds
$nseconds = $nseconds % 3600;
$nminutes = round($nseconds / 60); // One minute has 60 seconds
$nseconds = $nseconds % 60;
echo "ndays: $ndays";
echo '<br>';
 
	
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
 
<body>
<?php do { ?>
  <?php
				
					//tests to see if the last change value is empty (therefore never set)
					if (isset($row_taskscar['update'])) {
				
						$thisday = date("Y-m-d");
						
						
						//sets the last change date to a variable $date
						$datecar = $row_taskscar['update']; 
 
						//format the date for mktime() function -- you need day, month, year
						$yearcar = substr($datecar, 0,4); 
						$monthcar = substr($datecar, 5, 2); 
						$daycar = substr($datecar, 8, 2); 
 
						//sets the frequency to a variable $freq
						$freqcar = $row_taskscar['freq'];
 
						//sets the variable to hold the new date. Calculates the new date by 
						//evaluating the lastchange date and adding the frequency amount measured in months
						$new_datecar = date("Y-m-d", mktime(0, 0, 0, $monthcar +$freqcar, $daycar , $yearcar)); 
 
						echo "new_datecar: $new_datecar"; 
						echo '<br>';
						date_diff("$thisday", "$new_datecar");
						
							global $ndays;
							if ($ndays <= 15) {
							echo 'task: ';
							echo $row_taskscar['task'];			  
							 }		
						echo '<br><br>';
						}
				   ?>
  <?php } while ($row_taskscar = mysql_fetch_assoc($taskscar)); ?>
</body>
</html>
<?php
mysql_free_result($taskscar);
?>

Open in new window

0
kentcommunications
Asked:
kentcommunications
  • 4
  • 4
2 Solutions
 
Ray PaseurCommented:
Let me try to paraphrase...

I want to show only the rows when the date in the row is less than two weeks from today?

In that case, you would use a SELECT statement to create a record set with all those rows, and only those rows.  So the PHP / MySQL would be something like this
$future_date = date('c', strtotime('+2 weeks'));
 
$sql = "SELECT fields FROM table WHERE row_date <= \"$future_date\"";

Open in new window

0
 
kentcommunicationsAuthor Commented:
Thanks for your reply. Unfortunately, this (much easier) way doesn't work with the way the app is set up.

Currently, the user can see a list of tasks they have available. When they complete a task, they click a button and it updates the database with the date it was completed. The current system displays all tasks, whether they are due or not.
I'm trying to send users an e-mail once a month giving them a list of tasks they have almost due. In the table column, update, it is storing the last time they completed it--not the next time it's due. The php code is processing when the item is due by adding the "update" date to the "frequency" (from another table) to decide whether it's due or not. Not all items have the same frequency.
0
 
Ray PaseurCommented:
I'm sure that I am not understanding the application design correctly.  But try adding this statement to the date_diff function after line 40

global $ndays;

Also, you might want to add this error_reporting() statement at the top of your scripts when you are debugging.  It would show you that the $ndays variable is undefined in the main script because it was not declared to be global in the function scope.

error_reporting(E_ALL);

HTH, ~Ray
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
kentcommunicationsAuthor Commented:
That fixed it :)

I do have a quick question about globals, just to be sure.
Do you only have to put "global" next to the original statement of the $var for it to become a global variable? As a test, I removed the "global" part from line 94 and the code still works.
0
 
Ray PaseurCommented:
Global variables are made "global" inside functions, then they are available to the main script outside of the function (but not inside other functions).  There are a couple of ways to look at this sort of thing.  One is called variable "scope"...
http://us2.php.net/variables.scope

Another way is to put the values into $GLOBALS.  Learn about the "superglobal" variables here:
http://us2.php.net/manual/en/reserved.variables.php

In object oriented programming you can explicitly declare variables to be public, private, protected, static, etc.  They are called "properties" in OOP.  More here:
http://us2.php.net/manual/en/language.oop5.visibility.php

HTH, ~Ray
0
 
kentcommunicationsAuthor Commented:
Great, thanks for all the help!
0
 
kentcommunicationsAuthor Commented:
Excellent help again, thanks :)
0
 
Ray PaseurCommented:
Thanks for the points - glad to be of assistance! ~Ray
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now