473,412 Members | 1,921 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

Converting php code to c#

137 100+
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.
Expand|Select|Wrap|Line Numbers
  1. public static function regenerateEntireNtree()
  2.     {
  3.         $categories = Db::getInstance()->ExecuteS('SELECT id_category, id_parent FROM '._DB_PREFIX_.'category ORDER BY id_parent ASC, position ASC');
  4.         $categoriesArray = array();
  5.         foreach ($categories AS $category)
  6.             $categoriesArray[(int)$category['id_parent']]['subcategories'][(int)$category['id_category']] = 1;
  7.         $n = 1;
  8.  
  9.         self::_subTree($categoriesArray, 1, $n);
  10.     }
  11.  
  12.     protected static function _subTree(&$categories, $id_category, &$n)
  13.     {
  14.         $left = (int)$n++;
  15.  
  16.         if (isset($categories[(int)$id_category]['subcategories'])){
  17.  
  18.             foreach (array_keys($categories[(int)$id_category]['subcategories']) AS $id_subcategory){
  19.                 self::_subTree($categories, (int)$id_subcategory, $n);
  20.  
  21.  
  22.             }
  23.  
  24.         }
  25.  
  26.         $right = (int)$n++;
  27.  
  28.         Db::getInstance()->Execute('UPDATE '._DB_PREFIX_.'category SET nleft = '.(int)$left.', nright = '.(int)$right.' WHERE id_category = '.(int)$id_category.' LIMIT 1');
  29.     }
And this is what I have done so far..:
Expand|Select|Wrap|Line Numbers
  1. public static void ReGenerateEntireTree()
  2.        {
  3.            DataTable Table = SelectQuery("SELECT id_category, id_parent FROM ps_category ORDER BY id_parent ASC, position ASC");
  4.  
  5.            string[,] categoryArray = new string[Table.Rows.Count, 2];
  6.  
  7.            for (int i = 0; i < Table.Rows.Count; i++)
  8.            {
  9.  
  10.                categoryArray[i, 0] = Convert.ToString(Table.Rows[i]["id_parent"]);
  11.                categoryArray[i, 1] = Convert.ToString(Table.Rows[i]["id_category"]);
  12.            }
  13.  
  14.            int n = 1;
  15.            SubTree(categoryArray, 1, n);
  16.  
  17.        }
  18.  
  19.        public static void SubTree(string[,] categories, int id_category, int n)
  20.        {
  21.            int left = n++;
  22.            // debug 
  23.            // Als je het proggie debugt kun je bij Edit -> Test de functies uitvoeren....
  24.            //Endless loop... :(
  25.            if (categories[id_category, 0] != null)
  26.            {
  27.                for (int key = 0; key < categories.Length; key++)
  28.                {
  29.  
  30.                    int id_subcategory = Convert.ToInt32(categories[key, 1]);
  31.                    SubTree(categories, id_subcategory, n);
  32.  
  33.                }
  34.            }
  35.  
  36.            int right = n++;
  37.            string SQL = "UPDATE ps_category SET nleft = '" + left + "', nright = '" + right + "' WHERE id_category = " + id_category;
  38.            //UpdateQuery(SQL); 
  39.  
  40.        }
Thanks in advance!
Paul
Nov 17 '12 #1
3 3322
djpaul
137 100+
Never mind, I resolved the prob...
Thanks anyway!
Nov 23 '12 #2
zmbd
5,501 Expert Mod 4TB
Would you please post your solution so that someone searching with the same or similar question can benefit from your insight?
Nov 24 '12 #3
djpaul
137 100+
Hello,
This was my solution. After a lot of testing, it works great now! It's a part of the free Prestashop online store.

Expand|Select|Wrap|Line Numbers
  1. public static void ReGenerateEntireTree()
  2.        {
  3.            DataTable Table = SelectQuery("SELECT id_category, id_parent FROM ps_category ORDER BY id_parent ASC, position ASC");
  4.            Dictionary<int, int> categoryArray = new Dictionary<int,int>();
  5.  
  6.            for (int i = 0; i < Table.Rows.Count; i++)
  7.            {
  8.  
  9.                categoryArray.Add(Convert.ToInt32(Table.Rows[i]["id_category"]), Convert.ToInt32(Table.Rows[i]["id_parent"]));
  10.  
  11.            }
  12.  
  13.            int n = 1;
  14.            SubTree(categoryArray, 1, ref n);
  15.  
  16.        }
  17.  
  18.        private static void SubTree(Dictionary<int, int> categories, int id_category, ref int n)
  19.        {
  20.            int left;
  21.            int right;
  22.            left = n;
  23.            n++;
  24.            // debug 
  25.  
  26.            if (categories.ContainsValue(id_category))
  27.            {
  28.                var keysWithMatchingValues = categories.Where(p => p.Value == id_category).Select(p => p.Key);
  29.                foreach (var key in keysWithMatchingValues)
  30.                {
  31.  
  32.                    int id_subcategory = key;
  33.                    SubTree(categories, id_subcategory, ref n);
  34.  
  35.                }
  36.            }
  37.  
  38.            right = n++;
  39.  
  40.            string SQL = "UPDATE ps_category SET nleft = '" + left + "', nright = '" + right + "' WHERE id_category = " + id_category;
  41.            UpdateQuery(SQL); 
  42.  
  43.        }
  44.  
Nov 25 '12 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: William Krick | last post by:
I've been given the task of converting some existing java code to PHP so we can integrate it into our web based product. The problem I'm facing is that the java code makes heavy use of Vector...
7
by: Robert Diamond | last post by:
Hi ppl, just a quick question... I need to use "MultiByteToWideChar(stuff)" to convert a char to unicode, so that OleLoadPicturePath can get the image files i want, and load it into a HBITMAP,...
1
by: Mike P | last post by:
How can I convert this code from VB.NET to C#? Sub dgPopularFAQs_ItemDataBound(sender as Object, e as DataGridItemEventArgs) If e.Item.ItemType <> ListItemType.Header AND _ e.Item.ItemType <>...
2
by: Mountain Bikn' Guy | last post by:
This code is used in a simple example calculator. It has a text box that displays the result. It performs addition, subtraction, multiplication, etc. on two operands. For the operands it accepts...
4
by: Brian Henry | last post by:
Hi, I am looking at the project at http://vbaccelerator.com/home/NET/Code/Libraries/Windows/MDI_Client_Area_Painting/article.asp and i'm trying to convert it to VB.net right now im in the...
2
by: isaac rainsford | last post by:
i have some code that works fine in vb6, but when i paste it into vb.net 2003, i get errors thrown... can anyone tell me a good port of call for help converting this code??? i am assuming thing...
2
by: Elena | last post by:
I have code that works in VBA that I must convert to VB.Net. It works perfectly behind an Excel Form. I am writing a program in VB.Net that fills an Excel Document. I've exhausted my help in...
1
by: Fungal545 | last post by:
# include <stdio> int main() { //variables float famount; float ftax; float sale; int choice; for(;;)
10
by: Karl Rhodes | last post by:
Hi all, I'm in the middle of converting some C# code to VB and I'm almost done, but this last little bit is confusing me. I'm trying to convert the following C# code... public event...
10
by: Zubulake | last post by:
i need to convert this code into C #.. i am having the same problem with my code as did this person.. i need to replace a item in a listbox with text from a text box using a button.. THis is the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.