How to create a lua-compatible site
#1
Posted 06 February 2013 - 08:09 AM
#2
Posted 06 February 2013 - 08:29 AM
If you want to construct the table from a database, I suggest learning MySQL as well.
#3
Posted 06 February 2013 - 11:57 AM
#4
Posted 06 February 2013 - 12:54 PM
These commands will let you convert a table to text and back.
The most plain, bare-bones, simple way to do it is to serialize your table, and copy/paste the output into a file that you upload to your web host.
You can then use the http API to retrieve that page and give the contents to unserialize. Now you have your table from the website!
If this is for your own uses, this might be enough - as long as you don't mind creating, editing, and uploading such files to the website.
If you want either
A ) to store the table from lua, or
b ) allow others to create this data
then you will need a more dynamic page. The PHP suggestion from Orwell and xuma202 are more along those lines.
(Personally I use TCL on the server side - but PHP is by far the most common pre-processor out there, and there is a ton of resources available to help you out)
If you build a big table in Lua, using lots of variable types and sub-tables and different indexes (both numbers and strings) and then toss it into textutils.serialize() - study the output, as this will be what you need to recreate to get data from a database into a Lua table directly.
It's a pretty straight forward format.
{[1]="one",[2]="two",["three"]="snarf!",}
If that was in a file called "mytable.txt" that you upload to the website, it will probably have a full URL like "ht tp://www.example.com/~user/mytable.txt"
Some example code to read in the above file on the website into a table named "tTable"
local sData = http.get("http://www.example.com/~user/mytable.txt")
local sText = sData.readLine()
sData.close()
local tTable = {}
tTable = textutils.unserialize(sText)
print(tTable["three"])
snarf!
#5
Posted 06 February 2013 - 01:05 PM
#6
Posted 06 February 2013 - 01:56 PM
urielsalis, on 06 February 2013 - 01:05 PM, said:
definitely PHP then
any sort of dynamic webpage is often best done by creating a wordlist.php file
it would end up being something like website.com/wordlist.php?blabla=bla2
#7
Posted 06 February 2013 - 02:27 PM
tesla1889, on 06 February 2013 - 01:56 PM, said:
urielsalis, on 06 February 2013 - 01:05 PM, said:
definitely PHP then
any sort of dynamic webpage is often best done by creating a wordlist.php file
it would end up being something like website.com/wordlist.php?blabla=bla2
#8
Posted 06 February 2013 - 03:49 PM
#9
Posted 06 February 2013 - 09:16 PM
#10
Posted 07 February 2013 - 08:54 AM
lua:
function getData(index)
return http.get("www.yoururl.com/getdata.php?index=" .. textutils.urlEncode(tostring(index)))
end
function sendData(index,value)
return http.post("www.yoururl.com/senddata.php?index=" .. textutils.urlEncode(tostring(index)) .. "&value=" .. textutils.urlEncode(textutils.serialize(value)))
end
getdata.php:<?php
function getdata($index) {
$connect = @mysql_connect($host,$username,$password);
if (!$connect) die();
elseif (!@mysql_select_db($database,$connect)) {
@mysql_close($connect);
die();
}
$result = @mysql_query("SELECT * FROM yourtable WHERE index='$index'");
@mysql_close($connect);
if (!$result) die();
die($result[2]);
}
if isset($_GET["index"]) getdata($_GET["index"]);
?>
senddata.php:<?php
function senddata($index,$data) {
$connect = @mysql_connect($host,$username,$password);
if (!$connect) die();
elseif (!@mysql_select_db($database,$connect)) {
@mysql_close($connect);
die();
}
$result = @mysql_query("INSERT * INTO yourtable (index,value) VALUES ('$index','$value')");
@mysql_close($connect);
if (!$result) die();
die("success");
}
if (isset($_GET["index"]) && isset($_GET["value"])) getdata($_GET["index"],$_GET["value"]);
?>
#11
Posted 07 February 2013 - 09:02 AM
tesla1889, on 07 February 2013 - 08:54 AM, said:
lua:
function getData(index)
return http.get("www.yoururl.com/getdata.php?index=" .. textutils.urlEncode(tostring(index)))
end
function sendData(index,value)
return http.post("www.yoururl.com/senddata.php?index=" .. textutils.urlEncode(tostring(index)) .. "&value=" .. textutils.urlEncode(textutils.serialize(value)))
end
getdata.php:<?php
function getdata($index) {
$connect = @mysql_connect($host,$username,$password);
if (!$connect) die();
elseif (!@mysql_select_db($database,$connect)) {
@mysql_close($connect);
die();
}
$result = @mysql_query("SELECT * FROM yourtable WHERE index='$index'");
@mysql_close($connect);
if (!$result) die();
die($result[2]);
}
if isset($_GET["index"]) getdata($_GET["index"]);
?>
senddata.php:<?php
function senddata($index,$data) {
$connect = @mysql_connect($host,$username,$password);
if (!$connect) die();
elseif (!@mysql_select_db($database,$connect)) {
@mysql_close($connect);
die();
}
$result = @mysql_query("INSERT * INTO yourtable (index,value) VALUES ('$index','$value')");
@mysql_close($connect);
if (!$result) die();
die("success");
}
if (isset($_GET["index"]) && isset($_GET["value"])) getdata($_GET["index"],$_GET["value"]);
?>
#12
Posted 07 February 2013 - 10:02 AM
urielsalis, on 07 February 2013 - 09:02 AM, said:
You need to show at least some effort at finding stuff out yourself..
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











