Jump to content

Alphabetizing a list of links - click a link and it's not alphabetized anymore??


covenofone

Recommended Posts

Hi All,

I am editing a site, I didn't make it, and I have a list of links created from a mysql db.  I am sending the info from mysql in asc order, but this only works the first time the page comes up.  If you click on a link in the list, it appears to revert back to being ordered by the date entered and stays that way through subsequent clicks.  A point in the right direction on how to make my list stay alphabetized would be very good. 

With much appreciation.

Link to comment
Share on other sites

I set my Order to use the name in ASC order and that works only for the first time the page loads.  If one of the links is clicked, the list reverts back to some order that looks like date entered??

 

Here is the code that makes the list:

 

<?php

if (!defined('WEB_ROOT')) {

exit;

}

 

// get all categories

$categories = fetchCategories();

 

 

// format the categories for display

 

$categories = formatCategories($categories, $catId);

 

 

 

 

?>

<ul>

 

<?php

 

 

foreach ($categories as $category) {

extract($category);

 

// now we have $cat_id, $cat_parent_id, $cat_name

 

$level = ($cat_parent_id == 0) ? 1 : 2;

    $url  = $_SERVER['PHP_SELF'] . "?c=$cat_id";

 

// for second level categories we print extra spaces to give

// indentation look

if ($level == 2) {

 

$cat_name = '    » ' . $cat_name;

}

 

// assign id="current" for the currently selected category

// this will highlight the category name

$listId = '';

if ($cat_id == $catId) {

$listId = ' id="current"';

}

 

 

?>

<li<?php echo $listId; ?>>

 

 

<?php

# Displays SALE category in a different font style

if (($cat_name == 'Check out our Sales') & ($listId <> ' id="current"')){

echo '<a href="'.$url.'"><span class="style7">'.$cat_name.'</span>';

}

elseif (((substr($cat_name, -7, 5) == 'Sales') or (substr($cat_name, -7, 5) == 'sales')) & (($level == 2) & ($listId == ' id="current"'))) {

$url = $url."&hl=y";

echo '<a href="'.$url.'">'.$cat_name;

}

elseif (((substr($cat_name, -7, 5) == 'Sales') or (substr($cat_name, -7, 5) == 'sales')) & ($level == 2)) {

$url = $url."&hl=y";

echo '<a href="'.$url.'"><span class="style8">'.$cat_name.'</span>';

}

else {

 

echo '<a href="'.$url.'">'.$cat_name;

}

?></a></li>

<?php

}

?>

</ul>

 

 

<?php

require_once 'config.php';

 

/*********************************************************

*                CATEGORY FUNCTIONS

*********************************************************/

 

/*

Return the current category list which only shows

the currently selected category and its children.

This function is made so it can also handle deep

category levels ( more than two levels )

*/

function formatCategories($categories, $parentId)

{

// $navCat stores all children categories

// of $parentId

$navCat = array();

 

// expand only the categories with the same parent id

// all other remain compact

$ids = array();

foreach ($categories as $category) {

if ($category['cat_parent_id'] == $parentId) {

$navCat[] = $category;

}

 

// save the ids for later use

$ids[$category['cat_id']] = $category;

}

 

$tempParentId = $parentId;

 

// keep looping until we found the

// category where the parent id is 0

while ($tempParentId != 0) {

$parent    = array($ids[$tempParentId]);

$currentId = $parent[0]['cat_id'];

 

// get all categories on the same level as the parent

$tempParentId = $ids[$tempParentId]['cat_parent_id'];

foreach ($categories as $category) {

    // found one category on the same level as parent

// put in $parent if its not already in it

if ($category['cat_parent_id'] == $tempParentId && !in_array($category, $parent)) {

$parent[] = $category;

}

}

 

// sort the category alphabetically

array_multisort($parent);

 

// merge parent and child

$n = count($parent);

$navCat2 = array();

for ($i = 0; $i < $n; $i++) {

$navCat2[] = $parent[$i];

if ($parent[$i]['cat_id'] == $currentId) {

$navCat2 = array_merge($navCat2, $navCat);

}

}

 

$navCat = $navCat2;

}

 

return $navCat;

}

 

/*

Get all top level categories

*/

function getCategoryList()

{

$catId  = (isset($_GET['c']) && $_GET['c'] != '1') ? $_GET['c'] : 0;

 

$sql = "SELECT cat_id, cat_parent_id, cat_name, cat_image

        FROM tbl_category

WHERE cat_parent_id = '$catId'

ORDER BY cat_name ASC";

$sql = "SELECT cat_id, cat_parent_id, cat_name, cat_image

        FROM tbl_category

WHERE cat_parent_id <> '0'

ORDER BY cat_name ASC";

    $result = dbQuery($sql);

 

    $cat = array();

    while ($row = dbFetchAssoc($result)) {

extract($row);

 

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;

}

 

/*

Fetch all children categories of $id.

Used for display categories

*/

function getChildCategories($categories, $id, $recursive = true)

{

if ($categories == NULL) {

$categories = fetchCategories();

}

 

$n    = count($categories);

$child = array();

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);

 

    $cat = array();

 

    while ($row = dbFetchAssoc($result)) {

        $cat[] = $row;

    }

 

return $cat;

}

 

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.