PDA

View Full Version : Arrays and ListBox{}


ianhull
02-22-2007, 08:11 PM
Hi Guys,

I am using this code to get my information from php as an AMS Array (I think it's an AMS Array)




ClickHTTPReq = luacom.CreateObject("WinHttp.WinHttpRequest.5.1")
if not ClickHTTPReq then
return
end

vars = "ian";
ClickHTTPReq:Open("GET", "http://localhost/lua/index.php?fname=".. vars, 0)
ClickHTTPReq:Send();
if ClickHTTPReq.Status ~= 200 then
return
end

phpCompanies = ClickHTTPReq.ResponseText

// I tried adding this to retrieve the php return which is [echo "me, and, mydog";] unfortunatley it only displays me, and, mydog on the first line without breaking it up.

ListBox.AddItem("companiesList",phpCompanies);




If anyone can please help with getting all these values into the listBox it will be great.

ianhull
02-22-2007, 09:05 PM
Still working on this one

Here is what i have now




ClickHTTPReq = luacom.CreateObject("WinHttp.WinHttpRequest.5.1")
if not ClickHTTPReq then
return
end

vars = "ian";
ClickHTTPReq:Open("GET", "http://localhost/lua/index.php?fname=".. vars, 0)
ClickHTTPReq:Send();
if ClickHTTPReq.Status ~= 200 then
return
end
-- I can get this to work with this --
phpCompanies = {"me","and","mydog"};

-- BUT NOT THIS --
-- phpCompanies = ClickHTTPReq.Response.Text
-- My PHP File is Returning {"me","and","mydog"};

for varTitle,varValue in phpCompanies do
ListBox.AddItem("companiesList",varValue);
end

coderanger
02-22-2007, 10:38 PM
Will this work for you?:

In AMS Program:

ClickHTTPReq = luacom.CreateObject("WinHttp.WinHttpRequest.5.1")
if not ClickHTTPReq then
return
end

vars = "ian";
ClickHTTPReq:Open("GET", "http://localhost/lua/index.php?fname=".. vars, 0)
ClickHTTPReq:Send();
if ClickHTTPReq.Status ~= 200 then
return
end

phpCompanies = {ClickHTTPReq.ResponseText};

Debug.ShowWindow(true);

Table.Sort(phpCompanies, nil);

for x,y in phpCompanies do
Debug.Print(y.."\r\n");
end

for varTitle,varValue in phpCompanies do
ListBox.AddItem("ListBox1", varValue, "");
end



In PHP Script:
echo "me,\r\nand,\r\nmydog";

Peace

ianhull
02-22-2007, 10:44 PM
Thanks,

Unfortunatley this is returning me, and, mydog on 1 line.

coderanger
02-22-2007, 10:55 PM
OK, this isn't pretty, but it works:

AMS Code:
ClickHTTPReq = luacom.CreateObject("WinHttp.WinHttpRequest.5.1")
if not ClickHTTPReq then
return
end

vars = "ian";
ClickHTTPReq:Open("GET", "http://localhost/lua/index.php?fname=".. vars, 0)
ClickHTTPReq:Send();
if ClickHTTPReq.Status ~= 200 then
return
end

sString = String.Replace(ClickHTTPReq.ResponseText, ",", "\r\n", false);
TextFile.WriteFromString("C:\\MyFile.txt", sString, false);
phpCompanies = TextFile.ReadToTable("C:\\MyFile.txt");

for varTitle,varValue in phpCompanies do
ListBox.AddItem("ListBox1", varValue, "name");
end


PHP Code:
print("me,and,mydog");

I tried it, and it seems to separate the lines in the ListBox.

ianhull
02-22-2007, 11:10 PM
Code Ranger thanks very much for your effort on this one,

Your a star :yes

Very much appreciated

Dermot
02-23-2007, 12:33 AM
If you want to get what is returned by the PHP script, you can just use this.

result = HTTP.Submit("http://localhost/lua/index.php", {fname="ian"}, SUBMITWEB_POST, 20, 80, nil, nil);

You can use this function to convert a comma separated string into a table. This function is in the scripts folder.

function DelimitedStringToTable(DelimitedString, Delimiter)
tbReturn = {};
local strWorking;
local nPos = nil;
local strData;
local nTableIndex = 1;
local nDelimiterLength = String.Length(Delimiter);

if(nDelimiterLength < 1)then
tbReturn[nTableIndex] = DelimitedString;
return tbReturn;
end

strWorking = DelimitedString;
nPos = String.Find(strWorking,Delimiter);
while(nPos ~= -1)do
strData = String.Left(strWorking,nPos-1);
tbReturn[nTableIndex] = strData;
nTableIndex = nTableIndex + 1;
local nLength = String.Length(strWorking);
strWorking = String.Right(strWorking,nLength - (nPos + (nDelimiterLength-1)));
nPos = String.Find(strWorking,Delimiter);
end
if(strWorking ~= "")then
tbReturn[nTableIndex] = strWorking;
end

return tbReturn;
end

So if you have the PHP script return like this echo("me,and,my,dog"), you can use this function to convert it to a table and then step through the table and add the values to the listbox. Removes the need to write text files etc.

coderanger
02-23-2007, 02:25 PM
Hi Dermot,

Excellent solution!

Like Ian, I too need this type of solution for an application I've been working
on, but one problem that I'm noticing with all of these, is that it seems to
kind of freeze up the application until it finishes with connecting and receiving
the data back into the app.

Is there any way that you know of so the app doesn't go through this
momentary application freeze or are we kind of stuck with it?

Thanks for you help,
Patrick

Dermot
02-23-2007, 02:36 PM
The hanging is a problem with the http actions. Even downloading a tiny text file takes too long. I use a small dll I created for downloading small files. It is almost instant. Of course it is no good for submitting to a PHP script.

coderanger
02-23-2007, 08:12 PM
I use a small dll I created for downloading small files.
It is almost instant.

What is different about the DLL than the code above?

I mean, what is it that takes so long for the code above, but the code in the
DLL can be so fast?

Would it be a good idea to think about creating a DLL for posting/receiving
data from the Internet, and use it in these instances where a user is sitting
and waiting on the program?

I would be willing to look into creating something like this for the community
if it would help speed things up. (By speed things up, I mean to say help
speed up our programs, not the creation of this utility. ;) )

Patrick

ianhull
02-24-2007, 09:56 PM
Thanks Dermot,

I am wondering if it is possible to place a progress bar on the page which will move untill the function is complete?

Also, I am not too good with AMS and I cannot get this to work and populate my listBox1 so any help here would be great.

Thanks