Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
130 views
in Technique[技术] by (71.8m points)

php - How to create a filetree like table from an object?

I was a little bit confused when I made my first post. This time I'll be more specific.

I'm trying to make a "filetree-like" table from an object (From a php-transmission project). In my table I would have the filename, the size and the status (done or not). In the "Name" we can see some kind of "full path to file" string.

Thomas helped me out of making an array of names, but I forgot telling him that I was trying to grab it from an object..

Here is a sample of my code:

<?php
$transmission = new Transmission();
$transfer= $transmission->get('26ec249e2669388ff359923702ac0f5c7687d1be');  
$files = $transfer->getFiles();


foreach($files AS $file) 
    {
    var_dump($file->getName()); // RootFolder/Folder1/File1.jpg
    var_dump($file->getSize()); // 10383488
    var_dump($file->isDone());  // True or False

    print_r($file);
    }

Output:

string(59) "RootFolder/Folder1/File1.jpg" 
int(13324) 
bool(true) 
TransmissionModelFile Object
(
    [name:protected] => RootFolder/Folder1/File1.jpg
    [size:protected] => 13324
    [completed:protected] => 13324
    [client:protected] => 
)

$files will output:

Array
(
[0] => TransmissionModelFile Object
    (
        [name:protected] => RootFolder/Folder1/File1.jpg
        [size:protected] => 13324
        [completed:protected] => 13324
        [client:protected] => 
    )

[1] => TransmissionModelFile Object
    (
        [name:protected] => RootFolder/Folder1/File2.mp3
        [size:protected] => 10383488
        [completed:protected] => 10383488
        [client:protected] => 
    )
[2] ...
)

I saw a nice work from www.abeautifulsite.net, but it's based on is_dir() command.

What I want to do is a html table with collapsible parts (on folders) and files info (name, size and status) on file rows.

Any ideas?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I found the answer of the first part of my problem. Here's my code (based on my old post):

function createArray ($filesObject)
  {
  $result = array();
  foreach($filesObject AS $file) 
    {
    $prev = &$result;
    $s = strtok($file->getName(), '/');

     while (($next = strtok('/')) !== false) 
        {
        if (!isset($prev[$s])) {
        $prev[$s] = array();
        }

        $prev = &$prev[$s];
        $s = $next;
        }

    $prev[] = $s.'|'.$file->getSize().'|'.$file->isDone(); // Delimiter is '|'
    unset($prev);
    }
  return $result;
  }

The second part consists in displaying this array in some kind of file tree style html table. If someone have anny ideas?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...