Jump to content

is this a trivial problem?


bitbytebit

Recommended Posts

I am using a unix timestamp to store the date a row was added to a mysql db. The form asks the user to input a date, which I then strtotime and look for a record with that date. If they enter a date that isn't in the dbase I need to check backwards and forwards 2 days (unix timestamp+-86400). I just cannot figure it out, if I use a while it loops infinitely. I tried a for loop as well, pretty much didn't work either.Here is some code:

$connect = mysql_connect($hostname, $username, $password);  if (!$connect) die('Could not connect: '.mysql_error());  mysql_select_db($dbname, $connect);  $pastinvquery="SELECT * FROM TESTINV WHERE DateUpdated = '$fromdateunix'";  $curinvquery ="SELECT * FROM TESTINV WHERE DateUpdated = '$todateunix'";  $pastinvresult=mysql_query($pastinvquery);  $test=mysql_num_rows($pastinvquery);  echo "hey you ".$test." yes you!";  while (mysql_num_rows($pastinvquery)==0){	 $fromdateunix=$fromdateunix-86400;  $rowtest=mysql_fetch_array($pastinvresult);  $rowdate=$rowtest['DateUpdated'];  echo "<hr><hr><hr>from form ".$fromdateunix."<hr><hr><br>";  echo "from dbase ".$rowdate."<br>";  }

Thanks,

Link to comment
Share on other sites

what's the case for looking two days ahead/behind? If it's not in the database, whats the extra lookup for?but anyway, you could just use SQL to do the whole thing, lookup included. Just take the time stamp, add and subtract 86400 from it to get your forwards and backup, and then make a query out of it.

$bound = 86400;$backBound = $timestamp - $bound;$frontBound = $timestamp + $bound;$query = "SELECT * FROM table WHERE DateUpdated >= $backBound AND DateUpdated <= $frontBound";

then just loop through the results to see if any of those date's match the passed in one. Personally, I would just look to see if the date exists period. If not, then do whatever you need to tell the user it doesn't exist.

Link to comment
Share on other sites

what's the case for looking two days ahead/behind? If it's not in the database, whats the extra lookup for?
Thank you very much, the extra lookup is in case there is an entry a couple of days before or after the entered date.I was fixated on targeting a single row, I didn't think to return the whole set. Thanks you for the clarity and speed.-bit
Link to comment
Share on other sites

I assume that this is being done with a logged in user, and that you would be cross-referencing the forward/behing lookup with that, correct? So if their input date is off, you can make a logical guess which one is their by searching for the times around their input time, and where the userId == 'x'.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...