Pivity

Function to get code/variables from a website?

Last read Never
#1

Hello, for my newest project I do require a simple database that I can edit without updating the addon every time. I thought about a lua table on a website which's content I could retrieve with HTML fetch. But I don't know how I could use the table from the website in my code. So for example: Would it work if I'd just write

myTable={}
myTable[1]="First Entry"
myTable[2]="Second Entry"
myTable[3]="Third Entry"

this in the body part of my website and I'd retrieve it via http.fetch? I don't think so, that's why I am asking. Note: I am a beginner LUA developer and I'm currently only doing free stuff.

Thanks for any help! ~Momox

#2

So you're trying to edit something in a database from your server?

if so use PDO it's ezpz and more secure, make sure you prepare your statements too

Friendly x 1
#3

No, I dont wan't to edit something in the database, I want to retrieve the content from my database from a website and use it in my code. Sorry for my language.

#4
ByMomoxat 14th of September 2016 16:17:58
No, I dont wan't to edit something in the database, I want to retrieve the content from my database from a website and use it in my code. Sorry for my language.

oh then in that case use tmysql4, and make something similar to this in Lua:

$dbfind = $pdo->prepare('SELECT * FROM darkrp_player ORDER BY wallet DESC LIMIT 3'); - Select the data (in php u have to execute() it but i doubt that's how it works in Lua) $dbfind->fetchAll(PDO::FETCH_ASSOC); - grab all the data it selected

then after that you'd print it, I'm not a Lua developer but I believe it's somewhat similar to this. If you're making a leader board u should just make it in web and dHTML it to a derma panel or something.

I found this: https://wiki.garrysmod.com/page/sql/Query function SelectTable() sql.Query( "SELECT * FROM player_data ORDER BY wallet" ) end doubt this would work but it's a thought, someone that knows sql in Lua hopefully will reply soon. hopefully this gave you an idea.

#5

Mh... I don't think SQL will be the best solution... This addon will be release on the workshop and they all connect to the same database...

#6
ByMomoxat 14th of September 2016 16:46:32
Mh... I don't think SQL will be the best solution...This addon will be release on the workshop and they all connect to the same database...

You can't really retrieve data from a database without - connecting to it, selecting the tables, and executing those tables into your code

#7

So there's no way to do it like VCMod for example did it (All VCmod files are beeing downloaded from their webserver in any way)?

#8
ByMomoxat 14th of September 2016 16:51:58
So there's no way to do it like VCMod for example did it (All VCmod files are beeing downloaded from their webserver in any way)?

Wait you're trying to make a FastDL into your script? Use the workshop and make a file routing to lua/autorun/server/ and just add resource.AddWorkshop.

Agree x 1
#9

Ok, let's explain it again. Because I don't want to tell my idea yet, this is the idea of my script using different examples: The Web Page: A table like this: Basket1 = Apples Basket2 = Bananas Basket3 = Whatsoever (I know, that this is no actual lua table, but It will be one on the final website) Because I change the content of the "Baskets" every day I wrote them on a website to easily edit them without updating the addon. Now in my code I added a new command: "WhatsInside". So if I'd enter now "WhatsInside Basket1" in the Console it would print me Apples and the same goes on with Basket2 (Bananas) and Basket3... But that's just the theory, I now need any way to get the variables from the table on the website in my code. It'd be also ok if my could would just download a lua or text file containing the variables. Thanks for helping me!

#10

You basically want to set up static text on a page and have your Lua fetch it and run it?

Edited Does Lua have some sort of JSON parsing? I don't code Lua but I'm 99% sure you can do this.

#11

You can try something like ATomIK suggested, use RunString + http.Fetch. About the JSON part, lua has a simple util.JSONToTable function but nothing too special.

#12

util.JSONToTable will do the trick! (thx billy)

edit

ninja'd

Friendly x 1
#13

And how can I use that function to use the code from `my webpage within the lua script on my client?

#14

`http.Fetch(

"URL",

function onSuccess(contents,length,headers,httpcode)

end,

function onFailure(ERR)

end

) `

#15

Yeah... So far I've already been. So my website now contains the table I want. But If I use http fetch I just get a variable containing the hole body from the HTML... No what I want...

#16
ByMomoxat 14th of September 2016 17:42:58
Yeah... So far I've already been. So my website now contains the table I want. But If I use http fetch I just get a variable containing the hole body from the HTML... No what I want...

If I'm correct it return a string, which means you can just be creative using some string filter stuff ( String lib).

Friendly x 1
#17
ByMomoxat 14th of September 2016 17:42:58
Yeah... So far I've already been. So my website now contains the table I want. But If I use http fetch I just get a variable containing the hole body from the HTML... No what I want...

you shouldn't be returning HTML

Agree x 1Friendly x 1Optimistic x 1
#18
ByBillyat 14th of September 2016 18:30:37
you shouldn't be returning HTML

don't trust this guy, he got me to delete sys32

Funny x 2Agree x 1
#19

I trust him because I realised my mistake already thanks for the help to everyone!

#20
Bydrizzy.vipat 14th of September 2016 19:06:44
don't trust this guy, he got me to delete sys32

deleted my vps

Users viewing this thread
Nobody