php / XAMPP

Just starting out? Need help? Post your questions and find answers here.
JoeConnectALL
Posts: 3
Joined: Thu Mar 17, 2022 3:35 am

php / XAMPP

Post by JoeConnectALL »

I'm not sure how to describe this... I'd like to post an example but there is too much 'backend' to set up a reasonable working simulation of my issue.

My front end SpiderBasic app is talking to a MySQL server hosted on another local box via XAMPP using php scripts. I can call the php scripts with HTTPReqest, my JSON comes back fine, - I can parse it just fine and dump to ListIcon- all the data is present EXCEPT data from a second call I make to fill in the rest of the structure.
Correction- the data does indeed 'get there' and the structure IS filled but the ListIcon grid is displayed before the structure is populated with data from specific columns from the JSON array. That data comes from the second HttpRequest. The first request, fills the data from the first part of the stucture (baseinfo), then the send HTTP call gets the second structure (eduinfo). The eduinfo finally gets there but the grid is already displayed even though I called my DisplayGrid() proc after the last HTTPrequest.
Below are the strutures I'm referring to:

Ary_employees\eduinfo\GPA - that's part of the struct that doesn't display UNTIL I to a refresh - which for now- I put a button on the form and just hit REFRESH and the column shows up- I don't want my users having to do this- no way.
I'm know this has something to do with some type of async / background task issue . I've tried setting up a simple global flag as a 'signal' to do a refresh on the grid- the flag variable doesn't get touched while the JSON is being parsed/processed.

I know you guys probably need more than this to really break it down- I'll see what I can come up with.
Thanks to all- I really like this forum- I just wish more people would 'discover' SpiderBasic. I can't believe how much I can do with very little code. And it's fast :)
Anyway- any help would be appreciated. - thanks again.

Code: Select all

Global Dim Ary_employees.struct_employees(400)

Structure struct_eduInfo
    empID.s
    school.s
    gradDate.s
    GPA.f
    major.s
    minor.s
    cert.s  
 
EndStructure



Structure struct_baseInfo
    empID.s
    lastname.s
    firstname.s
    stAddress1.s
    stAddress2.s
    city.s
    state.s
    zipcode.s
    ssn.s
    dob.s
    phone1.s
    phone2.s
    email.s
    
EndStructure




Structure struct_employees
    empID.s
    baseinfo.struct_baseinfo
    education.struct_eduInfo
    
EndStructure
User avatar
Peter
Posts: 1086
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: php / XAMPP

Post by Peter »

JoeConnectALL wrote:[...] but the ListIcon grid is displayed before the structure is populate [...]
HttpRequests are asynchronous. Here is a small example of how you could build your process:

Code: Select all

Procedure DisplayGrid()
  ; ...  
EndProcedure

Procedure SecondRequestCallback()
  ; ...
  DisplayGrid()
EndProcedure

Procedure SecondRequest()
  HTTPRequest(..., SecondRequestCallback()
EndProcedure

Procedure FirstRequestCallback()
  ; ...
  SecondRequest()
EndProcedure

Procedure FirstRequest()
  HTTPRequest(..., FirstRequestCallback()
EndProcedure

FirstRequest()
JoeConnectALL
Posts: 3
Joined: Thu Mar 17, 2022 3:35 am

Re: php / XAMPP

Post by JoeConnectALL »

Peter - thank you for responding so quickly. As far as chaining HTTPrequests; I understand what you are talking about but in my case; it's not as simple because the second request needs to get called once for every record from the first request. The first request picks up an array of about 400 records; the records then get iterated through upon which each iteration; the second request is called.

BUT... YOU got me thinking... I ended up solving the problem by using a global variable as a 'signal'. I tried this before but it didn't work. I put the signal in the wrong spot- base on your reply- the 'signal' to the tell the second HTTPrequest that the last record is being sent has to be shut off by the second request itself. That's why my first attempt didn't work - I didn't have anything in the second request to reset the flag.


Again - THANK YOU for your response AND for pointing me in a better direction.



Below is what I ended up with:
I got a working version to give you an idea of what I'm doing;



https://jfmmacpro.connectallnh.com:1344 ... n_lab.html




(the above address is only reachable from a USA/Canada ip)
CORS friendly :)

Peter- again, many thanks.



Code: Select all

Global phpidx = -1  
Global recsExpected = -1 ; flag for HTTPrequests





; *********************************************************************
Procedure phpGetEduEvent(Success, Result.s, UserData)
    phpidx = UserData

    If Success
        ProcessJSON_eduInfo(Result)
        If phpidx >= 0 
            Ary_employees(phpidx)\education = Ary_eduInfo(0)
           
        Else
            Display_eduInfo()
            
        EndIf
        
    Else
        Debug "PHP data acquisition error"
        
    EndIf
    
    If phpidx >= recsExpected - 1 
        PopulateGrid_Main(-1)
        DisableEnableButtons(#enable)
        
    EndIf
   
    phpidx = -1
    
EndProcedure  ; (phpGetEduEventCallback)





; ************************************************************************
Procedure GetPHP_eduInfo(empID.s)
    
    Define hostAddress.s
    Define phpFile.s, suffix.s
    Define phpRequest.s
    Define userdata.i = phpidx
    

    hostAddress = #hostAddress
    phpFile = "getEduInfo.php"
    suffix = "?" + "empID=" + empID
    phpRequest = hostAddress + "Employees" + "/" + phpFile + suffix
    

    HTTPRequest(#PB_HTTP_Get, phpRequest, "", @phpGetEduEvent(), UserData)

    
EndProcedure ; (GetPHP_eduinfo)






; **********************************************************************
Procedure phpGetBaseEvent(Success, Result.s, UserData)

  
    If Success
        ProcessJSON_baseInfo(Result)
        Build_employees()

       
    Else
        Debug "PHP data acquisition error"
        
    EndIf
    
    
   phpidx = -1
    
EndProcedure  ; (phpGet_baseInfoCallback)




; *******************************************************************
Procedure GetPHP_baseinfo(empID.s = "*ALL*")
    
    Define hostAddress.s
    Define phpFile.s, suffix.s
    Define phpRequest.s
    Define userdata.i = -1
    

    

    hostAddress = #hostAddress
    phpFile = "getBaseInfo.php"
    suffix = "?" + "empID=" + empID
    phpRequest = hostAddress + "Employees" + "/" + phpFile + suffix
    
    
    HTTPRequest(#PB_HTTP_Get, phpRequest, "", @phpGetBaseEvent(), userdata)
    
          
EndProcedure  ; (GetPHP_baseinfo)





; *********************************************************************************************
Procedure Build_employees()
    
    Debug "Building employees..."
    Define row.i, numberofItems.i
    Define baseinfo.struct_baseinfo
    Define eduinfo.struct_eduInfo
    Define empID.s
    
    
    
    numberOfItems = ArraySize(Ary_baseinfo())
    ReDim Ary_employees.struct_employees(numberofItems)

    Debug "numberofItems: " + Str(numberOfItems)
    recsExpected = numberofItems
    

    For row = 0 To numberofItems - 1
        
        baseinfo = Ary_baseinfo(row)
        empID = baseinfo\empID

        

        Ary_employees(row)\baseinfo = baseinfo
        Ary_employees(row)\empID = empID
        
   
        
        phpidx = row  ; global var so GetPHP_eduinfo knows where to put the record
        GetPHP_eduInfo(empID)

        
    Next row
    
   
    Debug "Done building employees"
    
     
EndProcedure ;Build_employees
Last edited by JoeConnectALL on Thu Mar 17, 2022 10:39 pm, edited 3 times in total.
User avatar
Peter
Posts: 1086
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: php / XAMPP

Post by Peter »

JoeConnectALL wrote:The first request picks up an array of about 400 records; the records then get iterated through upon which each iteration; the second request is called.
You could also think about assembling the complete structure on the PHP side in one go. Then you could retrieve the data with only one HttpRequest() and read it out using ExtractJSONStructure().
JoeConnectALL
Posts: 3
Joined: Thu Mar 17, 2022 3:35 am

Re: php / XAMPP

Post by JoeConnectALL »

Peter- you're right... as usual. What I'm doing now is what the php script should be doing- you are indeed correct. I'm essentially doing the pseudo inner joins manually when MySQL can do it. Of course- MYSQL can do it- but I need to learn more php :)

Take care- stay safe.
Post Reply