3.0.5

Iterrate Http Request Problem

I am trying to Iterrate through each row on the grid and then using AW.HTTP.Request create a dump file of the grid.

The code below works and creates a file, however it's not in same sort order as the grid, why????

Heres the code that creates the file,

<script>
var myCells = [ 
["MSFT","Microsoft Corporation", "314,571.156", "32,187.000", "55000"], 
["ORCL", "Oracle Corporation", "62,615.266", "9,519.000", "40650"], 
["SAP", "SAP AG (ADR)", "40,986.328", "8,296.420", "28961"] ,
["MSFT","Microsoft Corporation", "314,571.156", "32,187.000", "55000"], 
["ORCL", "Oracle Corporation", "62,615.266", "9,519.000", "40650"], 
["SAP", "SAP AG (ADR)", "40,986.328", "8,296.420", "28961"] ,
]; 

var myHeaders = [ 
        "Ticker", "Company Name", "Market Cap.", "$ Sales", "Employees" 
    ]; 

    
var obj = new AW.UI.Grid; 
obj.setSelectionMode("multi-row");
obj.setSize(760, 280);
obj.setCellText(myCells); 
obj.setHeaderText(myHeaders); 
obj.setColumnCount(5); 
obj.setRowCount(10); 
obj.setCellEditable(true); 

var print = new AW.UI.Button;
print.setStyle("width","100px");		
print.setControlText("Print Report");
                                
    
print.onControlClicked = function printReport() {
     for(var i=0;i<obj.getRowCount();i++) {
    var c1 = obj.getCellText(0, i);
    var c2 = obj.getCellText(1, i);
    var c3 = obj.getCellText(2, i);
    var a = new AW.HTTP.Request; 
    a.setParameter("C1", c1);
    a.setParameter("C2", c2);
    a.setParameter("C3", c3);
    a.setParameter("C4", c4);
    a.setParameter("C5", c5);
    a.setURL("HTTPResponse.php");
    a.setRequestMethod("POST");
    a.request();
             }	
     }
                        
document.write(obj); 
document.write(print); 
</script>


Here's the example HTTPResponse.php

<?php
//Creates the file for $data array 	
$myFile = "body.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = $_POST[C1].";".$_POST[C2].";".$_POST[C3].";".$_POST[C4].";".$_POST[C5].";"."\n";
fwrite($fh, $stringData);
fclose($fh);
?>


Here is the example file it creates

MSFT;Microsoft Corporation;314,571.156;;;
SAP;SAP AG (ADR);40,986.328;;;
ORCL;Oracle Corporation;62,615.266;;;
MSFT;Microsoft Corporation;314,571.156;;;
SAP;SAP AG (ADR);40,986.328;;;
;;;;;
;;;;;
;;;;;
;;;;;
ORCL;Oracle Corporation;62,615.266;;;



As you can see the order of the lines is not the same as the grid...

Please advice
JEz
November 21,
Um... I've just tried this from a different angle and iterated through the myCells array and tried sending this via the HTTP Request, but instead of writing the array elements its writing a number.... !! What I am doing wrong..

for(var i=0; i<myCells.length; i++) {
   alert(myCells[i]);
   var a = new AW.HTTP.Request; 
   a.setParameter("C1", myCells[i]);
   a.setURL("HTTPResponse.php");
   a.setRequestMethod("POST");
   a.request();
  }	
}


I know its working because I've added the Alert(myCells[i]) which pops up each of the Array's elements as it the for loop iterates through the array but once it's sent via the HTTP Request it's POST the elements as numbers???? As follows..

1;
2;
3;
4;
5;
6;


Pleeeeeeease Help!!


Jez
November 22,
I've worked out that I needed to convert the array element into a string which I have done below, but when I HTTP Request it, it's still not in the order of the grid/array..


What I am I doing wrong ??????

[Code]
for(var i=0; i<myCells.length; i++) {
var c1=myCells[i].toString();
var a = new AW.HTTP.Request;
a.setParameter("C1", c1);
a.setURL("HTTPResponse.php");
a.setRequestMethod("POST");
a.request();
}

[/code]
Jez
November 22,
Try:

for(var i=0;i<obj.getRowCount();i++) {
var c1 = obj.getCellText(0, obj.getRowPosition(i) );
var c2 = obj.getCellText(1, obj.getRowPosition(i) );
var c3 = obj.getCellText(2, obj.getRowPosition(i) );
November 22,
Thanks for your reply...

I've tried your example but it still failed to order the HTTP Request correctly and thus creates the file in the incorrect order.

However, I've found that if you alert something on each loop, it then orders them correcty?? I am not sure if this is a common problem with looping through an Array or something in the AW.HTTP.Request object.

So.. to get around the problem for now I've added pause in the loop that appears to do the trick

as follows: -

function pause(milliseconds) {
    var dt = new Date();
    while ((new Date()) - dt <= milliseconds) { /* Do nothing */ }
}

print.onControlClicked = function printReport() {
 for(var i=0;i<obj.getRowCount();i++) { 
   var c1 = obj.getCellText(0, obj.getRowPosition(i) );
   var c2 = obj.getCellText(1, obj.getRowPosition(i) );
   var c3 = obj.getCellText(2, obj.getRowPosition(i) ); 
   var a = new AW.HTTP.Request;  
   a.setParameter("C1", c1); 
   a.setParameter("C2", c2); 
   a.setParameter("C3", c3); 
   a.setURL("HTTPResponse.php"); 
   a.setRequestMethod("POST"); 
   a.request(); 
   pause(100);
  }
}


I would still like to know what is causing the problem, is it an AW phenomenon or something else ??

Jez

JEz
November 22,
Jez, Javascript has a setTimeout() function which I recommend you use rather than your pause() function.

Since you are sending these requests to a web server, examine the server's logs to see what order the requests are coming in as.
Anthony
November 23,
Ok.. I've tried to enable the web server logs to dump the requests into a log file. I am using Apache 2.2 on a windows platform but for some reason it's not dumping the incoming traffic POST even though the module is loaded and I have enabled it in the HTTPd.conf file but that's not for this forum... and I shall keep working on that... Arrrr!! :)

I did try and use the setTimeout() before stumbling across pause function but I couldn't find the correct way to use it, to timeout the request, which I am sure is because of my lack of knowledge, however the pause function is working and now my printed grid is in the correct order and I can move my project forward so thanks for your advice once again!

I'm writing a stock invoicing programme that's really taking shape, the standard edition that produces invoices and handles basic accounting functions is almost ready for release, So I will soon be purchasing the Developer License, but I must say that the help I have received from this forum and the fact you can use the evaluation version to this extent has allowed me to write my application with no set-up costs, which really gives you the edge... !! So thanks again,

Jez
Jez
November 23,
Jez,

try a.setAsync(false);
I think your responses don't come in the same order as the requests..

Peter
November 24,
Thanks Pete for your comments, any help or advice is always appreciated. I did try you suggestion, but it still seemed to fail.

My solution to pause the HTTP request also is not 100% (it seems to fail the first post but orders them correctly there after). I've had move on from this problem for now because of other priorities, but intend to re-visit. Hopefully I can eliminate the web server.. and maybe Alex might have a suggestion?

Thanks...
Jez
December 8,

This topic is archived.

See also:


Back to support forum