Hola chicos
Como parano tirar todo y comenzar de nuevo, por ahi seria bueno saber donde esta el conflicto.
Este es el script del navegador
Código PHP:
Ver original<?php
}
$categories = fetchCategories();
$categories = formatCategories($categories, $catId);
?><ul>
<?php
sort($categories); // codigo me lo sugirio stramin, que ahora me recomendo que lo saque foreach ($categories as $category) {
$level = ($cat_parent_id == 0) ? 1 : 2;
$url = $_SERVER['PHP_SELF'] . "?c=$cat_id";
if ($level == 2) {
$cat_name = ' ' . $cat_name;
}
$listId = '';
if ($cat_id == $catId) {
$listId = ' id="current"';
}
?>
<li<?php echo $listId; ?>><a href="<?php echo $url; ?>"><?php echo $cat_name; ?></a></li>
<?php
}
?>
</ul>
y este es el que muestra las categorias
Código PHP:
Ver original<?php
require_once 'config.php';
function formatCategories($categories, $parentId)
{
foreach ($categories as $category) {
if ($category['cat_parent_id'] == $parentId) {
$navCat[] = $category;
}
$ids[$category['cat_id']] = $category;
}
$tempParentId = $parentId;
while ($tempParentId != 0) {
$parent = array($ids[$tempParentId]); $currentId = $parent[0]['cat_id'];
$tempParentId = $ids[$tempParentId]['cat_parent_id'];
foreach ($categories as $category) {
if ($category['cat_parent_id'] == $tempParentId && !in_array($category, $parent)) { $parent[] = $category;
}
}
for ($i = 0; $i < $n; $i++) {
$navCat2[] = $parent[$i];
if ($parent[$i]['cat_id'] == $currentId) {
}
}
$navCat = $navCat2;
}
return $navCat;
}
function getCategoryList()
{
$sql = "SELECT cat_id, cat_name, cat_image
FROM tbl_category
WHERE cat_parent_id = 0 LIKE 'A%'
";
$result = dbQuery($sql);
while ($row = dbFetchAssoc($result)) {
if ($cat_image) {
$cat_image = WEB_ROOT . 'images/category/' . $cat_image;
} else {
$cat_image = WEB_ROOT . 'images/no-image-small.png';
}
$cat[] = array('url' => $_SERVER['PHP_SELF'] . '?c=' . $cat_id, 'image' => $cat_image,
'name' => $cat_name);
}
return $cat;
}
function getChildCategories($categories, $id, $recursive = true)
{
if ($categories == NULL) {
$categories = fetchCategories();
}
for ($i = 0; $i < $n; $i++) {
$catId = $categories[$i]['cat_id'];
$parentId = $categories[$i]['cat_parent_id'];
if ($parentId == $id) {
$child[] = $catId;
if ($recursive) {
$child = array_merge($child, getChildCategories
($categories, $catId)); }
}
}
return $child;
}
function fetchCategories()
{
$sql = "SELECT cat_id, cat_parent_id, cat_name, cat_image, cat_description
FROM tbl_category
ORDER BY cat_name ASC";
$result = dbQuery($sql);
while ($row = dbFetchAssoc($result)) {
$cat[] = $row;
}
return $cat;
}
?>
Con el codigo de stramin, casi que funciono
Código PHP:
Ver original<?php
sort($categories); //este codigo foreach ($categories as $category) {
$level = ($cat_parent_id == 0) ? 1 : 2;
$url = $_SERVER['PHP_SELF'] . "?c=$cat_id";
if ($level == 2) {
$cat_name = ' ' . $cat_name;
}
$listId = '';
if ($cat_id == $catId) {
$listId = ' id="current"';
}
?>
Pero no los ordenaba por cat_id ! A ver si varios ojos ven mejor que 2 y encontrmos el problemita!?
Un