Jump to content

problem with include 'function'


amosse

Recommended Posts

Hello to everybody

 

I would like to ask your advice about the following problem:

 

I have the following script in order to get the date of : today minus 2 months, 1st day of the month (it meens 01/08/2007)

date_minus2m_1std.php

<?php
function date_minus2m_1std()
{
$date_minus2m_1std = mktime (0,0,0,date("m")-2,date("d"),date("Y"));
$data_minus2m_1std = date('Y-m-01',$date_minus2m_1std);
return $data_minus2m_1std;
}
?>

 

I have the following script in order to get the balance (running sum)

start_balance.php

<?php
function start_balance($dipu_p,$diba_p,$data_last_bal)
{
require 'config.php';
require 'connect.php';
$query = "select (fin_record_debit-fin_record_credit) as fin_record_bal1 from fin_records where (fin_record_date<='$data_last_bal') and (id_production_unit='$dipu_p') and (id_bank_account='$diba_p')";
$result = mysql_query($query);
if (!$result) {
die("Errore nella query $query: " . mysql_error());
}
while ($row = mysql_fetch_assoc($result)) 
{
$fin_record_bal2	+=$row[fin_record_bal1];
}
return $fin_record_bal2;
}
?>

 

With the above functions I have to calculate the starting balance at the date of date_minus2m_1std() minus  1 day with the following script:

trial_balance2.php

<?php
require '../date_minus2m_1std.php';
$data_last_bal = date('Y-m-d',strtotime(date_minus2m_1std())-1);
$dipu_p=2;
$diba_p=12;
require '../start_balance.php';
echo start_balance($dipu_p,$diba_p,$data_last_bal);
?>

 

If I try to run trial_balance2.php I get any result,

I tried to put one echo (before require '../start_balance.php';) in order to see $data_last_bal value, it is equal to '2007-07-31' it meens it is OK.

 

But if I try the following script, everything is working perfectly

trial_balance.php

<?php
require '../start_balance.php';
$dipu_p=2;
$diba_p=12;
$data_last_bal='2007-10-1';
echo start_balance($dipu_p,$diba_p,$data_last_bal);
?>

I get the balance (running sum at the date of '2007-10-1')

 

The different behaviours between trial_balance.php and trial_balance2.php it is rather strange.

 

So I decide to do further check putting some echo inside start_balance() in this way:

start_balance.php

<?php
function start_balance($dipu_p,$diba_p,$data_last_bal)
{
require 'config.php';
require 'connect.php';
$query = "select (fin_record_debit-fin_record_credit) as fin_record_bal1 from fin_records where (fin_record_date<='$data_last_bal') and (id_production_unit='$dipu_p') and (id_bank_account='$diba_p')";
$result = mysql_query($query);
echo '<br>';
echo 'start_balance()-$data_last_bal='.$data_last_bal;
echo '<br>';
echo 'start_balance()-$dipu_p='.$dipu_p;
echo '<br>';
echo 'start_balance()-$diba_p='.$diba_p;
echo '<br>';
if (!$result) {
die("Errore nella query $query: " . mysql_error());
}
while ($row = mysql_fetch_assoc($result)) 
{
$fin_record_bal2	+=$row[fin_record_bal1];
echo 'start_balance()-while-$row[fin_record_bal1]='.$row[fin_record_bal1];
echo 'start_balance()-while-$fin_record_bal2='.$fin_record_bal2;
echo '<br>';
}
return $fin_record_bal2;
}
?>

 

Now if I run trial_balance.php I get:

start_balance()-$data_last_bal=2007-10-1

start_balance()-$dipu_p=2

start_balance()-$diba_p=12

start_balance()-while-$row[fin_record_bal1]=-123.00start_balance()-while-$fin_record_bal2=-123

start_balance()-while-$row[fin_record_bal1]=-100.00start_balance()-while-$fin_record_bal2=-223

...............................etc.

till to get final balance (return $fin_record_bal2;) of -1002439.91

everything OK.

 

If I run trial_balance2.php I get:

start_balance()-$data_last_bal=2007-07-31

start_balance()-$dipu_p=2

start_balance()-$diba_p=12

I don't get to seen inside the while loop, why this strange behaviours between trial_balance.php and trail_balance2.php?

 

I tried to change inside start_balance.php scrpt if(!$result) with if($result) as well, subsequently I run trial_balance.php and trial_balance2.php in both I get 'Errore nella query...'

so it meens that $result = true, this is rather strange!

 

I hope someone could give me some advice about the problem.

Many thanks for your attention.

Amos

 

 

 

 

 

 

 

 

 

 

Link to comment
Share on other sites

Many thanks for your reply.

 

I changed your code in date_minus2m_1std.php and I put one echo at the end like this:

 

date_minus2m_1std2.php

<?php
function date_minus2m_1std()
{
$data_minus2m_1std = date('Y-m-01',strtotime("-2 months", date("Y")."-".date("m")."-".date("d")));
[b]return $data_minus2m_1std;[/b]
}
echo date_minus2m_1std();
?>

but if I run this script I get

'1969-11-01' instead of '2007-08-01'

 

After I changed trial_balance2.php in this way

<?php
require '../date_minus2m_1std2.php';
$data_last_bal = date('Y-m-d',strtotime(date_minus2m_1std())-1);
$dipu_p=2;
$diba_p=12;
require '../start_balance.php';
echo start_balance($dipu_p,$diba_p,$data_last_bal);
?>

 

but still I get

1969-11-01

start_balance()-$data_last_bal=1969-10-31

start_balance()-$dipu_p=2

start_balance()-$diba_p=12

 

So still not working....

any result in while fectching code

while ($row = mysql_fetch_assoc($result)) 
{
$fin_record_bal2	+=$row[fin_record_bal1];
echo 'start_balance()-while-$row[fin_record_bal1]='.$row[fin_record_bal1];
echo 'start_balance()-while-$fin_record_bal2='.$fin_record_bal2;
echo '<br>';
}

 

By

Amos

 

 

 

 

 

 

 

Link to comment
Share on other sites

Thank you for your reply.

 

I think you didn't focus problem,

problem is not inside date_minus2m_1std(), this function works well and it needs no changes.

 

problem is here in

trial_balance2.php

<?php
require '../date_minus2m_1std2.php';
$data_last_bal = date('Y-m-d',strtotime(date_minus2m_1std())-1);
$dipu_p=2;
$diba_p=12;
require '../start_balance.php';
echo start_balance($dipu_p,$diba_p,$data_last_bal);
?>

when I fire trial_balance2.php independently of the date_minus2m_1std() version that I use (correct one is date_minus2m_1std.php as per my first post, in any case), I get

start_balance()-$data_last_bal=2007-07-31

start_balance()-$dipu_p=2

start_balance()-$diba_p=12

I don't get to see inside the while (loop)

 

Instead if I fire

trial_balance.php

<?php
require '../start_balance.php';
$dipu_p=2;
$diba_p=12;
$data_last_bal='2007-10-1';
echo start_balance($dipu_p,$diba_p,$data_last_bal);
?>

I get

start_balance()-$data_last_bal=2007-10-1

start_balance()-$dipu_p=2

start_balance()-$diba_p=12

start_balance()-while-$row[fin_record_bal1]=-123.00start_balance()-while-$fin_record_bal2=-123

start_balance()-while-$row[fin_record_bal1]=-100.00start_balance()-while-$fin_record_bal2=-223

...............................etc.

till to get final balance (return $fin_record_bal2;) of -1002439.91

everything OK.

 

So problem is:

why start_balance() is working in trial_balance.php and not in trial_balance2.php??

 

 

 

 

ps:

start_balance.php

<?php
function start_balance($dipu_p,$diba_p,$data_last_bal)
{
require 'config.php';
require 'connect.php';
$query = "select (fin_record_debit-fin_record_credit) as fin_record_bal1 from fin_records where (fin_record_date<='$data_last_bal') and (id_production_unit='$dipu_p') and (id_bank_account='$diba_p')";
$result = mysql_query($query);
echo '<br>';
echo 'start_balance()-$data_last_bal='.$data_last_bal;
echo '<br>';
echo 'start_balance()-$dipu_p='.$dipu_p;
echo '<br>';
echo 'start_balance()-$diba_p='.$diba_p;
echo '<br>';
if (!$result) {
die("Errore nella query $query: " . mysql_error());
}
while ($row = mysql_fetch_assoc($result)) 
{
$fin_record_bal2	+=$row[fin_record_bal1];
echo 'start_balance()-while-$row[fin_record_bal1]='.$row[fin_record_bal1];
echo 'start_balance()-while-$fin_record_bal2='.$fin_record_bal2;
echo '<br>';
}
return $fin_record_bal2;
}
?>

 

 

 

 

Link to comment
Share on other sites

I tried to modifie all script loke this

trial_balance2.php

<?php
$data_last_bal = date('Y-m-d',strtotime(date_minus2m_1std())-1);
$dipu_p=2;
$diba_p=12;
echo start_balance($dipu_p,$diba_p,$data_last_bal);

function start_balance($pu,$ba,$data)
{
require '../config.php';
require '../connect.php';
$query = "select (fin_record_debit-fin_record_credit) as fin_record_bal1 from fin_records where (fin_record_date<='$data') and (id_production_unit='$pu') and (id_bank_account='$ba')";
$result = mysql_query($query);
echo '<br>';
echo 'start_balance()-$data='.$data;
echo '<br>';
echo 'start_balance()-$pu='.$pu;
echo '<br>';
echo 'start_balance()-$ba='.$ba;
echo '<br>';
if (!$result) {
die("Errore nella query $query: " . mysql_error());
}
while ($row = mysql_fetch_assoc($result)) 
{
$fin_record_bal2	+=$row[fin_record_bal1];
echo 'start_balance()-while-$row[fin_record_bal1]='.$row[fin_record_bal1];
echo 'start_balance()-while-$fin_record_bal2='.$fin_record_bal2;
echo '<br>';
}
return $fin_record_bal2;
}

function date_minus2m_1std()
{
$date_minus2m_1std = mktime (0,0,0,date("m")-2,date("d"),date("Y"));
$data_minus2m_1std = date('Y-m-01',$date_minus2m_1std);
return $data_minus2m_1std;
}
?>

 

nothing, I get always same risult

start_balance()-$data=2007-07-31

start_balance()-$pu=2

start_balance()-$ba=12

 

Any help will be very appreciate

 

Ciao

Amos

Link to comment
Share on other sites

Excuse me, I think everything is working perfectly, it is only my mistake.

I meen date_minus2m_1std() return '2007-7-31' and at that date there are no mouvements in the table, so trial_balance2.php doesn't show echo start_balance($dipu_p,$diba_p,$data_last_bal);

In stead when I fire trial_balance.php I get value for echo start_balance($dipu_p,$diba_p,$data_last_bal), because $data_last_bal='2007-10-1';it a valid date.

 

Sorry again.

Amos

Link to comment
Share on other sites

I should ask why you're not doing the summation in the database query?

 

SELECT (SUM(fin_record_debit)-SUM(fin_record_credit)) as fin_record_bal1 
FROM fin_records 
WHERE (fin_record_date<='$data') 
    AND (id_production_unit='$pu') 
    AND (id_bank_account='$ba')
GROUP BY id_production_unit, fin_record_date, id_bank_account;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.