Jump to content

SQL data in grid


Recommended Posts

Posted

Hello.

I want to fetch some data from sql and add in the gui but is not working.

addEvent("nume", true) 
addEventHandler("nume", getRootElement(), function(team) 
    local teamstr = getTeamName(team) 
    local sql = mysql:query("SELECT `username` FROM `accounts` WHERE `factiune` = '"..teamstr.."'") 
    if (sql) then 
        while true do 
            local row = mysql:fetch_assoc(sql) 
            if (not row) then break end 
            local jucatori = row["username"] 
            setElementData(source, "jucatori", jucatori) 
        end 
        mysql:free_result(sql) 
    end 
end) 

if i debug row["username"] it returns all those users that has my team.

  
grid = guiCreateGridList(9,29,467,379,false,meniu) 
local nume = guiGridListAddColumn(grid,"Nume",0.2) 
local team = getPlayerTeam(getLocalPlayer()) 
if (team) then 
    local players = getPlayersInTeam(team) 
    for k, v in ipairs(players) do 
        triggerServerEvent("nume", getLocalPlayer(), team) 
        local row = guiGridListAddRow(grid) 
        guiGridListSetItemText(grid, row, nume, getElementData( localPlayer, "jucatori"), false, false) 
    end 
end 

in grid only shows one user.

Posted

Of course, because you're overwritting the same element data over and over, you should create a table and insert each user then set that table as element data.

Posted

You mean change

setElementData(source, "jucatori", jucatori) 

to

setElementData(source, "jucatori", row) 

? This will give me a warning that guiGridListSetItemText expects string and not a table.

Posted

Well, from my point of view, that script makes no sense, you're looping all the players in the team of the local player and get his team, the loop is completely useless.

Posted

Try this:

-- client side:

grid = guiCreateGridList(9,29,467,379,false,meniu) 
local nume = guiGridListAddColumn(grid,"Nume",0.2) 
local team = getPlayerTeam ( localPlayer ) 
if ( team ) then 
    triggerServerEvent ( "nume", localPlayer, team ) 
end 
  
addEvent ( "returnUsers", true ) 
addEventHandler ( "returnUsers", root, 
    function ( users ) 
        for index, user in ipairs ( users ) do 
            local row = guiGridListAddRow ( grid ) 
            guiGridListSetItemText ( grid, row, nume, user [ "username" ], false, false ) 
        end 
    end 
) 

-- server side:

addEvent ( "nume", true ) 
addEventHandler ( "nume", getRootElement(), 
    function ( team ) 
        local teamstr = getTeamName ( team ) 
        local sql = mysql:query ( "SELECT `username` FROM `accounts` WHERE `factiune` = '".. teamstr .."'" ) 
        if ( sql ) then 
            triggerClientEvent ( source, "returnUsers", source, sql ) 
        end 
        mysql:free_result ( sql ) 
    end 
) 

Posted

Huh?

mysql:query ( "SELECT `username` FROM `accounts` WHERE `factiune` = '".. teamstr .."'" ) 

That should return a table with users matching that team, not a number?

Posted
You can try this:
triggerClientEvent ( source, "returnUsers", source, mysql:fetch_assoc ( sql ) ) 

It's wrong. By logic you can't use this because your self ( mysql ) is connection ( not query ).

triggerClientEvent ( source, "returnUsers", source, sql:fetch_assoc( ) ) 

Posted
You can try this:
triggerClientEvent ( source, "returnUsers", source, mysql:fetch_assoc ( sql ) ) 

It's wrong. By logic you can't use this because your self ( mysql ) is connection ( not query ).

triggerClientEvent ( source, "returnUsers", source, sql:fetch_assoc( ) ) 

How I'm supposed to know how his resource to manage MySQL queries work?

Posted

How about this ?

function sql(thePlayer) 
team = getPlayerTeam(thePlayer) 
teamstr = getTeamName(team) 
local sql = mysql:query("SELECT `username` FROM `accounts` WHERE `factiune` = '"..teamstr.."'") 
    if (sql) then 
        while true do 
            row = mysql:fetch_assoc(sql) 
            if (not row) then break end 
            jucatori = row["username"] 
            triggerClientEvent(source, "factiunemeniu", getRootElement(), jucatori) 
        end 
        mysql:free_result(sql) 
    end 
end 
addEventHandler("onPlayerJoin", getRootElement(), sql) 
  

function factiunemeniu(jucatori) 
meniu = guiCreateWindow(233,164,600,440,"Meniu factiune",false) 
grid = guiCreateGridList(9,29,467,379,false,meniu) 
local nume = guiGridListAddColumn(grid,"Nume",0.2) 
outputDebugString(jucatori) 
    for k, v in ipairs (jucatori) do 
        local row = guiGridListAddRow(grid) 
        guiGridListSetItemText(grid, row, nume, jucatori, false, false) 
    end 
end 
addEvent("factiunemeniu", true) 
addEventHandler("factiunemeniu", getRootElement(), factiunemeniu) 

On outputDebugString(jucatori) returns nil. In server side the debug shows

Posted
meniu = guiCreateWindow(233,164,600,440,"Meniu factiune",false) 
grid = guiCreateGridList(9,29,467,379,false,meniu) 
local nume = guiGridListAddColumn(grid,"Nume",0.2) 
  
function factiunemeniu(jucatori) 
    local row = guiGridListAddRow(grid) 
    guiGridListSetItemText(grid, row, nume, jucatori, false, false) 
end 
addEvent("factiunemeniu", true) 
addEventHandler("factiunemeniu", getRootElement(), factiunemeniu) 

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...