Jump to content


Photo

php/mysql math


  • Please log in to reply
7 replies to this topic

#1 quasiman

quasiman
  • Members
  • PipPipPip
  • Advanced Member
  • 194 posts
  • LocationPortland, Oregon

Posted 31 March 2006 - 11:14 PM

Hello,

I've found this script online that I'm trying to modify for my needs...still newbie at it though.
Basically it's supposed to take two values from the db table (AdmitDate and DischDate) and calculate the difference in days. If the dates are the same, make the value 1 day.

The error I'm getting is
Parse error: syntax error, unexpected T_ELSE in filename.php on line 13

<?php

// calculate difference and convert to days the absolute value; 1 day = 86400 s

$diff = abs($AdmitDate-$DischDate);
$diff = $diff/86400;

// remove decimals


    If ($AdmitDate == $dischDate);
        $diff = ceil($diff) + 1;
    else
       $diff = ceil($diff);

// Update records for LOS column in the database
$sql = "update PatientInfo set LOS=$diff";

// execute the select query
$open = execute_db($sql, $conn);

?>


#2 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 31 March 2006 - 11:38 PM

might be worth checking the posting rules/etc as their is a forum for third party scripts (ie, stuff you never wrote yourself).

but hey, it's friday and i'm in a good mood so here you are:

If ($AdmitDate == $dischDate);

the capital 'I' is not a problem, but best that it's all lower case (won't cause errors as far as i know, but i reckon it just looks rubbish). also - see the semi-colon at the end of that line? yup. you don't need it.

if ($AdmitDate == $dischDate)

hope that helps :)
"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!

#3 quasiman

quasiman
  • Members
  • PipPipPip
  • Advanced Member
  • 194 posts
  • LocationPortland, Oregon

Posted 31 March 2006 - 11:51 PM

AH HA!! Thank you!

Unfortunately it's not updating the days correctly, but at least I'm past the error!


Sorry it's in the wrong forum...

#4 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 01 April 2006 - 12:12 AM

$sql = "update PatientInfo set LOS=$diff";
Assuming you only want to update one record, your query will need a WHERE clause as well ..

$sql = "update PatientInfo set LOS= '$diff'  WHERE some_field = '$some_value' ";

Legend has it that reading the manual never killed anyone.
My site

#5 quasiman

quasiman
  • Members
  • PipPipPip
  • Advanced Member
  • 194 posts
  • LocationPortland, Oregon

Posted 01 April 2006 - 06:48 PM

Hi AndyB,

Actually I want it to update all records. I have the script included on the index page, so on every load it updates based on the other two fields.

#6 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 01 April 2006 - 11:03 PM


mysql_query ("UPDATE mytable SET diff = (TO_DAYS(AdmitDate) - TO_DAYS(DischDate) + 1)");

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#7 quasiman

quasiman
  • Members
  • PipPipPip
  • Advanced Member
  • 194 posts
  • LocationPortland, Oregon

Posted 03 April 2006 - 08:50 PM

The Dischdate field is a date, so when I create a record and leave it blank, it has an automatic value of 0000-00-00. I want the LOS (length of stay) field to account for this and never be less than 1:

if ($DischDate == 0000-00-00){
    mysql_query ("UPDATE mytable SET LOS = (TO_DAYS(AdmitDate) + 1)");
}
else {
    mysql_query ("UPDATE mytable SET LOS = (TO_DAYS(AdmitDate) - TO_DAYS(DischDate) + 1)");
}

This seems to work, but it creates records in the LOS as 732738 when there's only a 4 day difference. I'm assuming that's a date converted to a number? I have the LOS field set to VARCHAR.

#8 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 03 April 2006 - 11:37 PM

TO_DAYS converts a DATE type field to the number of days since 1970-01-01

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users