HustraDev Posted September 29, 2016 Share Posted September 29, 2016 Hi 2 all , i'm working on script i almost finish it but i have a problem that is i want to insert database row's in GUI gridlist well i want to poll all rows by account name like this >> Server Side function() local rows = dbQuery( connection, "SELECT * FROM `VList` WHERE `account`=?",getAccountName(getPlayerAccount(client))) local result = dbPoll( rows, -3 ) if result and type( result ) == 'table' then local sID = result[1][ 'id' ] local sName = result[2][ 'name' ] local sPrice = result[3][ 'price' ] triggerClientEvent(source,"SendRows",client,sID,sName,sPrice) end Client : function(sID,sName,sPrice) local row = guiGridListAddRow ( PlayerVehicleList ) guiGridListSetItemText ( PlayerVehicleList, row, 1, sID, false, false ) guiGridListSetItemText ( PlayerVehicleList, row, 2, sName, false, false ) guiGridListSetItemText ( PlayerVehicleList, row, 3, sPrice, false, false ) end but the problem is if the database contains more the 1 rows have the same account name it well insert only a 1 row in gui gridlist : [ database.db ] ---------------------------------------- account | id | name | price | koko00 | 12 | hustra | 2000 | koko00 | 14 | dev | 5000 | ---------------------------------------- Gui gridlist ----------------------- id | name | price 12 | dev | 2000 ----------------------- i hope anyone can help me ... sorry for bad english Link to comment
HustraDev Posted September 29, 2016 Author Share Posted September 29, 2016 ِ-- any help? Link to comment
pa3ck Posted September 29, 2016 Share Posted September 29, 2016 (edited) If you are getting more than one rows from a database, you will obviously need a loop to go through each of the rows. Edited September 29, 2016 by pa3ck 1 Link to comment
Gravestone Posted September 29, 2016 Share Posted September 29, 2016 (edited) As pa3ck said, use 'for' statement for sending each row to the client side. 5 hours ago, HustraDev said: getAccountName(getPlayerAccount(client))) And this, 'client' is a clientsided pre defined variable and I don't anywhere client being defined in your function. Edited September 29, 2016 by Gravestone 1 Link to comment
HustraDev Posted September 29, 2016 Author Share Posted September 29, 2016 28 minutes ago, Gravestone said: As pa3ck said, use 'for' statement for sending each row to the client side. And this, 'client' is a clientsided pre defined variable and I don't anywhere client being defined in your function. can you explain to how to use for statement to get every rows that contains account name? Link to comment
Gravestone Posted September 29, 2016 Share Posted September 29, 2016 10 minutes ago, HustraDev said: can you explain to how to use for statement to get every rows that contains account name? for index, value in ipairs(result) do -- code here end 1 Link to comment
HustraDev Posted September 29, 2016 Author Share Posted September 29, 2016 1 minute ago, Gravestone said: for index, value in ipairs(result) do -- code here end well i do this and it gave's me the same number of rows in database on gridlist but the rows are the same Link to comment
Gravestone Posted September 29, 2016 Share Posted September 29, 2016 (edited) -- server -- function sendRowsToClient() local result = dbPoll(dbQuery( connection, "SELECT * FROM `VList`", -1)) if result and type( result ) == 'table' then for i, v in ipairs(result) do triggerClientEvent(source, "onClientDataReceived", source, v.id, v.name, v.price) end end end -- client -- addEvent("onClientDataReceived", true) addEventHandler("onClientDataReceived", root, function(sID,sName,sPrice) local row = guiGridListAddRow ( PlayerVehicleList ) guiGridListSetItemText ( PlayerVehicleList, row, 1, sID, false, false ) guiGridListSetItemText ( PlayerVehicleList, row, 2, sName, false, false ) guiGridListSetItemText ( PlayerVehicleList, row, 3, sPrice, false, false ) end ) Try this, not tested Edited September 29, 2016 by Gravestone 1 Link to comment
HustraDev Posted September 29, 2016 Author Share Posted September 29, 2016 4 minutes ago, Gravestone said: -- server -- function sendRowsToClient() local result = dbPoll(dbQuery( connection, "SELECT * FROM `VList`", -1)) if result and type( result ) == 'table' then for i, v in ipairs(result) do triggerClientEvent(source, "SendRows", source, v.id, v.name, v.price) end end end -- client -- addEvent("onClientDataReceived", true) addEventHandler("onClientDataReceived", root, function(sID,sName,sPrice) local row = guiGridListAddRow ( PlayerVehicleList ) guiGridListSetItemText ( PlayerVehicleList, row, 1, sID, false, false ) guiGridListSetItemText ( PlayerVehicleList, row, 2, sName, false, false ) guiGridListSetItemText ( PlayerVehicleList, row, 3, sPrice, false, false ) end ) Try this, not tested thanks man i really really appreciate your help it works 100% Link to comment
pa3ck Posted September 29, 2016 Share Posted September 29, 2016 1 hour ago, Gravestone said: -- server --function sendRowsToClient() local result = dbPoll(dbQuery( connection, "SELECT * FROM `VList`", -1)) if result and type( result ) == 'table' then for i, v in ipairs(result) do triggerClientEvent(source, "onClientDataReceived", source, v.id, v.name, v.price) end endend -- client -- addEvent("onClientDataReceived", true) addEventHandler("onClientDataReceived", root, function(sID,sName,sPrice) local row = guiGridListAddRow ( PlayerVehicleList ) guiGridListSetItemText ( PlayerVehicleList, row, 1, sID, false, false ) guiGridListSetItemText ( PlayerVehicleList, row, 2, sName, false, false ) guiGridListSetItemText ( PlayerVehicleList, row, 3, sPrice, false, false ) end ) Try this, not tested It surely works, but it's terrible advice to use triggerClientEvent inside a loop for no reason. I mean, why can't you just send the whole table over and loop through it there? -- server function sendRowsToClient() local result = dbPoll(dbQuery( connection, "SELECT * FROM `VList`", -1)) if result and #result > 0 then triggerClientEvent(source, "SendRows", source, result) end end -- client -- addEvent("SendRows", true) addEventHandler("SendRows", root, function(tableData) local data = tableData for k, v in ipairs(data) do local row = guiGridListAddRow ( PlayerVehicleList ) guiGridListSetItemText ( PlayerVehicleList, row, 1, v["id"], false, false ) guiGridListSetItemText ( PlayerVehicleList, row, 2, v["name"], false, false ) guiGridListSetItemText ( PlayerVehicleList, row, 3, v["price"], false, false ) end end) 1 Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now