SlideShare a Scribd company logo
Ring Documentation, Release 1.3
aColumnsNames = ["id","username","email"]
Func UpdateRecord
oModel.id = aPageVars[cRecID]
oModel.updatecolumn("username", aPageVars[:username] )
oModel.updatecolumn("email", aPageVars[:email] )
oView.UpdateView(self)
Class UsersView from ViewBase
oLanguage = new UsersLanguageEnglish
Func AddFuncScript oPage,oController
return oPage.scriptfunc("myadd",oPage.scriptredirection("ex26.ring"))
Func FormViewContent oController,oTranslation,oPage
return [
[oTranslation.aColumnsTitles[2],"textbox","username",
oController.oModel.UserName,oPage.stylewidth("100%")],
[oTranslation.aColumnsTitles[3],"textbox","email",
oController.oModel.Email,oPage.stylewidth("50%")]
]
Class UsersLanguageEnglish
cTitle = "Users Table"
cBack = "back"
aColumnsTitles = ["ID","User Name","Email"]
cOptions = "Options"
cSearch = "Search"
comboitems = ["Select Option...","Edit","Delete"]
cAddRecord = "Add Record"
cEditRecord = "Edit Record"
cRecordDeleted = "Record Deleted!"
aMovePages = ["First","Prev","Next","Last"]
cPage = "Page"
cOf = "of"
cRecordsCount = "Records Count"
cSave = "Save"
temp = new page
cTextAlign = temp.StyleTextRight()
cNoRecords = "No records!"
In the file ex25.ring we load ex25_users.ring then create an object from UsersController class.
Using the created object, we call the routing method.
#!c:ringbinring.exe -cgi
Load "weblib.ring"
Load "datalib.ring"
Load "ex25_users.ring"
Import System.Web
website = "ex25.ring"
New UsersController { Routing() }
Screen Shot:
42.21. Users registration and Login 308
Ring Documentation, Release 1.3
See the next code for the registration page
#!c:ringbinring.exe -cgi
Load "weblib.ring"
Load "datalib.ring"
Import System.Web
website = "ex26.ring"
new page {
boxstart()
text( "Register")
newline()
boxend()
divstart([:style = stylegradient(6) + stylesize("100%","95%") ])
link([ :url = website, :title = "back" , :style = stylecolor("white")])
newline()
divstart([ :style= styledivcenter("500","160") + stylegradient(52) ])
formpost("ex27.ring")
tablestart([ :Style = stylemarginleft("2%") + stylemargintop("2%") +
stylewidth("90%") ])
rowstart([])
cellstart([:style = stylewidth("20%") + styleheight(30)])
text("User Name")
cellend()
cellstart([ :style = stylewidth("80%") ])
textbox([:name = "username", :style = stylewidth("100%")])
cellend()
rowend()
rowstart([])
cellstart([ :Style = styleheight(30)])
text("Password")
cellend()
cellstart([])
textbox([:name = "password" , :type = "password"])
42.21. Users registration and Login 309
Ring Documentation, Release 1.3
cellend()
rowend()
rowstart([])
cellstart([ :style = styleheight(30)])
text("Email")
cellend()
cellstart([])
textbox([:name = "email" , :style = stylewidth("100%")])
cellend()
rowend()
rowstart([])
cellstart([ :style = styleheight(30)])
cellend()
cellstart([ :style = styleheight(30)])
submit([:value = "Register" ])
cellend()
rowend()
tableend()
formend()
divend()
divend()
}
Screen Shot:
The Registration response
#!c:ringbinring.exe -cgi
Load "weblib.ring"
Load "datalib.ring"
Load "ex25_users.ring"
42.21. Users registration and Login 310
Ring Documentation, Release 1.3
Import System.Web
oUser = new UsersModel
oUser.Connect()
if oUser.findwith("username",aPageVars["username"])
new page {
text("The user name is already registered")
}
return
ok
if oUser.findwith("email",aPageVars["email"])
new page {
text("This email is already registered")
}
return
ok
aPageVars["salt"] = str2hex(RandBytes(32))
aPageVars["pwhash"] = sha256(aPagevars["password"]+aPageVars["salt"])
aPageVars["sessionid"] = str2hex(randbytes(32))
oUser.Insert()
new page {
cookie("sessionid",aPageVars["sessionid"])
text("New User Created!")
newline()
text("User Name : " + aPageVars["username"])
newline()
}
oUser.Disconnect()
See the next code for the Login page
#!c:ringbinring.exe -cgi
Load "weblib.ring"
Load "datalib.ring"
Import System.Web
website = "ex28.ring"
new page {
boxstart()
text( "Login")
newline()
boxend()
divstart([:style = stylegradient(6) + stylesize("100%","95%") ])
link([ :url = website, :title = "back" , :style = stylecolor("white")])
newline()
divstart([ :style= styledivcenter("500","130") + stylegradient(52) ])
formpost("ex29.ring")
tablestart([ :Style = stylemarginleft("2%") + stylemargintop("2%") +
stylewidth("90%") ])
rowstart([])
cellstart([:style = stylewidth("20%") + styleheight(30)])
text("User Name")
cellend()
cellstart([ :style = stylewidth("80%") ])
textbox([:name = "username", :style = stylewidth("100%")])
cellend()
42.21. Users registration and Login 311
Ring Documentation, Release 1.3
rowend()
rowstart([])
cellstart([ :style = styleheight(30)])
text("Password")
cellend()
cellstart([])
textbox([:name = "password" , :type = "password"])
cellend()
rowend()
rowstart([])
cellstart([ :style = styleheight(30) ])
cellend()
cellstart([])
submit([:value = "Login" ])
cellend()
rowend()
tableend()
formend()
divend()
divend()
}
Screen Shot:
The response page
#!c:ringbinring.exe -cgi
Load "weblib.ring"
Load "datalib.ring"
Load "ex25_users.ring"
Import System.Web
42.21. Users registration and Login 312
Ring Documentation, Release 1.3
oUser = new UsersModel
oUser.Connect()
lResult = oUser.FindWith("username",aPageVars["username"])
new page {
if lResult
if sha256(aPagevars["password"]+oUser.Salt) = oUser.pwhash
text ("Correct Password!")
aPageVars["sessionid"] = str2hex(randbytes(32))
oUser.UpdateColumn("sessionid",aPageVars["sessionid"])
cookie("sessionid",aPageVars["sessionid"])
else
text ("Bad password!")
ok
else
text("Bad User Name!")
ok
}
oUser.Disconnect()
The next code for checking if the user needs to login or not
#!c:ringbinring.exe -cgi
Load "weblib.ring"
Load "datalib.ring"
Load "ex25_users.ring"
Import System.Web
oUser = new UsersModel
oUser.Connect()
lResult = oUser.FindWith("sessionid",aPageVars["sessionid"])
new page {
if lResult
text("User Name : " + oUser.username )
else
text("Please Login First!")
ok
}
oUser.Disconnect()
42.22 Database, ModelBase & ControllerBase classes
In this section we will see some code from datalib.ring
The next code presents the Database, ModelBase & ControllerBase classes
Import System.Web
Class Database
cServer = "localhost"
cUserName = "root"
cPassword = "root"
cDatabase = "mahdb"
Func Connect
42.22. Database, ModelBase & ControllerBase classes 313
Ring Documentation, Release 1.3
con = mysql_init()
mysql_connect(con, cServer, cUserName, cPassWord,cDatabase)
Func Disconnect
mysql_close(con)
Func Query cQuery
mysql_query(con,cQuery)
Func QueryResult
return mysql_result(con)
Func QueryResultWithColumns
# return columns names + query result
return mysql_result2(con)
Func QueryValue
aResult = mysql_result(con)
if islist(aResult) and len(aResult) >= 1
aResult = aResult[1]
if len(aResult) >= 1
return aResult[1]
ok
ok
return 0
Func EscapeString x
if isstring(x)
return MySQL_Escape_String(con,x)
else
return MySQL_Escape_String(con,string(x))
ok
Private
con = NULL
Class ModelBase from Database
cTableName = ""
cSearchColumn = "name"
aColumns = []
aQueryResult = []
ID = 0
# set table name from class name
classname = lower(classname(self))
if right(classname,5) = :model
cTablename = left(classname,len(classname)-5)
ok
Func Insert
cValues = ""
for x in aColumns
cValues += "'" + EscapeString(aPageVars[x]) + "',"
42.22. Database, ModelBase & ControllerBase classes 314
Ring Documentation, Release 1.3
Next
cValues = left(cValues,len(cValues)-1) # remove last comma
cColumns = ""
for x in aColumns
cColumns += x + ","
next
cColumns = left(cColumns,len(cColumns)-1)
query("insert into " + cTableName + "("+cColumns+") values (" +
cValues + ")" )
Func Update nID
cStr = ""
for x in aColumns
cStr += x + " = '" + EscapeString(aPageVars[x]) + "' , "
# the space after comma is necessary
Next
cStr = left(cStr,len(cStr)-2)
query("update " + cTableName + " set " + cStr + " where id = " + nID )
Func UpdateColumn cColumn,cValue
query("update " + cTableName + " set " + cColumn + " = '" +
EscapeString(cValue) + "' where id = " + self.ID )
Func Count cValue
query("SELECT count(*) FROM " + cTableName +
" where "+cSearchColumn+" like '" + EscapeString(cValue) + "%'")
return queryValue()
Func Read nStart,nRecordsPerPage
query("SELECT * FROM "+ cTableName+" limit " + EscapeString(nStart) + "," +
EscapeString(nRecordsPerPage) )
aQueryResult = queryResult()
Func Search cValue,nStart,nRecordsPerPage
query("SELECT * FROM "+ cTableName+" where "+cSearchColumn+" like '" +
EscapeString(cValue) + "%'" +
" limit " + EscapeString(nStart) + "," + EscapeString(nRecordsPerPage) )
aQueryResult = queryResult()
Func Find nID
query("select * from " + cTableName + " where id = " + EscapeString(nID) )
aResult = queryResult()[1]
# move the result from the array to the object attributes
ID = nID
cCode = ""
for x = 2 to len(aResult)
cCode += aColumns[x-1] + " = hex2str('" + str2hex(aResult[x]) + "')" + nl
next
eval(cCode)
Func FindWith cColumn,cValue
42.22. Database, ModelBase & ControllerBase classes 315
Ring Documentation, Release 1.3
query("select * from " + cTableName + " where "+cColumn+" = '" +
EscapeString(cValue) + "'" )
aResult = queryResult()
if len(aResult) > 0
aResult = aResult[1]
else
return 0
ok
# move the result from the array to the object attributes
ID = aResult[1]
cCode = ""
for x = 2 to len(aResult)
cCode += aColumns[x-1] + " = hex2str('" + str2hex(aResult[x]) + "')" + nl
next
eval(cCode)
return 1
Func Delete ID
query("delete from " + cTableName + " where id = " + EscapeString(ID) )
Func Clear
cCode = ""
for x in aColumns
cCode += x + ' = ""' + nl
next
eval(cCode)
Func LoadModel
# create the columns array
query("SELECT * FROM "+ cTableName + " limit 0,1")
aQueryResult = QueryResultWithColumns()[1]
for x = 2 to len(aQueryResult)
aColumns + lower(trim(aQueryResult[x]))
next
# create attribute for each column
for x in aColumns
addattribute(self,x)
next
Func Connect
Super.Connect()
if nLoadModel = 0
nLoadModel = 1
LoadModel()
ok
private
nLoadModel = 0
Class ControllerBase
42.22. Database, ModelBase & ControllerBase classes 316
Ring Documentation, Release 1.3
nRecordsPerPage = 5
nRecordsCount = 0
nPagesCount = 0
nActivePage = 0
# Dynamic creation of oView = new tablenameView and oModel = new tablename.Model
classname = lower(classname(self))
if right(classname,10) = :controller
tablename = left(classname,len(classname)-10)
cCode = "oView = new " + tablename+"View" + nl
cCode += "oModel = new " + tablename+"Model" + nl
eval(cCode)
oModel.connect()
ok
cSearchName = "searchname"
cPart = "part"
cPageError = "The page number is not correct"
cLast = "last"
cOperation = "operation"
cRecID = "recid"
aColumnsNames = ["id"]
for t in oModel.aColumns
aColumnsNames + t
next
cMainURL = website + "?"
func Routing
switch aPageVars[cOperation]
on NULL showtable()
on :add addrecord()
on :save saverecord()
on :delete deleterecord()
on :edit editrecord()
on :update updaterecord()
off
func ShowTable
nRecordsCount = oModel.Count( aPageVars[cSearchName] )
nPagesCount = ceil(nRecordsCount / nRecordsPerPage)
if aPageVars[cPart] = cLast
aPageVars[cPart] = string(nPagesCount)
ok
nActivePage = number(aPageVars[cPart])
if nActivePage = 0 nActivePage = 1 ok
if ( nActivePage > nPagesCount ) and nRecordsCount > 0
ErrorMsg(cPageError)
return
ok
42.22. Database, ModelBase & ControllerBase classes 317

More Related Content

What's hot (20)

PDF
The Ring programming language version 1.4.1 book - Part 13 of 31
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.5.2 book - Part 43 of 181
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.8 book - Part 49 of 202
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.5 book - Part 8 of 31
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.5.2 book - Part 29 of 181
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.4 book - Part 8 of 30
Mahmoud Samir Fayed
 
PDF
Sistema de ventas
DAYANA RETO
 
PDF
The Ring programming language version 1.5.1 book - Part 43 of 180
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.2 book - Part 18 of 84
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 47 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.9 book - Part 36 of 210
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.3 book - Part 20 of 88
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.2 book - Part 19 of 84
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.9 book - Part 46 of 210
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.5.4 book - Part 29 of 185
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.9 book - Part 52 of 210
Mahmoud Samir Fayed
 
PPTX
Rapid and Scalable Development with MongoDB, PyMongo, and Ming
Rick Copeland
 
PDF
The Ring programming language version 1.4.1 book - Part 8 of 31
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.3 book - Part 33 of 88
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.5.1 book - Part 42 of 180
Mahmoud Samir Fayed
 
The Ring programming language version 1.4.1 book - Part 13 of 31
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.2 book - Part 43 of 181
Mahmoud Samir Fayed
 
The Ring programming language version 1.8 book - Part 49 of 202
Mahmoud Samir Fayed
 
The Ring programming language version 1.5 book - Part 8 of 31
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.2 book - Part 29 of 181
Mahmoud Samir Fayed
 
The Ring programming language version 1.4 book - Part 8 of 30
Mahmoud Samir Fayed
 
Sistema de ventas
DAYANA RETO
 
The Ring programming language version 1.5.1 book - Part 43 of 180
Mahmoud Samir Fayed
 
The Ring programming language version 1.2 book - Part 18 of 84
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 47 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.9 book - Part 36 of 210
Mahmoud Samir Fayed
 
The Ring programming language version 1.3 book - Part 20 of 88
Mahmoud Samir Fayed
 
The Ring programming language version 1.2 book - Part 19 of 84
Mahmoud Samir Fayed
 
The Ring programming language version 1.9 book - Part 46 of 210
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.4 book - Part 29 of 185
Mahmoud Samir Fayed
 
The Ring programming language version 1.9 book - Part 52 of 210
Mahmoud Samir Fayed
 
Rapid and Scalable Development with MongoDB, PyMongo, and Ming
Rick Copeland
 
The Ring programming language version 1.4.1 book - Part 8 of 31
Mahmoud Samir Fayed
 
The Ring programming language version 1.3 book - Part 33 of 88
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.1 book - Part 42 of 180
Mahmoud Samir Fayed
 

Similar to The Ring programming language version 1.3 book - Part 34 of 88 (20)

PDF
The Ring programming language version 1.5.3 book - Part 54 of 184
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.5.2 book - Part 44 of 181
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.3 book - Part 50 of 88
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.5.3 book - Part 77 of 184
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.7 book - Part 41 of 196
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.4 book - Part 18 of 30
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.9 book - Part 51 of 210
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.3 book - Part 35 of 88
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.5.3 book - Part 29 of 184
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.4.1 book - Part 12 of 31
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.8 book - Part 46 of 202
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.3 book - Part 31 of 88
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.5.3 book - Part 52 of 184
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.5.3 book - Part 42 of 184
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.5.4 book - Part 40 of 185
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.6 book - Part 45 of 189
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.5.3 book - Part 40 of 184
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.7 book - Part 47 of 196
Mahmoud Samir Fayed
 
PDF
Using Ruby on Rails with legacy Oracle databases
Raimonds Simanovskis
 
PDF
The Ring programming language version 1.8 book - Part 43 of 202
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.3 book - Part 54 of 184
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.2 book - Part 44 of 181
Mahmoud Samir Fayed
 
The Ring programming language version 1.3 book - Part 50 of 88
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.3 book - Part 77 of 184
Mahmoud Samir Fayed
 
The Ring programming language version 1.7 book - Part 41 of 196
Mahmoud Samir Fayed
 
The Ring programming language version 1.4 book - Part 18 of 30
Mahmoud Samir Fayed
 
The Ring programming language version 1.9 book - Part 51 of 210
Mahmoud Samir Fayed
 
The Ring programming language version 1.3 book - Part 35 of 88
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.3 book - Part 29 of 184
Mahmoud Samir Fayed
 
The Ring programming language version 1.4.1 book - Part 12 of 31
Mahmoud Samir Fayed
 
The Ring programming language version 1.8 book - Part 46 of 202
Mahmoud Samir Fayed
 
The Ring programming language version 1.3 book - Part 31 of 88
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.3 book - Part 52 of 184
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.3 book - Part 42 of 184
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.4 book - Part 40 of 185
Mahmoud Samir Fayed
 
The Ring programming language version 1.6 book - Part 45 of 189
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.3 book - Part 40 of 184
Mahmoud Samir Fayed
 
The Ring programming language version 1.7 book - Part 47 of 196
Mahmoud Samir Fayed
 
Using Ruby on Rails with legacy Oracle databases
Raimonds Simanovskis
 
The Ring programming language version 1.8 book - Part 43 of 202
Mahmoud Samir Fayed
 
Ad

More from Mahmoud Samir Fayed (20)

PDF
The Ring programming language version 1.10 book - Part 212 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 211 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 210 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 208 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 207 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 205 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 206 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 204 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 203 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 202 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 201 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 200 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 199 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 198 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 197 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 196 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 195 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 194 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 193 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 192 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 212 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 211 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 210 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 208 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 207 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 205 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 206 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 204 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 203 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 202 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 201 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 200 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 199 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 198 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 197 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 196 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 195 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 194 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 193 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 192 of 212
Mahmoud Samir Fayed
 
Ad

Recently uploaded (20)

PPTX
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
PDF
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
PDF
SIZING YOUR AIR CONDITIONER---A PRACTICAL GUIDE.pdf
Muhammad Rizwan Akram
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
AI Agents in the Cloud: The Rise of Agentic Cloud Architecture
Lilly Gracia
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
PPTX
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
PDF
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PPTX
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
PDF
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
PDF
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
PDF
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
DOCX
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PPTX
Agentforce World Tour Toronto '25 - Supercharge MuleSoft Development with Mod...
Alexandra N. Martinez
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PPTX
Digital Circuits, important subject in CS
contactparinay1
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
SIZING YOUR AIR CONDITIONER---A PRACTICAL GUIDE.pdf
Muhammad Rizwan Akram
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
AI Agents in the Cloud: The Rise of Agentic Cloud Architecture
Lilly Gracia
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
Agentforce World Tour Toronto '25 - Supercharge MuleSoft Development with Mod...
Alexandra N. Martinez
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
Digital Circuits, important subject in CS
contactparinay1
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 

The Ring programming language version 1.3 book - Part 34 of 88

  • 1. Ring Documentation, Release 1.3 aColumnsNames = ["id","username","email"] Func UpdateRecord oModel.id = aPageVars[cRecID] oModel.updatecolumn("username", aPageVars[:username] ) oModel.updatecolumn("email", aPageVars[:email] ) oView.UpdateView(self) Class UsersView from ViewBase oLanguage = new UsersLanguageEnglish Func AddFuncScript oPage,oController return oPage.scriptfunc("myadd",oPage.scriptredirection("ex26.ring")) Func FormViewContent oController,oTranslation,oPage return [ [oTranslation.aColumnsTitles[2],"textbox","username", oController.oModel.UserName,oPage.stylewidth("100%")], [oTranslation.aColumnsTitles[3],"textbox","email", oController.oModel.Email,oPage.stylewidth("50%")] ] Class UsersLanguageEnglish cTitle = "Users Table" cBack = "back" aColumnsTitles = ["ID","User Name","Email"] cOptions = "Options" cSearch = "Search" comboitems = ["Select Option...","Edit","Delete"] cAddRecord = "Add Record" cEditRecord = "Edit Record" cRecordDeleted = "Record Deleted!" aMovePages = ["First","Prev","Next","Last"] cPage = "Page" cOf = "of" cRecordsCount = "Records Count" cSave = "Save" temp = new page cTextAlign = temp.StyleTextRight() cNoRecords = "No records!" In the file ex25.ring we load ex25_users.ring then create an object from UsersController class. Using the created object, we call the routing method. #!c:ringbinring.exe -cgi Load "weblib.ring" Load "datalib.ring" Load "ex25_users.ring" Import System.Web website = "ex25.ring" New UsersController { Routing() } Screen Shot: 42.21. Users registration and Login 308
  • 2. Ring Documentation, Release 1.3 See the next code for the registration page #!c:ringbinring.exe -cgi Load "weblib.ring" Load "datalib.ring" Import System.Web website = "ex26.ring" new page { boxstart() text( "Register") newline() boxend() divstart([:style = stylegradient(6) + stylesize("100%","95%") ]) link([ :url = website, :title = "back" , :style = stylecolor("white")]) newline() divstart([ :style= styledivcenter("500","160") + stylegradient(52) ]) formpost("ex27.ring") tablestart([ :Style = stylemarginleft("2%") + stylemargintop("2%") + stylewidth("90%") ]) rowstart([]) cellstart([:style = stylewidth("20%") + styleheight(30)]) text("User Name") cellend() cellstart([ :style = stylewidth("80%") ]) textbox([:name = "username", :style = stylewidth("100%")]) cellend() rowend() rowstart([]) cellstart([ :Style = styleheight(30)]) text("Password") cellend() cellstart([]) textbox([:name = "password" , :type = "password"]) 42.21. Users registration and Login 309
  • 3. Ring Documentation, Release 1.3 cellend() rowend() rowstart([]) cellstart([ :style = styleheight(30)]) text("Email") cellend() cellstart([]) textbox([:name = "email" , :style = stylewidth("100%")]) cellend() rowend() rowstart([]) cellstart([ :style = styleheight(30)]) cellend() cellstart([ :style = styleheight(30)]) submit([:value = "Register" ]) cellend() rowend() tableend() formend() divend() divend() } Screen Shot: The Registration response #!c:ringbinring.exe -cgi Load "weblib.ring" Load "datalib.ring" Load "ex25_users.ring" 42.21. Users registration and Login 310
  • 4. Ring Documentation, Release 1.3 Import System.Web oUser = new UsersModel oUser.Connect() if oUser.findwith("username",aPageVars["username"]) new page { text("The user name is already registered") } return ok if oUser.findwith("email",aPageVars["email"]) new page { text("This email is already registered") } return ok aPageVars["salt"] = str2hex(RandBytes(32)) aPageVars["pwhash"] = sha256(aPagevars["password"]+aPageVars["salt"]) aPageVars["sessionid"] = str2hex(randbytes(32)) oUser.Insert() new page { cookie("sessionid",aPageVars["sessionid"]) text("New User Created!") newline() text("User Name : " + aPageVars["username"]) newline() } oUser.Disconnect() See the next code for the Login page #!c:ringbinring.exe -cgi Load "weblib.ring" Load "datalib.ring" Import System.Web website = "ex28.ring" new page { boxstart() text( "Login") newline() boxend() divstart([:style = stylegradient(6) + stylesize("100%","95%") ]) link([ :url = website, :title = "back" , :style = stylecolor("white")]) newline() divstart([ :style= styledivcenter("500","130") + stylegradient(52) ]) formpost("ex29.ring") tablestart([ :Style = stylemarginleft("2%") + stylemargintop("2%") + stylewidth("90%") ]) rowstart([]) cellstart([:style = stylewidth("20%") + styleheight(30)]) text("User Name") cellend() cellstart([ :style = stylewidth("80%") ]) textbox([:name = "username", :style = stylewidth("100%")]) cellend() 42.21. Users registration and Login 311
  • 5. Ring Documentation, Release 1.3 rowend() rowstart([]) cellstart([ :style = styleheight(30)]) text("Password") cellend() cellstart([]) textbox([:name = "password" , :type = "password"]) cellend() rowend() rowstart([]) cellstart([ :style = styleheight(30) ]) cellend() cellstart([]) submit([:value = "Login" ]) cellend() rowend() tableend() formend() divend() divend() } Screen Shot: The response page #!c:ringbinring.exe -cgi Load "weblib.ring" Load "datalib.ring" Load "ex25_users.ring" Import System.Web 42.21. Users registration and Login 312
  • 6. Ring Documentation, Release 1.3 oUser = new UsersModel oUser.Connect() lResult = oUser.FindWith("username",aPageVars["username"]) new page { if lResult if sha256(aPagevars["password"]+oUser.Salt) = oUser.pwhash text ("Correct Password!") aPageVars["sessionid"] = str2hex(randbytes(32)) oUser.UpdateColumn("sessionid",aPageVars["sessionid"]) cookie("sessionid",aPageVars["sessionid"]) else text ("Bad password!") ok else text("Bad User Name!") ok } oUser.Disconnect() The next code for checking if the user needs to login or not #!c:ringbinring.exe -cgi Load "weblib.ring" Load "datalib.ring" Load "ex25_users.ring" Import System.Web oUser = new UsersModel oUser.Connect() lResult = oUser.FindWith("sessionid",aPageVars["sessionid"]) new page { if lResult text("User Name : " + oUser.username ) else text("Please Login First!") ok } oUser.Disconnect() 42.22 Database, ModelBase & ControllerBase classes In this section we will see some code from datalib.ring The next code presents the Database, ModelBase & ControllerBase classes Import System.Web Class Database cServer = "localhost" cUserName = "root" cPassword = "root" cDatabase = "mahdb" Func Connect 42.22. Database, ModelBase & ControllerBase classes 313
  • 7. Ring Documentation, Release 1.3 con = mysql_init() mysql_connect(con, cServer, cUserName, cPassWord,cDatabase) Func Disconnect mysql_close(con) Func Query cQuery mysql_query(con,cQuery) Func QueryResult return mysql_result(con) Func QueryResultWithColumns # return columns names + query result return mysql_result2(con) Func QueryValue aResult = mysql_result(con) if islist(aResult) and len(aResult) >= 1 aResult = aResult[1] if len(aResult) >= 1 return aResult[1] ok ok return 0 Func EscapeString x if isstring(x) return MySQL_Escape_String(con,x) else return MySQL_Escape_String(con,string(x)) ok Private con = NULL Class ModelBase from Database cTableName = "" cSearchColumn = "name" aColumns = [] aQueryResult = [] ID = 0 # set table name from class name classname = lower(classname(self)) if right(classname,5) = :model cTablename = left(classname,len(classname)-5) ok Func Insert cValues = "" for x in aColumns cValues += "'" + EscapeString(aPageVars[x]) + "'," 42.22. Database, ModelBase & ControllerBase classes 314
  • 8. Ring Documentation, Release 1.3 Next cValues = left(cValues,len(cValues)-1) # remove last comma cColumns = "" for x in aColumns cColumns += x + "," next cColumns = left(cColumns,len(cColumns)-1) query("insert into " + cTableName + "("+cColumns+") values (" + cValues + ")" ) Func Update nID cStr = "" for x in aColumns cStr += x + " = '" + EscapeString(aPageVars[x]) + "' , " # the space after comma is necessary Next cStr = left(cStr,len(cStr)-2) query("update " + cTableName + " set " + cStr + " where id = " + nID ) Func UpdateColumn cColumn,cValue query("update " + cTableName + " set " + cColumn + " = '" + EscapeString(cValue) + "' where id = " + self.ID ) Func Count cValue query("SELECT count(*) FROM " + cTableName + " where "+cSearchColumn+" like '" + EscapeString(cValue) + "%'") return queryValue() Func Read nStart,nRecordsPerPage query("SELECT * FROM "+ cTableName+" limit " + EscapeString(nStart) + "," + EscapeString(nRecordsPerPage) ) aQueryResult = queryResult() Func Search cValue,nStart,nRecordsPerPage query("SELECT * FROM "+ cTableName+" where "+cSearchColumn+" like '" + EscapeString(cValue) + "%'" + " limit " + EscapeString(nStart) + "," + EscapeString(nRecordsPerPage) ) aQueryResult = queryResult() Func Find nID query("select * from " + cTableName + " where id = " + EscapeString(nID) ) aResult = queryResult()[1] # move the result from the array to the object attributes ID = nID cCode = "" for x = 2 to len(aResult) cCode += aColumns[x-1] + " = hex2str('" + str2hex(aResult[x]) + "')" + nl next eval(cCode) Func FindWith cColumn,cValue 42.22. Database, ModelBase & ControllerBase classes 315
  • 9. Ring Documentation, Release 1.3 query("select * from " + cTableName + " where "+cColumn+" = '" + EscapeString(cValue) + "'" ) aResult = queryResult() if len(aResult) > 0 aResult = aResult[1] else return 0 ok # move the result from the array to the object attributes ID = aResult[1] cCode = "" for x = 2 to len(aResult) cCode += aColumns[x-1] + " = hex2str('" + str2hex(aResult[x]) + "')" + nl next eval(cCode) return 1 Func Delete ID query("delete from " + cTableName + " where id = " + EscapeString(ID) ) Func Clear cCode = "" for x in aColumns cCode += x + ' = ""' + nl next eval(cCode) Func LoadModel # create the columns array query("SELECT * FROM "+ cTableName + " limit 0,1") aQueryResult = QueryResultWithColumns()[1] for x = 2 to len(aQueryResult) aColumns + lower(trim(aQueryResult[x])) next # create attribute for each column for x in aColumns addattribute(self,x) next Func Connect Super.Connect() if nLoadModel = 0 nLoadModel = 1 LoadModel() ok private nLoadModel = 0 Class ControllerBase 42.22. Database, ModelBase & ControllerBase classes 316
  • 10. Ring Documentation, Release 1.3 nRecordsPerPage = 5 nRecordsCount = 0 nPagesCount = 0 nActivePage = 0 # Dynamic creation of oView = new tablenameView and oModel = new tablename.Model classname = lower(classname(self)) if right(classname,10) = :controller tablename = left(classname,len(classname)-10) cCode = "oView = new " + tablename+"View" + nl cCode += "oModel = new " + tablename+"Model" + nl eval(cCode) oModel.connect() ok cSearchName = "searchname" cPart = "part" cPageError = "The page number is not correct" cLast = "last" cOperation = "operation" cRecID = "recid" aColumnsNames = ["id"] for t in oModel.aColumns aColumnsNames + t next cMainURL = website + "?" func Routing switch aPageVars[cOperation] on NULL showtable() on :add addrecord() on :save saverecord() on :delete deleterecord() on :edit editrecord() on :update updaterecord() off func ShowTable nRecordsCount = oModel.Count( aPageVars[cSearchName] ) nPagesCount = ceil(nRecordsCount / nRecordsPerPage) if aPageVars[cPart] = cLast aPageVars[cPart] = string(nPagesCount) ok nActivePage = number(aPageVars[cPart]) if nActivePage = 0 nActivePage = 1 ok if ( nActivePage > nPagesCount ) and nRecordsCount > 0 ErrorMsg(cPageError) return ok 42.22. Database, ModelBase & ControllerBase classes 317