spot7.org logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories

Generating a tree from nested set


I used this function, but on php, not on ruby:

<?php
//nested sets data ordered by left
$data = array(
 array("left" => 1, "right" => 10, "name"
=> "P0"),
 array("left" => 2, "right" => 7, "name"
=> "P1"),
 array("left" => 3, "right" => 4, "name"
=> "P11"),
 array("left" => 5, "right" => 6, "name"
=> "P12"),
 array("left" => 8, "right" => 9, "name"
=> "P2")
);

//Converter function gets nested sets array and
returns nested php array
function nest($arrData){
 $stack = array();
 $arraySet = array();
 foreach( $arrData as $intKey=>$arrValues) {
  $stackSize = count($stack);
  while($stackSize > 0 &&
$stack[$stackSize-1]['right'] <
$arrValues['left']) {
   array_pop($stack);
   $stackSize--;
  }

  $link =& $arraySet;
  for($i=0;$i<$stackSize;$i++) {
   $link =&
$link[$stack[$i]['id']]["children"]; //navigate to
the proper children array
  }

  $tmp = array_push($link,  array
('item'=>$arrValues,'children'=>array()));
  array_push($stack, array('id' => $tmp-1,
'right' => $arrValues['right']));
 }

 return $arraySet;
}


//Print result
printArray(nest($data));

function printArray($array){
 echo "<ul>";
 foreach ($array as $row){
  $children = $row['children'];
  echo "<li>";
  echo $row['item']['name'];
  if (!empty($children)) printArray($children);
  echo "</li>";
 }
 echo "</ul>";
}
?>

Categories : Ruby On Rails

Related to : Generating a tree from nested set
Ruby on Rails: nested attributes, create a model and its nested attributes use the main model to create more nested attribute models using remote
I tried the following in the create action of order controller and it worked. If anyone have a better solution please let me know. def create if session[:order_id] @order = Order.find(session[:order_id]) @order.update_attributes(item_params) else @order = Order.new(order_params) @order.save end session[:order_id] = @order.id respond_to do |forma

Categories : Ruby On Rails
Convert a mixed nested list to a nested tuple
Convert recursively, and test for lists: def to_tuple(lst): return tuple(to_tuple(i) if isinstance(i, list) else i for i in lst) This produces a tuple for a given list, but converts any nested list objects using a recursive call. Demo: >>> def to_tuple(lst): ... return tuple(to_tuple(i) if isinstance(i, list) else i for i in lst) ... >>> mixed_nested_list = [['foo',

Categories : Python
Rails form_tag nested with nested resources
I think, team_plan_charges_path([@team, plan]) should be something like team_plan_charges_path([@team, @plan, @charges]) where @plan and @charges should be in your charges controller, like @plan = @team.plans.new @charges = @plan.charges.new assuming you have a has_many association between Team and Plan and Plan and Charge. I am also assuming that plan is not some method in your helpers and

Categories : Javascript
vcd not generating with right format
To generate a VCD file, you need to call $dumpvars in your Verilog file. For example: initial $dumpvars; Refer to the IEEE Std 1800-2012, section 21.7 "Value change dump (VCD) files".

Categories : Windows
Crypt generating *0
crypt returns *0 also if given an invalid salt - and that's the case here. Quoting the doc: Blowfish hashing with a salt as follows: "$2a$", "$2x$" or "$2y$", a two digit cost parameter, "$", and 22 characters from the alphabet "./0-9A-Za-z" Most probably, you've assumed that base64_encode() returns strings of the same set of characters. But it's not - there's also a + sign (the full alp

Categories : PHP
Recently Add
Groups and Users has many oder has many through?
Rails format specifier differences
Capistrano can not create symlinks: file exist
Admin can't delete users. Ruby on Rails 3 - Michael Hartl
Don't show user name who create post
Using Wicked with Devise (2 step signup process)
RubyOnRails Rake test failures
Rails session start event handler
Why ActiveModel::ForbiddenAttributesError error?
How can i track the issue in production RAILS
Cannot install spree gem - ERROR: invalid gem: package is corrupt
Why are the nested resources for Devise authentication not working?
Rails Devise - how to open access to the site root
Rails: respond_with the same template for two methods
rails with postgresl database "role postgres does not exist"
Instance variable in Rails helper not set
Monkey patch rails 3.2 rake task
Rails_admin: Should I have admin_user or user with admin role to manage users and admin panel
undefined method `attachments' for nil:NilClass
Memory usage increase with Ruby 2.1 versus Ruby 2.0 or 1.9
Can a Rails app be deployed without using Heroku Toolbelt? If so, how?
Enable random access to collection with MongoDB
libmysqlclient-dev installation fails
undefined method `name' for nil:NilClass. Can't find @followed.name but @followed is set
Allowing an arbitrary domain and subdomain in session_store.rb
Pushmeup Gem - Can't send push notifications in Active Admin model
Rails console does not start
GitHub Import: Could not locate Gemfile
AWS::S3::PermanentRedirect in SongsController#index "The bucket you are attempting to access must be addressed using the specified endpoint"
automatically create ActiveRecord parent for association
© Copyright 2017 spot7.org Publishing Limited. All rights reserved.