View Full Version : PHP Array into AMS?
ianhull
02-21-2007, 04:42 PM
Hi All,
Is it possible to somehow load a PHP Array in to AMS and use that Array?
Thanks
TJ_Tigger
02-21-2007, 05:02 PM
Just have your PHP script return a concatenated string to AMS and you can then use Table.Concat to format that string into a table.
Or you could have PHP return the formating of a table so when it sets the variable used in HTTP.Submit it will format it as a table. Maybe, I would have to play with it.
Tigg
ianhull
02-21-2007, 05:08 PM
Thanks again Tigg,
Wow didn't even see that concat!
:)
coderanger
02-21-2007, 07:53 PM
Something else I wanted to add here since this is a fresh post, I keep
forgetting to respond every time I see this subject come up, is that you can
have your PHP scripts (or any other web language/scripts for that matter)
write out whatever you need to the screen, and have your AMS program read
it in.
In other words, let's say you have a lot of info to pass back to your program,
you can have your PHP script write it out to the screen as Lua code, and
have your AMS program read it in, variables and all.
I had a program I was working on a little while ago do this very thing so I
could grab data from my database. It was one of my first programs while I
was testing out AMS and looking at the possibilities.
You could have your app read in the info, maybe create a temporary file on
your system, and then read it in with the Lua dofile() function or whatever.
The reason I ended up buying AMS and using it more than I will ever use my
copy of Visual Basic Dot NET is because of the Lua scripting! Man is it cool
and fast, and works extremely well for stuff like this.
I can also create these unbelievable programs in a matter of minutes, and in
Visual Basic, it would have taken me hours to create the same thing.
Anyway, you could do something like this:
<?php
//Connect to database - prints 1 if connect fails and prints 2 if DB select fails
mysql_connect ($dbhost, $dbuser, $dbpasswd) OR DIE ('1');
mysql_select_db ($dbname) OR DIE ('2');
$q1 = "SELECT * FROM DB_Table WHERE DB_Some_Field = '$_POST[VarFromAMSProgram]'";
$r1 = mysql_query($q1) OR DIE ('3');
$a1 = mysql_fetch_array($r1);
//Add DB data to AMS/Lua variables
$data = "sVar1ForAMSProgram = \"Here Is Some Data ".$a1[Field_Info1_You_Want]."\";\r\n";
$data .= "sVar2ForAMSProgram = \"Here Is Some More Data ".$a1[Field_Info2_You_Want]."\";\r\n";
//write the DB data to a file
$file = "lua/data.lua";
$handle = fopen($file, 'w');
fwrite($handle, $data);
fclose($handle);
?>
Or, In The PHP Script Above For An Table Array:
//Add DB data to variable
$data = "sVarForAMSProgram = {}";
$data .= "sVarForAMSProgram.data1 = \"Here Is Some Data ".$a1[Field_Info1_You_Want]."\";\r\n";
$data .= "sVarForAMSProgram.data2 = \"Here Is Some More Data ".$a1[Field_Info2_You_Want]."\";\r\n";
In your AMS Program, You Might Do Something Like This:
HTTP.Download( "http://www.domain.com/lua/data.lua", _TempFolder.."\\data.lua", MODE_TEXT, 20, 80, nil, nil, nil );
dofile( _TempFolder.."\\data.lua" );
You may also want to remove the data.lua file from your server after you
receive the needed information from it so others cannot access it.
This type of thing also works really, really well for doing "live" program updates
for your customer/client software, the possibilities are really endless.
This is just one of many ways you can handle this type of situation. There's
prettier and there's uglier. Hope that helps for future ideas. Sorry, I didn't
mean to carry on like this, I just love this application.
Patrick
ianhull
02-21-2007, 11:08 PM
WOW Patrick,
Excellent stuff!
Man am I going to have some fun with this :)
Would it be possible to open the php file from the server without creating and downloading a .lua file?
HTTP.Download( "http://www.domain.com/lua/data.lua", _TempFolder.."\\data.lua", MODE_TEXT, 20, 80, nil, nil, nil );
dofile( _TempFolder.."\\data.lua" );
//Change to something like
result = TextFile.ReadToString("http://www.domain.com/my.php");
Thanks
coderanger
02-22-2007, 12:53 AM
A couple of ways that I can think of.
1) Get sside's WebBrowser thingy he just released, and do something like this:
WebBrowserWindow.SetUrl("http://www.domain.com/lua/data.html");
Application.Sleep(200);
result = WebBrowserWindow.GetTextContent();
--result contains your string. You can view it in a message popup for testing..
Dialog.Message("Notice", result, MB_OK, MB_ICONINFORMATION, MB_DEFBUTTON1);
You'll need to add an Application.Sleep because it will take a few milliseconds
to read your string in, otherwise, you'll most likely get an error or worse, a
crashed program. You will also have to change your data filename to .html
extension.
OR, A MUCH BETTER SOLUTION:
You might want to use Worms "GetUrlSrc.dll" and read it in that way. I think
it's a much better solution than that above, but the other will work in a pinch.
See this for Worms cool dll:
http://www.indigorose.com/forums/showthread.php?t=10772&highlight=read+file+server
One last option would be to connect and get your info directly from your MySQL
database. Again, endless solutions. I wish all of my other programming tools offered
so many solutions.
Patrick
P.S. Funny thing is, it was a post that you posted and Worm responded to for
just this type of thing.. he he he
ianhull
02-22-2007, 05:52 AM
Oh My Gawd,
This is absolutley fantastic,
This little piece of code allows me to connect to a web server, send variables/values wait for a response, write out lua variables suitable for AMS and pass and play with the results vars in AMS woo hoo.
I really like it.
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
s = ClickHTTPReq.ResponseText
Dialog.Message("Notice", s, MB_OK, MB_ICONINFORMATION, MB_DEFBUTTON1);
:) lol yes, I remeber that post but thought it was just for text files. haha
Powered by vBulletin™ Version 4.0.6 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.