开发者

PHP: Strange behaviour while calling custom php functions

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()
//---------------------------------------------------开发者_StackOverflow中文版-----------------------
{   
$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");
?>


If your createXML function generates xml on the same exact page generation, and it sends a Content-Type of application/xml then it might be possible the table stuff doesn't parse. Again, this would only be guessing until you provided code.


Well, the only answer is that createXML somewhere overwrites/deletes your (probably global) table. Please have a look into your code again.


like comments given above, will be difficult without more info/codes/backend db:

however my wild guest:

populateTable() insert data to db

createXML() query data inserted by populateTable().

looks like 2 diff db conn. certain db required u to do commit inside your code, if so, make sure you do it inside populateTable() before closing the db conn.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜