PHP Connector - TreeGrid and SQL

Hi!
I am generating TreeGrid XML from SQL (PHP).
My mySQL table “filetest” looks like this:

id, path, name, parentId, parentName
1, /Filename.txt, Filename.txt, 0,
2, /myFolder, myFolder, 0,
3, /xFilename.txt, xFilename.txt, 0,
4, /myFolder/mySubFolder, mySubFolder, 2, myFolder
5, /myFolder/subFilename.txt, subFilename.txt, 2, myFolder
6, /myFolder/mySubFolder2, mySubFolder2, 2, myFolder
7, /myFolder/mySubFolder/SubFolderPic.jpg, SubFolderPic.jpg, 4, mySubFolder

I am using the code bleow:
require("./connector/treegrid_connector.php");
$tree = new TreeGridConnector($res);
$tree->render_sql(“SELECT * from filestest”,“id”,“name”,"",“parentId”);

However.

  1. I wold like to show for folder rows and for file rows.
  2. Also, I would like to insert checkboxes <input type=“checkbox” name=“53” > on file rows.

How can this be done?
Thanks!

  1. Please, try to use the set_image function in your connector configuration:
    docs.dhtmlx.com/connector__php_ … l#setimage
    You may find a working example in your connector package “connector\samples\treegrid\04_custom_styles_connector.php”

  2. You may try to create an additional column in your treegrid with the “ch” column type and use the checkboxes from that column.

Thank you very much!
I will check the sample, did not think of using the “set_image” method. :confused:
Adding another column seems like a good idea - I will try that too.

After consideration I found it easier to generate XML structure from disk and transforming it via XSL.
I could not find any option for generating XML recursively from file system, including sub directories, in the dhtmlxConnector / PHP Connector.

So here is my solution:

[sort.xsl]

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet xmlns:xsl=“http://www.w3.org/1999/XSL/Transform
xmlns:xs=“http://www.w3.org/2001/XMLSchema” exclude-result-prefixes=“xs” version=“1.0”>
<xsl:output encoding=“ISO-8859-1” method=“xml”/>

<xsl:template match="node() | @*">
    <xsl:copy>
        <xsl:apply-templates select="node() | @*"/>
    </xsl:copy>
</xsl:template>

<!-- 
    Sort elements rows / row 
    First sort by attribute isFolder then 
    sort by attribute fileName (path) 
-->
<xsl:template match="rows | row">
    <xsl:copy>
        <xsl:apply-templates select="@*"/>
        <xsl:apply-templates select="*">
            <xsl:sort select="@isFolder" data-type="text" order="ascending"/>
            <xsl:sort select="@fileName" data-type="text" order="ascending"/>
        </xsl:apply-templates>
    </xsl:copy>
</xsl:template>

<!-- Remove attributes used for sorting -->
<xsl:template match="row/@isFolder" />
<xsl:template match="row/@filePath" />

</xsl:stylesheet>

[treeGridXML.php]

<?php header("Content-type: text/xml; charset=iso-8859-1"); $start_dir = '/YOUR SERVERS BASE PATH FOR STORING FILES/PATH/PATH'; $domtree = new DOMDocument('1.0', 'ISO-8859-1'); $xmlRoot = $domtree->createElement("rows"); //XML Root $domAttribute = $domtree->createAttribute('parent'); //Create attribute $domAttribute->value = '0'; // Value for the created attribute $xmlRoot->appendChild($domAttribute); // Append attribute $xmlRoot = $domtree->appendChild($xmlRoot); //Append it to root element $folderElement; $folderName; $fileElement; $fileName; $parent; $counter = 0; $rit = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $start_dir, FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::SKIP_DOTS ), RecursiveIteratorIterator::SELF_FIRST ); foreach ($rit as $path => $fileinfo) { $exclude = "Substring for folders and files to exclude"; $include = (strpos($path , $exclude)) === false ? true : false; if($include){ $level = $rit->getDepth(); $counter++; $name = str_replace($start_dir."/", "", $path); $folderTitle = ($level==0) ? $name : substr($name, strrpos($name, '/') + 1); //XPath - find parent node $xpath = new DomXpath($domtree); $parts = explode('/', $name); $lastPart = array_pop($parts); $parentFolderName = (count($parts)>=1) ? implode('/', $parts) : ''; $rowNode = $xpath->query('//row[@filePath="'. $parentFolderName .'"][1]')->item(0); $parent = ($rowNode instanceof DomElement) ? $rowNode : $xmlRoot; if ($fileinfo->isDir()) { $folderName = $name ."/"; $folderElement = $domtree->createElement("row"); $domAttribute = $domtree->createAttribute('id'); $domAttribute->value = $counter; $folderElement->appendChild($domAttribute); $domAttribute = $domtree->createAttribute('isFolder'); $domAttribute->value = "1"; $folderElement->appendChild($domAttribute); $domAttribute = $domtree->createAttribute('fileName'); $domAttribute->value = $folderTitle; $folderElement->appendChild($domAttribute); $folderElement->appendChild($domAttribute); $domAttribute = $domtree->createAttribute('filePath'); $domAttribute->value = $name; $folderElement->appendChild($domAttribute); $cell = $domtree->createElement("cell", $folderTitle); $domAttribute = $domtree->createAttribute('image'); $domAttribute->value = "folder.gif"; $cell->appendChild($domAttribute); $folderElement->appendChild($cell); $folderElement->appendChild($domtree->createElement("cell","")); $folderElement = $parent->appendChild($folderElement); } else { $fileName = $folderTitle; $fileElement = $domtree->createElement("row"); $domAttribute = $domtree->createAttribute('id'); $domAttribute->value = $counter; $fileElement->appendChild($domAttribute); $domAttribute = $domtree->createAttribute('isFolder'); $domAttribute->value = "2"; $fileElement->appendChild($domAttribute); $domAttribute = $domtree->createAttribute('filePath'); $domAttribute->value = $fileName; $fileElement->appendChild($domAttribute); $cell = $domtree->createElement("cell", $fileName); $domAttribute = $domtree->createAttribute('image'); $domAttribute->value = "file.gif"; $fileElement->appendChild($cell); $fileElement->appendChild($domtree->createElement("cell","")); $fileElement = $parent->appendChild($fileElement); } } } $xml = $domtree->saveXML(); $xsl = new DOMDocument; $xsl->load('sort.xsl'); $proc = new XSLTProcessor(); $proc->importStyleSheet($xsl); $sortedXml = $proc->transformToXML($domtree); echo $sortedXml; ?>