Link to home
Start Free TrialLog in
Avatar of justmelat
justmelat

asked on

How do I add this function to my php, codeigniter application

We have a small form php application, that feeds data into a larger application/database.
The larger database is an oracle db, so the 'date' that is sent has to be in oracle format
(we are getting a parse error when the date is sent to the field in the oracle db.)

The smaller form application is written in CodeIgniter which I am just learning.

I think I have found the section of code that bundles the data up in the smaller app and sends it
to the large oracle database.

What I think I understand this code is doing is: checks and counts the fields that are assigned to a field in the larger oracle db, then checks to see if it is a checkbox, radio button, dropdown, etc, if so, seperate the values and send them to the oracle db.

I want to add a check to see if one of the field being send is a DATE field and if so, change the value in that field to oracle formatted date and then send it - somthing like this>>[if ($fldType = FIELD_DATE_FIELD){get_oracle_date($value);}

How do I add this to the above code???? Thanks in advance.
if(count($AcctMgFlds) > 0)
{
foreach($AcctMgFlds as $fld)
{
$fldVal = "";	
//Get Option Text from multi option fields like checkbox, dropdown etc.				
if($fld->getOptionsFromAppFlag() != FIELD_OPTIONS_FROM_App && ($fld->getFieldTypeId() == FIELD_CHECK_BOX || $fld->getFieldTypeId() == FIELD_MULTI_SELECT || $fld->getFieldTypeId() == FIELD_DROP_DOWN || $fld->getFldTypeId() == FIELD_RADIO))
{																		
	$fldVal = $fldMgr->getOptionDisplayText($fld->getId(), str_replace("'","''",$req->getFldVal($fld->getId())));
}
else
{
	$fldVal = str_replace("'","''",$req->getFldVal($fld->getId())); 
}					
$TR[$field->getTRMapIdentifier()] = $fldVal;
}
}

Open in new window

Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Let's try this a bite at a time.

What is the required format for the Oracle DATETIME strings?
Avatar of justmelat
justmelat

ASKER

Hi Ray:

The format has to be yyyy-dd-mm - I think, but there is a built in function that will handle that.  I just need to send the value in that field to the function below

/*
 * format_date - formats date to given date format
 */
if ( ! function_exists('get_oracle_date'))
http://us3.php.net/manual-lookup.php?pattern=get_oracle_date
Where is the code for the get_oracle_date() function?  Is this part of your code?
Hi Ray

It is on one of the functions/controller pages in the codeIgniter/php, so it is set to go, I just need to call it correctly.  I can't figure out how to do this in this massive code.
Is it a stand-alone function or part of a class?  Can you please post the function code?
Hi Ray

It oracle date function is attached.
/*
 * format_date - formats date to given date format
 */
if ( ! function_exists('get_oracle_date'))
{
	function get_oracle_date($date)
	{
		$PARTS = explode('/', $date);
	
		if (count($PARTS) < 3)
		{
			return NULL;
		}
	
		switch ($PARTS[0])
		{
			case 01:
				$PARTS[0]='JAN';
				break;
			case 1:
				$PARTS[0]='JAN';
				break;
			case 02:
				$PARTS[0]='FEB';
				break;
			case 2:
				$PARTS[0]='FEB';
				break;
			case 03:
				$PARTS[0]='MAR';
				break;
			case 3:
				$PARTS[0]='MAR';
				break;
			case 04:
				$PARTS[0]='APR';
				break;
			case 4:
				$PARTS[0]='APR';
				break;
			case 05:
				$PARTS[0]='MAY';
				break;
			case 5:
				$PARTS[0]='MAY';
				break;
			case 06:
				$PARTS[0]='JUN';
				break;
			case 6:
				$PARTS[0]='JUN';
				break;
			case 07:
				$PARTS[0]='JUL';
				break;
			case 7:
				$PARTS[0]='JUL';
				break;
			case 08:
				$PARTS[0]='AUG';
				break;
			case 8:
				$PARTS[0]='AUG';
				break;
			case 09:
				$PARTS[0]='SEP';
				break;
			case 9:
				$PARTS[0]='SEP';
				break;
			case 10:
				$PARTS[0]='OCT';
				break;
			case 11:
				$PARTS[0]='NOV';
				break;
			case 12:
				$PARTS[0]='DEC';
				break;
			default:
				return NULL;
		}
	
		return $PARTS[1]."-".$PARTS[0]."-".substr($PARTS[2],-2);
	}

Open in new window

Oh and it is a part of the date help class
It never ceases to amaze me - people will write 80 lines of code to do the work of three!

But that aside, can you please post the top few lines of the date help class?

The general way of calling this is to use the class name :: method name($args)
<?php 
error_reporting(E_ALL);
echo "<pre>";
/*
 * format_date - formats date to given date format
 */
if ( ! function_exists('get_oracle_date'))
{
    function get_oracle_date($date)
    {
        $PARTS = explode('/', $date);

        if (count($PARTS) < 3)
        {
            return NULL;
        }

        switch ($PARTS[0])
        {
            case 01:
                $PARTS[0]='JAN';
                break;
            case 1:
                $PARTS[0]='JAN';
                break;
            case 02:
                $PARTS[0]='FEB';
                break;
            case 2:
                $PARTS[0]='FEB';
                break;
            case 03:
                $PARTS[0]='MAR';
                break;
            case 3:
                $PARTS[0]='MAR';
                break;
            case 04:
                $PARTS[0]='APR';
                break;
            case 4:
                $PARTS[0]='APR';
                break;
            case 05:
                $PARTS[0]='MAY';
                break;
            case 5:
                $PARTS[0]='MAY';
                break;
            case 06:
                $PARTS[0]='JUN';
                break;
            case 6:
                $PARTS[0]='JUN';
                break;
            case 07:
                $PARTS[0]='JUL';
                break;
            case 7:
                $PARTS[0]='JUL';
                break;
            case 08:
                $PARTS[0]='AUG';
                break;
            case 8:
                $PARTS[0]='AUG';
                break;
            case 09:
                $PARTS[0]='SEP';
                break;
            case 9:
                $PARTS[0]='SEP';
                break;
            case 10:
                $PARTS[0]='OCT';
                break;
            case 11:
                $PARTS[0]='NOV';
                break;
            case 12:
                $PARTS[0]='DEC';
                break;
            default:
                return NULL;
        }

        return $PARTS[1]."-".$PARTS[0]."-".substr($PARTS[2],-2);
    }
}


// TEST WITH AMBIGUOUS DATES
echo get_oracle_date('11/2/2009');
echo PHP_EOL;
echo get_oracle_date('5/3/2009');
echo PHP_EOL;




// THE CONDENSED VERSION OF THIS FUNCTION
function RAY_get_oracle_date($d)
{
    date_default_timezone_set('America/Chicago');
    if (!$t = strtotime($d)) return NULL;
    return strtoupper(date('j-M-y', $t));
}


// TEST WITH AMBIGUOUS DATES
echo RAY_get_oracle_date('11/2/2009');
echo PHP_EOL;
echo RAY_get_oracle_date('5/3/2009');
echo PHP_EOL;

Open in new window

Hi Ray

Sorry, it's been a bear of a week.  I have attached the entire date_help.php page.  does this help?

I love your shorter version for the date function, but since I am new to this application, I don't want to change anything just in case.  Knowing my luck, something else will break. LOL.
<?php  
/*
 *--------------------------------------------------------------------------
 * Date Helper Class 
 * @Use Page contains functions related to date  
 *--------------------------------------------------------------------------
 * @author progGroup
 * @created 08/24/2009
 * @version 1.0
 *--------------------------------------------------------------------------
 *
 */

//Code to reject direct script call
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/*
 * format_date - formats date to given date format
 */
if ( ! function_exists('format_date'))
{
	function format_date($d, $if='y-m-d', $of='m/d/y')
	{
		if ($if == 'y-m-d')
		{
			$DP=explode("-",$d);
			$y=$DP[0];
			$m=$DP[1];
			$d=$DP[2];
		}
	
		if ($if == 'm/d/y')
		{
			$DP=explode("/",$d);
			$y=$DP[2];
			$m=$DP[0];
			$d=$DP[1];
	
		}
		if (($y == '0000') || (empty($y)) )
		{
			return "";
		} else
		{
	
			$R=array($y,$m,$d);
			$F=array("/y/","/m/","/d/");
	
			return preg_replace($F,$R,$of);
		}
	}
}

/*
 * format_date - formats date to given date format
 */
if ( ! function_exists('get_oracle_date'))
{
	function get_oracle_date($date)
	{
		$PARTS = explode('/', $date);
	
		if (count($PARTS) < 3)
		{
			return NULL;
		}
	
		switch ($PARTS[0])
		{
			case 01:
				$PARTS[0]='JAN';
				break;
			case 1:
				$PARTS[0]='JAN';
				break;
			case 02:
				$PARTS[0]='FEB';
				break;
			case 2:
				$PARTS[0]='FEB';
				break;
			case 03:
				$PARTS[0]='MAR';
				break;
			case 3:
				$PARTS[0]='MAR';
				break;
			case 04:
				$PARTS[0]='APR';
				break;
			case 4:
				$PARTS[0]='APR';
				break;
			case 05:
				$PARTS[0]='MAY';
				break;
			case 5:
				$PARTS[0]='MAY';
				break;
			case 06:
				$PARTS[0]='JUN';
				break;
			case 6:
				$PARTS[0]='JUN';
				break;
			case 07:
				$PARTS[0]='JUL';
				break;
			case 7:
				$PARTS[0]='JUL';
				break;
			case 08:
				$PARTS[0]='AUG';
				break;
			case 8:
				$PARTS[0]='AUG';
				break;
			case 09:
				$PARTS[0]='SEP';
				break;
			case 9:
				$PARTS[0]='SEP';
				break;
			case 10:
				$PARTS[0]='OCT';
				break;
			case 11:
				$PARTS[0]='NOV';
				break;
			case 12:
				$PARTS[0]='DEC';
				break;
			default:
				return NULL;
		}
	
		return $PARTS[1]."-".$PARTS[0]."-".substr($PARTS[2],-2);
	}
}
?>

Open in new window

I read this from the original post:
(we are getting a parse error when the date is sent to the field in the oracle db.)

But the script posted above parses correctly.  

That said, I can only tell you that this is a technically incompetent piece of programming, and it can be replaced in its entirety with the code snippet, which handles the strings correctly.  This article might be helpful to you.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

Notwithstanding the comment on line 4, above, there is no class definition statement in the snippet.  So what question remains now?

// REQUIRED FOR PHP 5.1+
date_default_timezone_set('America/Chicago');

function RAY_format_date($d, $if='y-m-d', $of='m/d/y')
{
    if (!$t = strtotime($d)) return NULL;
    return date($of, $t);
}

function RAY_get_oracle_date($d)
{
    if (!$t = strtotime($d)) return NULL;
    return strtoupper(date('j-M-y', $t));
}

Open in new window

HI Ray


I want to add a check to see if one of the field being sent is a DATE field and if so, change the value in that field to oracle formatted date and then send it - somthing like this>>[if ($fldType = FIELD_DATE_FIELD){get_oracle_date($value);}
"if one of the field being sent is a DATE field and if so..."

And if not?  The "Date Helper Class" above would likely return a useless value or NULL.
Not sure about this.
Not sure about "this?"  

Please give us a little more to go on if you still need help, thanks.
Maybe the long way around, but I think i have my code working

For those who may want to try it the code is attached
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of modus_operandi
Starting auto-close process to implement the recommendations of the participating Expert(s).
 
modus_operandi
EE Admin