PHP: Strange behaviour while calling custom php functions
- by baltusaj
I am facing a strange behavior while coding in PHP with Flex. Let me explain the situation:
I have two funcions lets say:
populateTable() //puts some data in a table made with flex
createXML() //creates an xml file which is used by Fusion Charts to create a chart
Now, if i call populateTable() alone, the table gets populated with data but if i call it with createXML(), the table doesn't get populated but createXML() does it's work i.e. creates an xml file.
Even if i run following code, only xml file gets generated but table remains empty whereas i called populateTable() before createXML(). Any idea what may be going wrong?
MXML Part
<mx:HTTPService id="userRequest" url="request.php" method="POST" resultFormat="e4x">
<mx:request xmlns="">
<getResult>send</getResult>
</mx:request>
and
<mx:DataGrid id="dgUserRequest" dataProvider="{userRequest.lastResult.user}" x="28.5" y="36" width="525" height="250" >
<mx:columns>
<mx:DataGridColumn headerText="No." dataField="no" />
<mx:DataGridColumn headerText="Name" dataField="name"/>
<mx:DataGridColumn headerText="Age" dataField="age"/>
</mx:columns>
PHP Part
<?php
//--------------------------------------------------------------------------
function initialize($username,$password,$database)
//--------------------------------------------------------------------------
{
# Connect to the database
$link = mysql_connect("localhost", $username,$password);
if (!$link)
{
die('Could not connected to the database : ' . mysql_error());
}
# Select the database
$db_selected = mysql_select_db($database, $link);
if (!$db_selected)
{
die ('Could not select the DB : ' . mysql_error());
}
//
populateTable();
createXML();
# Close database connection
}
//--------------------------------------------------------------------------
populateTable()
//--------------------------------------------------------------------------
{
if($_POST['getResult'] == 'send')
{
$Result = mysql_query("SELECT * FROM session" );
$Return = "<Users>";
$no = 1;
while ( $row = mysql_fetch_object( $Result ) )
{
$Return .= "<user><no>".$no."</no><name>".$row->name."</name><age>".$row->age."</age><salary>". $row->salary."</salary></session>";
$no=$no+1;
$Return .= "</Users>";
mysql_free_result( $Result );
print ($Return);
}
//--------------------------------------------------------------------------
createXML()
//--------------------------------------------------------------------------
{
$users=array
(
"0"=>array("",0),
"1"=>array("Obama",0),
"2"=>array("Zardari",0),
"3"=>array("Imran Khan",0),
"4"=>array("Ahmadenijad",0)
);
$selectedUsers=array(1,4); //this means only obama and ahmadenijad are selected and the xml file will contain info related to them only
//Extracting salaries of selected users
$size=count($users);
for($i = 0; $i<$size; $i++)
{
//initialize temp which will calculate total throughput for each protocol separately
$salary = 0;
$result = mysql_query("SELECT salary FROM userInfo where name='$users[$selectedUsers[$i]][0]'");
$row = mysql_fetch_array($result))
$salary = $row['salary'];
}
$users[$selectedUsers[$i]][1]=$salary;
}
//creating XML string
$chartContent = "<chart caption=\"Users Vs Salaries\" formatNumberScale=\"0\" pieSliceDepth=\"30\" startingAngle=\"125\">";
for($i=0;$i<$size;$i++)
{
$chartContent .= "<set label=\"".$users[$selectedUsers[$i]][0]."\" value=\"".$users[$selectedUsers[$i]][1]."\"/>";
}
$chartContent .= "<styles>" .
"<definition>" .
"<style type=\"font\" name=\"CaptionFont\" size=\"16\" color=\"666666\"/>" .
"<style type=\"font\" name=\"SubCaptionFont\" bold=\"0\"/>" .
"</definition>" .
"<application>" .
"<apply toObject=\"caption\" styles=\"CaptionFont\"/>" .
"<apply toObject=\"SubCaption\" styles=\"SubCaptionFont\"/>" .
"</application>" .
"</styles>" .
"</chart>";
$file_handle = fopen('ChartData.xml','w');
fwrite($file_handle,$chartContent);
fclose($file_handle);
}
initialize("root","","hiddenpeak");
?>