Php.Show is a small utility app that has been built with b4j as a php source code generator. One is able to define a table and its fields (currently text and int) and then Compile a single php file based on the table name that can be used in a web server.
NB: Latest update, 07 August 2016, ensures that the php scripts are protected from sql injections. also fixed some minor bugs.
I developed this after I needed some functionality for my b4i app to use with MySQL, however the generated php source can also be used with b4a apps with httputils. You can download the phpshow jar file from here. However the source has been included here. (Copy the phpshow.db database file to the Objects folder first)
Using the generated scripts can be done with httputils for example..
Calling executeremote...
Then on job is done...
For my example, I pass a map object which gets converted to a querystring and then encoded before it gets passed to the web server. On job done, I return the result as a list and loop through each item and process it. Each item is a map object.
Enjoy.
NB: Latest update, 07 August 2016, ensures that the php scripts are protected from sql injections. also fixed some minor bugs.
I developed this after I needed some functionality for my b4i app to use with MySQL, however the generated php source can also be used with b4a apps with httputils. You can download the phpshow jar file from here. However the source has been included here. (Copy the phpshow.db database file to the Objects folder first)
Using the generated scripts can be done with httputils for example..
B4X:
Sub ExecuteRemoteQuery(pQuery As Map, JobName As String)
Dim job As HttpJob
job.Initialize(JobName, Me)
Dim json As String = Map2QueryString(pQuery)
job.Download("http://www.myserver/anele.php?" & json)
End Sub
Sub Map2QueryString(sm As Map) As String
' convert a map to a querystring string
Dim SU As StringUtils
Dim iCnt As Int
Dim iTot As Int
Dim sb As StringBuilder
Dim mValue As String
sb.Initialize
' get size of map
iTot = sm.Size - 1
iCnt = 0
For Each mKey As String In sm.Keys
mValue = sm.Get(mKey)
mValue = SU.EncodeUrl(mValue, "UTF8")
sb.Append(mKey).Append(mValue)
If iCnt < iTot Then sb.Append("&")
iCnt = iCnt + 1
Next
Return sb.ToString
End Sub
Sub Json2List(jsonTxt As String) As List
Dim res As List
Dim parser As JSONParser
parser.Initialize(jsonTxt)
res = parser.NextArray
Return res
End Sub
Calling executeremote...
B4X:
dim uID as string = "a"
Dim lareas As Map
lareas.Initialize
lareas.Put("action=", "get")
lareas.Put("xx=", uID)
ExecuteRemoteQuery(lareas, "get")
Then on job is done...
B4X:
Sub JobDone (Job As HttpJob)
If Job.Success = True Then
Select Job.JobName
Case "get"
Dim lAreas As List = Json2List(Job.GetString)
Dim aTot As Int = lAreas.Size - 1
Dim aCnt As Int
Dim aMap As Map
Dim aid As String
Dim aname As String
Select Case aTot
Case -1
b4iMash.HideProgress
Msgbox("There are no records captured in the app.","No Areas")
Case Else
' loop through each record and load it to the listview
For aCnt = 0 To aTot
' get each area
aMap = lAreas.Get(aCnt)
aid = aMap.Get("field1")
aname = aMap.Get("field2")
AddTwoLinesAndBitmap2(lstAreas,aname,"", LoadBitmap(File.DirAssets, "areablue.png") , aid)
Next
lstAreas.ReloadAll
End Select
End Select
Else
Msgbox(Job.ErrorMessage, "Error")
End If
Job.Release
End Sub
For my example, I pass a map object which gets converted to a querystring and then encoded before it gets passed to the web server. On job done, I return the result as a list and loop through each item and process it. Each item is a map object.
Enjoy.
Attachments
Last edited: