Jump to content

upload tree from file jsTree


Hybride

Recommended Posts

Hi everyone,

 

I have a file filled with categories (about 100K or so lines), each one by indentation (for illustration purposes, I added + instead).

Root

+ Category

++ File 1

+++ Sub Cat

++++ File 4

++++ File 5

++ File 2

+ Category 2

++ SubCategory 1

+++ File 1

++ SubCategory 2

 

What am trying to is upload this file, and have it input into my table so jsTree can render it correctly. It correctly inserts most of the lines, but starts to get confused when reverting back to previous lines (in the above example, it will insert up to file 5 correctly, then get lost on the left/right on File 2.)

 

Am using jsTree's create_node, but not sure why the render is failing? My (rather brute-force) upload script is below:

 

 
$dataFile = fopen($file_dir . basename($_FILES['uploaded']['name']), "rb");  
    while (!feof($dataFile))
    { 
      $lines = array();

      while (($line = fgets($dataFile)) !== false)
      {
        array_push($lines, rtrim($line)); 
      }

      $count = count($lines);
      $res = preg_replace('/\t/i', "|", $lines);
      $final = preg_replace("/\s(\W)[^\w]/i", "|", $res);
         
      $i = 0;
      foreach($final as $string)
      {
        //$parent_id = $delimiter_count - 1;
        $delimiter_count = substr_count($string, '|'); //level
        $left = $i + 1;
        $right = $left + 1;
        preg_match("/(\|+)(\w.+)/i", $string, $match);
        if(isset($match[2]))
        {
        
        /* even this doesn't work, if I directly inject it into the tree
          $insert = $keysdb->query("INSERT INTO tree (`parent_id`, `position`, `left`, `right`, `level`, `title`, `type`, `post_syn_epon`, `post_author_id`) 
          VALUES('$delimiter_count',0,'$left','$right','$delimiter_count','$match[2]','default','$match[2], ','user_id')");
    */
   
	$jstree->create_node(array(
		"id" => "$delimiter_count",
		"position" => 0,
		"title" => "$match[2]",
		"type" => "default"
	));
        
          if(!$insert) {
            print "Failed!" . $keysdb->error;
          } 
        }
      $i++;
    }
  }    
//  all done  close the file 
   fclose($dataFile); 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.