Forum Moderators: coopster

Message Too Old, No Replies

passing variables from one function to another.

         

tonynoriega

10:21 pm on Nov 15, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



i hope i can clearly define what i am trying to do...

I have a "mini" application that is on my site. It is a basic reporting tool for my agents. The query two dates to select a date range, to query records from a dbase table.

These dates become $date1 and $date2. They are then passed into the query to use as teh start and end dates.

When they hit submit, it posts back to the page and returns the records in a nicely presented format...! :)

I am using FPDF to try to generate a PDF of this page once it has some records displayed.

I got the FPDF function to output a PDF but the values keep coming back as zeros...i.e. "0 0 0"

Im guessing it is becuase i can not pass the $date1 and $date2 to this separate function....

I tried using an <a href....>Print Here</a> and calling the FPDF function, the using $_GET on the FPDF function, but no use....

Then i tried to create a submit button, and use $_POST on the FPDF function, but still didnt work....

can anyone tell me what i should be trying to do...?
(if that made sense)

hughie

10:28 pm on Nov 15, 2007 (gmt 0)

10+ Year Member



Not totally with you but this gives a quick indication of one way of passing variables between functions.

function getnumbers()
{
$numArray['One']=1;
$numArray['Two']=2;
return $numArray;
}

function displayit($NewArray)
{
echo $NewArray['One'];
echo '<Br>';
echo $NewArray['Two'];
}

$thearray=getnumbers();
displayit($thearray);

tonynoriega

7:01 pm on Nov 16, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I tried to shorten this as much as i could while still keeping the base of the function in tact....Again, what i do here is select 2 dates from a calendar, and a specific community. That creates 3 variables $date1 and $date2, also $community.

It then posts back to the pate, displaying the records within the date range.

The next function after this, is where i need to pass the variables to, so that when it generates the PDF, it uses the same variables and outputs the same number of records....

Here is the first function
**********************************************************************
function builderreporttestreport_display_addon() {
global $config, $conn, $jscript;
require_once($config['basepath'] . '/include/misc.inc.php');

$misc = new misc();
//********JAVASCRIPTS GO HERE******

//**********DECLARE VARIABLES*********

$date1 = $_POST['date1'];
$date2 = $_POST['date2'];
$community = $_POST['community'];

//*********BUNCH MORE VARIABLES HERE************

//*********DISPLAY FORM TO SELECT DATES AND COMMUNITY********
$display .='<form name="registration" action="index.php?action=addon_builderreport_displaytestreport" method="post">
<table>
<tr>
<td>
<input type="text" name="date1" value="" size=15 id="date1">
<a href="#" onclick="cal.select(document.forms[\'registration\'].date1,\'anchor1\',\'yyyy-MM-dd\'); return false;" name="anchor1" id="anchor1">Begin Date</a>
</td>
<td>
<input type="text" name="date2" value="" size=15 id="date2">
<a href="#" onclick="cal.select(document.forms[\'registration\'].date2,\'anchor2\',\'yyyy-MM-dd\'); return false;" name="anchor2" id="anchor2">End Date</a>
</td>
<td>
<select name="community" id="community">
<option selected>Community</option>
<option value="Sonata">Sonata</option>
<option value="Hazelwood Village">Hazelwood Village</option>
<option value="RockHampton">RockHampton</option>
</select>
</td>
<td>
<input type="submit" name="submit" value="Go" class="button">
</td>
</tr>
</table><br /></form>';

//**********THIS QUERY USES THE VARIABLES FROM THE CALENDAR SELECTS I.E. $date1 and $date2***********

$sql = 'SELECT COUNT(community) AS comm_count, (SELECT COUNT(last_visit) FROM registration_table WHERE last_visit BETWEEN date(\''. $date1 . '\') AND date(\'' . $date2 . '\') AND community=\'' . $community . '\') AS last_visit, (SELECT COUNT(lead_date) FROM registration_table WHERE lead_date BETWEEN date(\''. $date1 . '\') AND date(\'' . $date2 . '\') AND community=\'' . $community . '\') AS lead_date FROM registration_table WHERE entry_time BETWEEN date(\''. $date1 . '\') AND date(\'' . $date2 . '\') AND community=\'' . $community . '\'';

$recordSet = $conn->Execute($sql);

if ($recordSet === false) {
$misc->log_error($sql);
}

$display .='<table>';

while (!$recordSet->EOF) {

$reg_lv = $misc->make_db_unsafe ($recordSet->fields['last_visit']);
$reg_ld = $misc->make_db_unsafe ($recordSet->fields['lead_date']);
$reg_cc = $misc->make_db_unsafe ($recordSet->fields['comm_count']);
$reg_tc = $reg_ld + $reg_lv;

//********DISPLAY RECORDS FROM QUERY HERE IN A TABLE LAYOUT********

$recordSet->MoveNext();

}

$display .='</table><br /><br /><br /></form></body></html>';

}

return $display;
}
*******************************************************************
Then next is the FPDF Function where i need to get the variables...now i know it is working, becuase if i put \'2007-11-05\' and \'2007-11-11\' as the respective dates in the query, it returns the correct amount of records.... but again, i need to get the variable from the first function. How do i pass those variables above down here.... Like i mentioned, i tried calling this function with a submit button, and putting in the variables as hidden fields and placing $date1 = $_GET['date1']; in the FPDF function, but it didnt work...
*******************************************************************
function builderreportpdf_display_addon() {
global $config, $conn;
require_once($config['basepath'] . '/include/misc.inc.php');
require('fpdf.php');

$misc = new misc();

//The FPDF code starts here

//Create new pdf file
$pdf=new FPDF();

//Open file
$pdf->Open();

//Disable automatic page break
$pdf->SetAutoPageBreak(false);

//Add first page
$pdf->AddPage();

//set initial y axis position per page
$y_axis_initial = 25;

// These variables define the SQL data that is required

$sql = 'SELECT COUNT(community) AS comm_count, (SELECT COUNT(last_visit) FROM registration_table WHERE last_visit BETWEEN date(\'2007-11-05\') AND date(\'2007-11-11\') AND community=\'Sonata\') AS last_visit, (SELECT COUNT(lead_date) FROM registration_table WHERE lead_date BETWEEN date(\'2007-11-05\') AND date(\'2007-11-11\') AND community=\'Sonata\') AS lead_date FROM registration_table WHERE entry_time BETWEEN date(\'2007-11-05\') AND date(\'2007-11-11\') AND community=\'Sonata\'';

$result=mysql_query($sql) or die(mysql_error());

$pdf=new FPDF();

$pdf->SetAutoPageBreak(false);

//Add first page
$pdf->AddPage();

//set initial y axis position per page
$y_axis = 25;
$x_axis = 18;

//Set Row Height
$row_height = 4;

$y_axis = $y_axis + $row_height;

$pdf->SetFont('Arial','B',14);

//To draw a border around the brochure

$pdf->Line(10,10,200,10);
$pdf->Line(10,10,10,290);
$pdf->Line(200,10,200,290);
$pdf->Line(10,290,200,290);

$i = 0;
while($row = mysql_fetch_array($result)) {

$LV = $row['last_visit'];
$LD = $row['lead_date'];
$TT = ($LV + $LD);
$pdf->SetY($y_axis);
$pdf->SetX($x_axis);
$pdf->Write(6,$LV);
$pdf->Write(6,$LD);
$pdf->Write(6,$TT);
//Go to next row
$y_axis = $y_axis + $row_height;
$i = $i + 1;
}

//Create file

$pdf->Output();
}

hughie

11:23 pm on Nov 17, 2007 (gmt 0)

10+ Year Member



I think that looks ok (without having a major scan through)

i think the problem is what you're posting from the form isn;t correct.

put in
print_r($_POST); somewhere in the script, if the date1 and date2 variables aren't correct then amened your form to match what you want.