Getting Folder list from server

Workarounds and usability notes.

Getting Folder list from server

Postby John Robin Dove » Tue Jan 27, 2015 8:48 am

Hi,

I have succeeded in doing this but my method seems a bit laborious. I can't help thinking there must be a better way.

Here is my php code:

(in getFiles.php)

Code: Select all
<?php
  $serverPath=$_SERVER['DOCUMENT_ROOT'].'/programs/';
  $files = scandir($serverPath);
  print_r($files);
?>


And here are my action sequences:

--------------------------------------------------------------------------------
Actions for Button "getFolders" of Page id 0
--------------------------------------------------------------------------------

-- On click... -----------------------------------------------------------------
Execute Script XMLHttpRequest (HTML)

(url parameter: "../server/getFiles.php", notifyObject parameter: name of self)

-- On User event... ------------------------------------------------------------
Define local variable "ctr" (Initial value: 0)
Define local variable "myArray" (Array)
Define local variable "nameStart" (Initial value: 0)
Define local variable "theLength" (Initial value: 0)
Define local variable "txtline" (Initial value: "")

Comment: value is set to list of files in the directory.
Execute Script pgSplitToArray (HTML)
Comment: splitExp parameter: crlf
Step ctr from 5 to myArray [ 0 ] - 2 by 1
Set txtline to myArray [ ctr ]
If txtline contains "."
Comment: My directories never contain periods.
Else
Comment: The directory name always starts 2 chars after =>
Set nameStart to offset ( ">", txtline ) + 2
Set theLength to charCount ( txtline )
Set txtline to characters nameStart to theLength of txtline
If text of field "result" = ""
Set text of Field "result" to txtline
Else
Set text of Field "result" to text of field "result" & crlf & txtline
End if
End if
End step loop


Is it not possible to convert Javascript arrays directly to Toolbook (Actions) arrays? Are Javascript arrays really just strings in disguise?
John Robin Dove
 
Posts: 486
Joined: Thu Jan 23, 2014 4:35 am

Re: Getting Folder list from server

Postby Clifton » Wed Jan 28, 2015 6:27 pm

To simplify your action code you could leverage the ability of pgSplitToArray() to split your http request by a regular expression.

Try setting the parameter splitExp = "/\n? +?\d+ +=> +?/"
What this means is to split your string by:
    A optional carriage return "\n?"
    followed optionally by any number of spaces " +?"
    followed by any number of digits "\d+"
    followed optionally by any number of spaces " +?"
    followed by an equal and greater than symbol "+>"
    followed optionally by any number of spaces " +?"
It may be that the expression above may have to be modified somewhat, but most PowerPac functions are designed to use JavaScript Regular Expressions to unleash the power of what they can do in certain situations. The one you have mentioned above is one such situation.

Then, instead of using a step loop, consider using a for each loop as it is easier to code and simpler in the long run. There are some nice examples of for each loops in the PowerPac Help API.

What is the difference between ToolBook action arrays and JavaScript arrays?
    ToolBook exports an action array using JSON object format (note the curly braces):
    myActionArray = { 1 : "some value", 2 : "another value", 3 : 12345, "color" : "green" }
    In the above case, this produces an object with a mix of numeric and string properties:
    myActionArray[1] = "some value"
    myActionArray[2] = "another value"
    myActionArray[3] = 12345
    myActionArray["color"] = "green"
    The downside of the above definition is that when the length of this associative array is requested:
    myActionArray.length = 0
    ... when one may assume it should be 4. This is because ToolBook's action arrays are really object/properties or key/value pairs and not really arrays at all.
    A JavaScript array is a true array in the sense that each index holds a value of any type. The index of JavaScript arrays are always numeric (note that simple brackets define these arrays):
    jsArray = [ "some value", "another value", 12345 ];
    In the above case:
    jsArray[0] => "some value"
    jsArray[1] => "another value"
    jsArray[2] => 12345
    The nice thing here is we can get the length of this (true) array:
    jsArray.length = 3
    ... notice that our request for size of the array reveals the expected value of 3.
pgSplitToArray() creates true JavaScript arrays, but you can use all of the action editor loop structures to navigate them. There are advantages to both types of arrays/objects. True arrays, like in the latter example will have predictable index positions when navigating the array. Objects, like in the former example, are more flexible and complex and can have not only numeric, but string values for the index or property (e.g.: myActionArray[ "vehicle" ] = "Honda" ). From a development perspective, the ToolBook action array permits the maximum power to be achieved because its object array gives you the ability to have either normal or associative (string) indexes. The PowerPac uses both types but pgSplitToArray() returns only true arrays because we can then have more flexibility when sorting and returning the data stored in them.
Clifton
Site Admin
 
Posts: 732
Joined: Tue Jan 14, 2014 1:04 am

Re: Getting Folder list from server

Postby John Robin Dove » Fri Jan 30, 2015 11:55 am

Thanks for your detailed reply. I now have a better understanding of how JS and TB arrays work.

In the example above maybe be my PHP code is at fault? Maybe I should not be using print_r as this seems to return a string rather than an array. On the other hand, as it works perfectly, I shall keep it for the time being.
John Robin Dove
 
Posts: 486
Joined: Thu Jan 23, 2014 4:35 am

Re: Getting Folder list from server

Postby Clifton » Fri Jan 30, 2015 1:59 pm

When you do a HttpXMLRequest() using a client side application the return is always a string. Complex variables (arrays and objects) cannot be transferred over the http protocol.

In your php script, instead of using print_r() consider using
echo implode(",", $files);

This command will directly send your directory list already comma-separated back to the ToolBook application. Should make parsing a lot easier. It is always better, in my experience to let the server do as much as possible before sending data back to the client application. It is also better for security reasons.
Clifton
Site Admin
 
Posts: 732
Joined: Tue Jan 14, 2014 1:04 am

Re: Getting Folder list from server

Postby John Robin Dove » Sat Jan 31, 2015 9:07 am

OK, thanks for the info. I have other problems at the moment so I'll start a new topic.
John Robin Dove
 
Posts: 486
Joined: Thu Jan 23, 2014 4:35 am


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 3 guests

cron