Jump to content

Moving SQL Data to Client Side


Recommended Posts

First you dbPoll that query you can then just send over the Lua table with triggerClientEvent / triggerClientLatentEvent depending on the amount of players you have in the database. You can then use a loop to create the grid. There should be number of examples on the wiki that shows how to populate gridlists, the only difference here would be that you're populating from a MySQL result table.

  • Like 1
Link to comment
-- SERVER SIDE

function generateTable()
local query = dbQuery(connection, "SELECT * FROM players")
local result = dbPoll(query, -1)
    if result then
    --
    end
end
addEvent("gen:table", true)
addEventHandler("gen:table", getRootElement(), generateTable)

 

-- CLIENT SIDE

local screenW, screenH = guiGetScreenSize()

function openTable()
        window = guiCreateWindow((screenW - 468) / 2, (screenH - 360) / 2, 468, 360, "Players", false)
        guiWindowSetSizable(window, false)

        gridlist = guiCreateGridList(19, 39, 426, 304, false, window)
        guiGridListAddColumn(gridlist, "ID", 0.2)
        guiGridListAddColumn(gridlist, "Name", 0.2)
        guiGridListAddColumn(gridlist, "Rank", 0.2)
        guiGridListAddColumn(gridlist, "IP Address", 0.2) 
  
        guiGridListAddRow(gridlist)
        guiGridListSetItemText(gridlist, 0, 1, "-", false, false)
        guiGridListSetItemText(gridlist, 0, 2, "-", false, false)
        guiGridListSetItemText(gridlist, 0, 3, "-", false, false)
        guiGridListSetItemText(gridlist, 0, 4, "-", false, false
    
        showCursor(true)     
        guiSetVisible(window, true)
end
addCommandHandler("open", openTable)

I know how to trigger it, but how would I take over the data onto the Client Side, via triggerServerEvent? And then set it to the Gridlist

Edited by raysmta
Link to comment

I think the easiest would be to add the commandHandler server side, then get the data from MySQL and trigger to client to create the GUI, this way you wouldn't: triggerToServer - triggerToClient, you would only triggerToClient. 

Link to comment

It's a proper Lua table, the same way as any other variable..

function generateTable()
  local query = dbQuery(connection, "SELECT * FROM players")
  local result = dbPoll(query, -1)
  if result and #result > 0 then -- result is always true, except when the connection is not working, make sure you always check the length..
    triggerClientEvent(playerElement,"myEvent", playerElement, result)  

 

Link to comment

I get that part, but how do I apply the certain rows and stuff to the client side script, like -

-- CLIENT SIDE

local screenW, screenH = guiGetScreenSize()

function openTable(result)
  
        window = guiCreateWindow((screenW - 468) / 2, (screenH - 360) / 2, 468, 360, "Players", false)
        guiWindowSetSizable(window, false)

        gridlist = guiCreateGridList(19, 39, 426, 304, false, window)
        guiGridListAddColumn(gridlist, "ID", 0.2)
        guiGridListAddColumn(gridlist, "Name", 0.2)
        guiGridListAddColumn(gridlist, "Rank", 0.2)
        guiGridListAddColumn(gridlist, "IP Address", 0.2) 
  
        guiGridListAddRow(gridlist)
        guiGridListSetItemText(gridlist, 0, 1, "-", false, false)
        guiGridListSetItemText(gridlist, 0, 2, "-", false, false)
        guiGridListSetItemText(gridlist, 0, 3, "-", false, false)
        guiGridListSetItemText(gridlist, 0, 4, "-", false, false
    
        showCursor(true)     
        guiSetVisible(window, true)
end
addCommandHandler("open", openTable)

How do I apply the different column's and data.

Link to comment

I don't see your triggerClientEvent which sends over the data, so I'll quickly write a piece of code to help you with it a little bit better, although I think I explained everything above..

local myGuiWindow, myGuiGrid

function openGUI()
  myGuiWindow = guiCreateWindow()
  myGuiGrid = guiCreateGridlist(..., myGuiWindow)
  guiGridListAddColumn(myGuiGrid, "ID", 0.2)
  guiGridListAddColumn(myGuiGrid, "Name", 0.2)
  guiGridListAddColumn(myGuiGrid, "Rank", 0.2)
  guiGridListAddColumn(myGuiGrid, "IP Address", 0.2) 
  triggerServerEvent("onClientRequestMyData", localPlayer, "fly")
end
addCommandHandler("open", openGUI)

addEvent("onClientSendMyData", true)
addEventHandler("onClientSendMyData", root, function(myData)
  if not guiGetVisible(myGuiWindow) then return end -- make sure it is open
  guiGridListClear(myGuiGrid) -- might need to clear it first
  for k, rows in ipairs(myData) do
    local row = guiGridListAddRow(myGuiGrid)
    guiGridListSetItemText(myGuiGrid, row, 1, rows["someId"], false, false)
    guiGridListSetItemText(myGuiGrid, row, 2, rows["someName"], false, false)
    guiGridListSetItemText(myGuiGrid, row, 3, rows["someRank"], false, false)
    guiGridListSetItemText(myGuiGrid, row, 4, rows["someIP"], false, false)
  end
end)

-- server
addEvent("onClientRequestMyData", true)
addEventHandler("onClientRequestMyData", root, function(someInfo)
  local q = dbQuery(connection, "SELECT * FROM somewhere-over-the-rainbow WHERE blue-birds = ?", someInfo)
  local r = dbPoll(q, -1)
  triggerClientEvent(client, "onClientSendMyData", client, r)
end)

Not tested, not a fully functional code (probably), should give you a brief idea. As I mentioned earlier, you might be better of binding the open command server side and only trigger once. But if you need some info from the client (as it is in the code), you can leave it client side, since you'd need to trigger from c to s anyway..

Edited by pa3ck
Link to comment

Thanks, just was wondering what you meant, but I understand it now kind of, so it sends data back and forth, in the triggers. I wanted to ask one last thing whats the different between ipairs and pairs? I mean last time I tried it they both outputted the same thing, so I was kind of confused.

Link to comment
  • Administrators

http://www.luafaq.org/#T1.10

local table = {fruit = "apple", sport = "football", "toast", language = "french"}
local itable = {"apple", "football", "toast", language = "french"}

for i,v in pairs(table) do
  print(i,v)
end
--[OUTPUT]
--1 toast
--fruit	apple		
--sport	football		
--language	french			

for i,v in pairs(itable) do
  print(i,v)
end
--[OUTPUT]
--1	apple		
--2	football	
--3 toast
--language french


for i,v in ipairs(itable) do
  print(i,v)
end
--[OUTPUT]
--1	apple		
--2	football	
--3 toast



for i,v in ipairs(table) do
  print(i,v)
end
--[OUTPUT]
--1	toast

 

 

Edited by LopSided_
Link to comment

ipairs loops through indexed tables only (in order), pairs loops through everything (random order.).

Indexed table is when the key is a number ie. myTable[1] = "something" or there's no key (well, the key is a number by default..), non indexed when the key is not a number myTable["smt"] = "something".

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...