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.
And this is what I have done so far..:
Thanks in advance!
Paul
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');
}
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);
}
Paul
Comment