• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

TFS 0.X [OTX 1.X] Insert tables with different ID

elnelson

Lunaria World Dev
Joined
Jun 20, 2009
Messages
579
Solutions
2
Reaction score
58
Location
México
Hello, im trying to get this table result:

Lua:
Pending to retrieve:
[ID: 75985 | Name: None]
1 - plate armor
[ID: 75999 | Name: None]
 100 - crystal coin
[ID: 76483 | Name: None]
 1 - knight armor
 1 - boots of haste

with this code:
Lua:
        local text = ""
        local str = ""
        local check_id = db.getResult("SELECT `pid2`, `did`, `itemid`, `count`,`city`, `payment` FROM `player_tradeitems` WHERE `pid` = " .. getPlayerGUID(cid) .. ";")
        if (check_id:getID() ~= -1) then
            while (true) do
                local receiver = check_id:getDataInt("pid2")
                local did = check_id:getDataInt("did")
                local name = check_id:getDataInt("itemid")
                local count = check_id:getDataInt("count")
                local city = check_id:getDataInt("city")
                local payment = check_id:getDataInt("payment")
                str =  str.." "..count.." - "..getItemNameById(tonumber(name)).."\n"
                text = text.."[ID: "..did.." | Name: None]\n"..str
                if not(check_id:next()) then
                    break
                end
            end
        end
doShowTextDialog(cid, 2334, "Insert transaction ID to reclaim:\n\nTransactions:\nPending to retrieve:\n"..text.."")


But its driving me crazy trying to get the correct format, i get this result:

Code:
Pending to retrieve:
[ID: 75985 | Name: None]
1 - plate armor
[ID: 75999 | Name: None]
1 - plate armor
 100 - crystal coin
[ID: 76483 | Name: None]
1 - plate armor
 100 - crystal coin
 1 - knight armor
[ID: 76483 | Name: None]
1 - plate armor
 100 - crystal coin
 1 - knight armor
 1 - boots of haste

It is not correct, its merging all the existing items from table, anyone know how to solve this?

My table in PhPMyAdmin looks like this:
"did" is the id i want to separate in groups like i said on first example.
1652839189519.png
im using OTX 1.0.X.S - 4 (GitHub - mattyx14/otxserver: OTX Server from: (https://github.com/mattyx14/otxserver/))
 
Last edited:
Solution
Bring up my post please? :^)

I'd try something like this.
Store the last did, and check if it's the same.
If yes, then concat string.
If no, then over-write string?

idk, the entire loop looks weird to me.

Lua:
local text = ""
local str = ""
local last_did
local check_id = db.getResult("SELECT `pid2`, `did`, `itemid`, `count`,`city`, `payment` FROM `player_tradeitems` WHERE `pid` = " .. getPlayerGUID(cid) .. ";")
if (check_id:getID() ~= -1) then
    while (true) do
        local receiver = check_id:getDataInt("pid2")
        local did = check_id:getDataInt("did")
        local name = check_id:getDataInt("itemid")
        local count = check_id:getDataInt("count")
        local city = check_id:getDataInt("city")...
Bring up my post please? :^)

I'd try something like this.
Store the last did, and check if it's the same.
If yes, then concat string.
If no, then over-write string?

idk, the entire loop looks weird to me.

Lua:
local text = ""
local str = ""
local last_did
local check_id = db.getResult("SELECT `pid2`, `did`, `itemid`, `count`,`city`, `payment` FROM `player_tradeitems` WHERE `pid` = " .. getPlayerGUID(cid) .. ";")
if (check_id:getID() ~= -1) then
    while (true) do
        local receiver = check_id:getDataInt("pid2")
        local did = check_id:getDataInt("did")
        local name = check_id:getDataInt("itemid")
        local count = check_id:getDataInt("count")
        local city = check_id:getDataInt("city")
        local payment = check_id:getDataInt("payment")
        if last_did == did then
            str = str.." "..count.." - "..getItemNameById(tonumber(name)).."\n"
        else
            str = count.." - "..getItemNameById(tonumber(name)).."\n"
        end
        last_did = did
        text = text.."[ID: "..did.." | Name: None]\n"..str
        if not(check_id:next()) then
            break
        end
    end
end
doShowTextDialog(cid, 2334, "Insert transaction ID to reclaim:\n\nTransactions:\nPending to retrieve:\n"..text.."")

I feel like text shouldn't be updated until all did's have been accounted for.
I'd probably throw everything into a table, and then loop through the table to create a proper string.
Post automatically merged:

I feel like text shouldn't be updated until all did's have been accounted for.
I'd probably throw everything into a table, and then loop through the table to create a proper string.

Continuing thought from above...

I'd do it like this
(you might need to convert did using tonumber, to make it go into the table properly. idk what getDataInt actually gives us)
Lua:
local text = "There is nothing pending to retrieve."

local check_id = db.getResult("SELECT `pid2`, `did`, `itemid`, `count`,`city`, `payment` FROM `player_tradeitems` WHERE `pid` = " .. getPlayerGUID(cid) .. ";")
if (check_id:getID() ~= -1) then

    local results = {}
   
    while (true) do
        local receiver = check_id:getDataInt("pid2")
        local did = check_id:getDataInt("did")
        local name = check_id:getDataInt("itemid")
        local count = check_id:getDataInt("count")
        local city = check_id:getDataInt("city")
        local payment = check_id:getDataInt("payment")
       
        -- create temporary table
        if not results[did] then
            results[did] = {receiver = receiver, items = {{name = name, count = count}}, city = city, payment = payment}
        else
            table.insert(results[did].items, {name = name, count = count})
        end
        if not(check_id:next()) then
            break
        end
    end
   
   
    -- actually form the string
    text = "Pending to retrieve:\n"
   
    for v, k in pairs(results) do
        local items = ""
        for i = 1, #k.items do
            items = items .. "\n    " .. k.items[i].count .. " - " .. getItemNameById(tonumber(k.items[i].name))
        end
        text = text .. "\n[ID: " .. v .. " | Name: None]" .. items
    end
   
end
doShowTextDialog(cid, 2334, text)
 
Last edited:
Solution
I'd try something like this.
Store the last did, and check if it's the same.
If yes, then concat string.
If no, then over-write string?

idk, the entire loop looks weird to me.

Lua:
local text = ""
local str = ""
local last_did
local check_id = db.getResult("SELECT `pid2`, `did`, `itemid`, `count`,`city`, `payment` FROM `player_tradeitems` WHERE `pid` = " .. getPlayerGUID(cid) .. ";")
if (check_id:getID() ~= -1) then
    while (true) do
        local receiver = check_id:getDataInt("pid2")
        local did = check_id:getDataInt("did")
        local name = check_id:getDataInt("itemid")
        local count = check_id:getDataInt("count")
        local city = check_id:getDataInt("city")
        local payment = check_id:getDataInt("payment")
        if last_did == did then
            str = str.." "..count.." - "..getItemNameById(tonumber(name)).."\n"
        else
            str = count.." - "..getItemNameById(tonumber(name)).."\n"
        end
        last_did = did
        text = text.."[ID: "..did.." | Name: None]\n"..str
        if not(check_id:next()) then
            break
        end
    end
end
doShowTextDialog(cid, 2334, "Insert transaction ID to reclaim:\n\nTransactions:\nPending to retrieve:\n"..text.."")

I feel like text shouldn't be updated until all did's have been accounted for.
I'd probably throw everything into a table, and then loop through the table to create a proper string.
Post automatically merged:



Continuing thought from above...

I'd do it like this
(you might need to convert did using tonumber, to make it go into the table properly. idk what getDataInt actually gives us)
Lua:
local text = "There is nothing pending to retrieve."

local check_id = db.getResult("SELECT `pid2`, `did`, `itemid`, `count`,`city`, `payment` FROM `player_tradeitems` WHERE `pid` = " .. getPlayerGUID(cid) .. ";")
if (check_id:getID() ~= -1) then

    local results = {}
 
    while (true) do
        local receiver = check_id:getDataInt("pid2")
        local did = check_id:getDataInt("did")
        local name = check_id:getDataInt("itemid")
        local count = check_id:getDataInt("count")
        local city = check_id:getDataInt("city")
        local payment = check_id:getDataInt("payment")
     
        -- create temporary table
        if not results[did] then
            results[did] = {receiver = receiver, items = {{name = name, count = count}}, city = city, payment = payment}
        else
            table.insert(results[did].items, {name = name, count = count})
        end
        if not(check_id:next()) then
            break
        end
    end
 
 
    -- actually form the string
    text = "Pending to retrieve:\n"
 
    for v, k in pairs(results) do
        local items = ""
        for i = 1, #k.items do
            items = items .. "\n    " .. k.items[i].count .. " - " .. getItemNameById(tonumber(k.items[i].name))
        end
        text = text .. "\n[ID: " .. v .. " | Name: None]" .. items
    end
 
end
doShowTextDialog(cid, 2334, text)
The good old Xikini doing amazing stuff like always, take your like bro <3 <3
someday i will be as good as you ^^
 
Back
Top