Solved

Confusion about sequences and how php/mysql handles repeat regions

Posted on 2009-05-15
8
234 Views
Last Modified: 2013-12-12
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
Comment
Question by:kentcommunications
  • 4
  • 4
8 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 24399296
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
 

Author Comment

by:kentcommunications
ID: 24412884
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
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 24412986
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
 

Author Comment

by:kentcommunications
ID: 24413537
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 500 total points
ID: 24415648
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
 

Author Comment

by:kentcommunications
ID: 24416065
Great, thanks for all the help!
0
 

Author Closing Comment

by:kentcommunications
ID: 31582089
Excellent help again, thanks :)
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 24416077
Thanks for the points - glad to be of assistance! ~Ray
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now