Solved

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

Posted on 2010-11-11
18
675 Views
Last Modified: 2012-05-10
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

0
Comment
Question by:justmelat
  • 8
  • 8
18 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 34112961
Let's try this a bite at a time.

What is the required format for the Oracle DATETIME strings?
0
 
LVL 1

Author Comment

by:justmelat
ID: 34113992
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'))
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 34116305
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?
0
 
LVL 1

Author Comment

by:justmelat
ID: 34120416
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.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 34120497
Is it a stand-alone function or part of a class?  Can you please post the function code?
0
 
LVL 1

Author Comment

by:justmelat
ID: 34120968
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

0
 
LVL 1

Author Comment

by:justmelat
ID: 34121073
Oh and it is a part of the date help class
0
 
LVL 108

Expert Comment

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

0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 1

Author Comment

by:justmelat
ID: 34156387
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

0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 34158642
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.
http://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

0
 
LVL 1

Author Comment

by:justmelat
ID: 34167268
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);}
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 34169848
"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.
0
 
LVL 1

Author Comment

by:justmelat
ID: 34297054
Not sure about this.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 34301536
Not sure about "this?"  

Please give us a little more to go on if you still need help, thanks.
0
 
LVL 1

Author Comment

by:justmelat
ID: 34379552
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
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 34381679
I object to deleting this.  A correct answer was given in the tested code snippet, line 101, et seq, at ID:34121611.  Correct versions of format_date and get_oracle_date were given in the code snippet at ID:34158642.  

You wrote "...since I am new to this application, I don't want to change anything..." That is understandable, but that does not mean you have not gotten a good answer to your question.

You wrote "add a check to see if one of the field being sent is a DATE field" - this is what strtotime() does.  And it returns FALSE if the field is not a date.  Install the code snippet attached here and run it.  You will see that the function check_date_format() does not work correctly.  

All of this is clearly presented in the EE article I cited at ID:34158642.  If you read that article you will understand why we use the PHP built-in functions instead of trying to create our own!
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

Best regards, ~Ray
<?php // RAY_temp_justmelat.php

error_reporting(E_ALL);

date_default_timezone_set('America/Chicago');





// COPIED FROM THE "SOLUTION" AT EE

if ( ! function_exists('check_date_format'))

{

      function check_date_format($datevalue)

      {

            if(!preg_match("/[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{4}/",$datevalue))

            {

                  //print "Date format is invalid.";

                  return false;

            }

            else

            {

                  return true;

                  //return get_oracle_date($datevalue);

            }

      }

}





// THE RIGHT WAY TO TEST A DATE

function RAY_check_date_format($d)

{

    if (!strtotime($d))

    {

        echo "<br/>strtotime SAYS DATE STRING $d IS NOT VALID";

    }

    else

    {

        echo "<br/>strtotime SAYS DATE STRING $d IS VALID";

    }

}





// TESTING THE SOLUTION

$date = '99/0/0000';

$news = check_date_format($date);

if ($news) echo "<br/>check_valid_date() SAYS $date IS A VALID DATE";

RAY_check_date_format($date);



$date = '19/47/1947';

$news = check_date_format($date);

if ($news) echo "<br/>check_valid_date() SAYS $date IS A VALID DATE";

RAY_check_date_format($date);



$date = '15-SEP-72';

$news = check_date_format($date);

if ($news) echo "<br/>$date IS A VALID DATE";

if (!$news) echo "<br/>check_valid_date SAYS THE ORACLE DATE $date IS NOT VALID";

RAY_check_date_format($date);

Open in new window

0
 
LVL 1

Expert Comment

by:modus_operandi
ID: 34407513
Starting auto-close process to implement the recommendations of the participating Expert(s).
 
modus_operandi
EE Admin
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This video shows how to recover a database from a user managed backup
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

760 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

20 Experts available now in Live!

Get 1:1 Help Now