Converting php code to c#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • djpaul
    New Member
    • Oct 2006
    • 137

    Converting php code to c#

    Hello,
    I am converting a php code to c# but I'm stuck.. Or the code is stuck. whatever.. :)
    In the second function, it keeps on looping now (of course) but I can't figure out who, why, or what.

    First the original php code (from prestashop). The code sets the left and right values in the database. This is for the breadcrumb.
    Code:
    public static function regenerateEntireNtree()
    	{
    		$categories = Db::getInstance()->ExecuteS('SELECT id_category, id_parent FROM '._DB_PREFIX_.'category ORDER BY id_parent ASC, position ASC');
    		$categoriesArray = array();
    		foreach ($categories AS $category)
    			$categoriesArray[(int)$category['id_parent']]['subcategories'][(int)$category['id_category']] = 1;
    		$n = 1;
            
    		self::_subTree($categoriesArray, 1, $n);
    	}
    
    	protected static function _subTree(&$categories, $id_category, &$n)
    	{
    		$left = (int)$n++;
           
    		if (isset($categories[(int)$id_category]['subcategories'])){
    		  
    			foreach (array_keys($categories[(int)$id_category]['subcategories']) AS $id_subcategory){
    				self::_subTree($categories, (int)$id_subcategory, $n);
                    
            
                }
                    
            }
            
    		$right = (int)$n++;
    
    		Db::getInstance()->Execute('UPDATE '._DB_PREFIX_.'category SET nleft = '.(int)$left.', nright = '.(int)$right.' WHERE id_category = '.(int)$id_category.' LIMIT 1');
    	}
    And this is what I have done so far..:
    Code:
    public static void ReGenerateEntireTree()
           {
               DataTable Table = SelectQuery("SELECT id_category, id_parent FROM ps_category ORDER BY id_parent ASC, position ASC");
               
               string[,] categoryArray = new string[Table.Rows.Count, 2];
    
               for (int i = 0; i < Table.Rows.Count; i++)
               {
                   
                   categoryArray[i, 0] = Convert.ToString(Table.Rows[i]["id_parent"]);
                   categoryArray[i, 1] = Convert.ToString(Table.Rows[i]["id_category"]);
               }
    
               int n = 1;
               SubTree(categoryArray, 1, n);
               
           }
    
           public static void SubTree(string[,] categories, int id_category, int n)
           {
               int left = n++;
               // debug 
               // Als je het proggie debugt kun je bij Edit -> Test de functies uitvoeren....
               //Endless loop... :(
               if (categories[id_category, 0] != null)
               {
                   for (int key = 0; key < categories.Length; key++)
                   {
    
                       int id_subcategory = Convert.ToInt32(categories[key, 1]);
                       SubTree(categories, id_subcategory, n);
    
                   }
               }
    
               int right = n++;
               string SQL = "UPDATE ps_category SET nleft = '" + left + "', nright = '" + right + "' WHERE id_category = " + id_category;
               //UpdateQuery(SQL); 
               
           }
    Thanks in advance!
    Paul
  • djpaul
    New Member
    • Oct 2006
    • 137

    #2
    Never mind, I resolved the prob...
    Thanks anyway!

    Comment

    • zmbd
      Recognized Expert Moderator Expert
      • Mar 2012
      • 5501

      #3
      Would you please post your solution so that someone searching with the same or similar question can benefit from your insight?

      Comment

      • djpaul
        New Member
        • Oct 2006
        • 137

        #4
        Hello,
        This was my solution. After a lot of testing, it works great now! It's a part of the free Prestashop online store.

        Code:
        public static void ReGenerateEntireTree()
               {
                   DataTable Table = SelectQuery("SELECT id_category, id_parent FROM ps_category ORDER BY id_parent ASC, position ASC");
                   Dictionary<int, int> categoryArray = new Dictionary<int,int>();
              
                   for (int i = 0; i < Table.Rows.Count; i++)
                   {
                       
                       categoryArray.Add(Convert.ToInt32(Table.Rows[i]["id_category"]), Convert.ToInt32(Table.Rows[i]["id_parent"]));
        
                   }
        
                   int n = 1;
                   SubTree(categoryArray, 1, ref n);
                   
               }
                
               private static void SubTree(Dictionary<int, int> categories, int id_category, ref int n)
               {
                   int left;
                   int right;
                   left = n;
                   n++;
                   // debug 
                   
                   if (categories.ContainsValue(id_category))
                   {
                       var keysWithMatchingValues = categories.Where(p => p.Value == id_category).Select(p => p.Key);
                       foreach (var key in keysWithMatchingValues)
                       {
        
                           int id_subcategory = key;
                           SubTree(categories, id_subcategory, ref n);
        
                       }
                   }
        
                   right = n++;
                       
                   string SQL = "UPDATE ps_category SET nleft = '" + left + "', nright = '" + right + "' WHERE id_category = " + id_category;
                   UpdateQuery(SQL); 
                   
               }

        Comment

        Working...