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. - 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..: - 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
3 3322
Never mind, I resolved the prob...
Thanks anyway!
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?
Hello,
This was my solution. After a lot of testing, it works great now! It's a part of the free Prestashop online store. - 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);
-
-
}
-
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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,...
|
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 <>...
|
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...
|
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...
|
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...
|
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...
|
by: Fungal545 |
last post by:
# include <stdio>
int main()
{
//variables
float famount;
float ftax;
float sale;
int choice;
for(;;)
|
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...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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,...
|
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...
| |