Displaying rows, extracted from a database, one by one in a DataGrid
I have a table (session) in a database which has almost 72,000 rows. I extract those rows with the help php+mysql but when the result is returned to the HTTPService, i need to wait for some 32 seconds before the all the rows start appearing in the DataGrid at once.
Question Is there any way by which DataGrid may start displaying data one by one while the application may extract next rows in parallel. Or that the DataGrid may show data in chunks of hundreds. Like when application starts, it may show first 400 enteries in DataGrid, then the next 400 hundred are extracted until all the 72,000 rows are extracted?
Or can i involve threading such that one thread may be responsible for displaying data in datagrid while the other, executing in parallel may be responsible for extracting next data from database?
Thanks a lot guys as always.
<mx:HTTPService id="populateTable" url="request.php" method="POST" resultFormat="e4x">
<mx:request xmlns="">
<getResult>table</getResult>
</mx:request>
</mx:HTTPService>开发者_如何学运维
code from PHP file
function populateTable()
{
$Result = mysql_query("SELECT * FROM session" );
$Return = "<Sessions>";
while ( $row = mysql_fetch_object( $Result ) )
{
$Return .= "<session><no>".$no."</no>" .
"<srcIP>".$row->srcIP."</srcIP>" .
"<dstIP>".$row->dstIP."</dstIP>" .
"<sPort>".$row->sPort."</sPort>" .
"<dPort>".$row->dPort."</dPort>" .
"<sessionID>".$row->sessionID."</sessionID>" .
"<numberOfConnections>".$row->numberOfConnections."</numberOfConnections>" .
"</session>";
}
$Return .= "</Sessions>";
// mysql_free_result( $Result );
echo $Return;
}
Consider redesigning the app. No sane user is gonna need to see the whole 72K of data at the same time.
- Change the php script so that it accepts a startIndex parameter and selects 100 rows from that index instead of selecting
*
. - Add Next page/Previous page buttons in the flex app that causes
HTTPService
to be resend with changed startIndex value. Bind thelastResult
of theHTTPService
to the DataGridsdataProvider
.
Update:
<mx:HTTPService id="service" resultFormat="e4x"/>
<mx:DataGrid dataProvider="{service.lastResult}">
<!-- columns -->
</mx:DataGrid>
<mx:Button label="Next" click="next()"/>
<mx:Button label="Prev" click="prev()"/>
<mx:Script>
<![CDATA[
private var currentIndex:Number = 0;
private var itemsPerPage:Number = 100;
private var total:Number = 72000;
private function next():void
{
if(currentIndex + 1 >= total/itemsPerPage)
return;
currentIndex++;
service.url = "request.php?page=" + currentIndex;
service.send();
}
private function prev():void
{
if(currentIndex == 0)
return;
currentIndex--;
service.url = "request.php?page" + currentIndex;
service.send();
}
]]>
</mx:Script>
Here I've appended the index to the url itself. You may also use the request property of HTTPService
to send the data.
In php, change the query "SELECT * FROM session"
so that it selects only 100 queries based on the value of $_GET["page"]
.
精彩评论