Jump to content

Making a loop (MySQL)


Vivalavido

Recommended Posts

Hello,

I have a question bout making a loop in LUA, im expierenced in it with PHP, but not with LUA.

Heres how its done in PHP:

while($row = mysql_fetch_array( $result )) {
echo $row[subject];
echo "<br />";
}

But how am I suposed to do this in LUA?

I came this far:

local getSiteInfoDatabase = mysql_query( connect_mysql, "SELECT * FROM `messages` WHERE `to`='"..userName.."' " )
theReciever = mysql_fetch_assoc( getSiteInfoDatabase )
local subject = theReciever['title']

So I want to get all the data from theReciever[title], so I need a loop, but how?

Link to comment
for index,value in ipairs(table) do
-- actions
end

You can change index and value keys to whatever you like. They're just called index and value to show you what vars over there would contain.

Oh and don't forget to change table to an actual table.

Link to comment
for index,value in ipairs(table) do
-- actions
end

You can change index and value keys to whatever you like. They're just called index and value to show you what vars over there would contain.

Oh and don't forget to change table to an actual table.

So, I actually have to make a table first like:

local table = {}

Then I go further with:

local getSiteInfoDatabase = mysql_query( connect_mysql, "SELECT * FROM `messages` WHERE `to`='"..userName.."' " )
 
theReciever = mysql_fetch_assoc( getSiteInfoDatabase )l
subject = theReciever['title']
 
for theReciever,subject in ipairs(table) do
outputChatBox( subject )
end

I dont understand wich var's are necceserly at the for statement. Im not realy expierenced with loops :P

Link to comment
local getSiteInfoDatabase = mysql_query( connect_mysql, "SELECT * FROM `messages` WHERE `to`='"..userName.."' " )
 
theReciever = mysql_fetch_assoc( getSiteInfoDatabase )
--subject = theReciever['title']
 
for theReciever,subject in ipairs(theReciever) do
outputChatBox( subject['title'] )
end

Link to comment
local getSiteInfoDatabase = mysql_query( connect_mysql, "SELECT * FROM `messages` WHERE `to`='"..userName.."' " )
 
theReciever = mysql_fetch_assoc( getSiteInfoDatabase )
--subject = theReciever['title']
 
for theReciever,subject in ipairs(theReciever) do
outputChatBox( subject['title'] )
end

"bad argument #1 to 'ipairs' (table expected, got nil)

Doesnt it mean the table is empty? ( Because it isnt.. )

Link to comment

oh, sorry, wrong, i wasnt using mysql for some time, and i was writing it from memory :P

here's part of my script:

local sQuery = "SELECT cash FROM players WHERE login='"..accName.."' LIMIT 1"
local oResult = mysql_query(oDb, sQuery)
if (oResult) then
local numRows = mysql_num_rows(oResult)
if (numRows >= 1) then
while true do
local row = mysql_fetch_assoc(oResult)
if (not row) then break end
-- here you "start" getting values
outputChatBox(row['cash'])
end				
else
return nil
end
end

try this, modify it before;p

Link to comment
oh, sorry, wrong, i wasnt using mysql for some time, and i was writing it from memory :P

here's part of my script:

try this, modify it before;p

Okay, I have done that now. It works perfectly!

BUT: Only server sided it loops because look:

--SERVER:
local getSiteInfoDatabase = mysql_query( connect_mysql, "SELECT * FROM `messages` WHERE `to`='" .. userName .. "' " )
if (getSiteInfoDatabase) then
local numRows = mysql_num_rows(getSiteInfoDatabase)
if (numRows >= 1) then
while true do
local row = mysql_fetch_assoc(getSiteInfoDatabase)
if (not row) then break end
outputChatBox(row['title'])
               subject = row['title']
end				
else
return nil
end
end

Now what I do is make a var off the row['title] in subject.

triggerClientEvent( theUser, "openTheUserPanel", getRootElement(), loginName, class, serial, skin, countMessage, unReadMessage, subject )

Im sending that too my clientsided GUI!

GUI:

pmList = guiCreateGridList ( 0.03, 0.21, 0.93, 0.24, true, mainWindowUserPM )
pmColumn1 = guiGridListAddColumn( pmList, "Subject", 0.31 )
pmColumn2 = guiGridListAddColumn( pmList, "From", 0.31 )
pmColumn3 = guiGridListAddColumn( pmList, "Date", 0.31 )
pmRow = guiGridListAddRow( pmList )
guiGridListSetItemText( pmList, pmRow, pmColumn1, subject, false, false )

Still, it only displays one item in the gridlist whilst I have more items to be dsiplayed!

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