Post by marcella » Sat Oct 16, 2010 9:20 am

I was looking for a way to bring out a formatted list of categories to latch onto and style accordingly. For a drop down menu more specifically.

In order to be semantic it should be...

Code: Select all

<ul>
     <li><a href="#">Link</a>
           <ul>
                 <li><a href="#">Link</a>
                     <ul>
                          <li><a href="#">Link</a>
                    </ul>
                </li>
            </ul>
     </li>
</ul>
Anyway, I found a snippet here...

http://craigmurray.me.uk/opencart-mods/ ... -opencart/

I've taken that which only supports 2 levels and hacked it somewhat. Without even considering the implications of code standard i dare to submit it below.

Hope it's useful to someone.

Code: Select all

$this->load->model('tool/seo_url');

$results = $this->model_catalog_category->getCategories();

if($results)
{
    $output = '<ul>';
}

$output .= '<li><a href="/">Home</a></li>';

$counter = 0;

foreach ($results as $result) 
{                    
    // HTML
    $output .= '<li class="nav-item-'.$counter.'">';
    
    // Grab the specific category, reset each loop
    $new_path = $result['category_id'];
    
    // Prepare the URL
    $unrewritten = HTTP_SERVER.'index.php?route=product/category&path=' . $new_path;
    
    // Pass it to the SEO URL tool
    $rewritten = $this->model_tool_seo_url->rewrite($unrewritten);
    
    // Output the path and category name
    $output .= '<a href="' . $rewritten . '">' . $result['name'] . '</a>';
    
    $counter++;
    
    // Next level deep with the current category as the parameter.
    // Children of
    $sub_results = $this->model_catalog_category->getCategories($new_path);
    
    // If there are subs
    if ($sub_results) 
    {
        // HTML
        $output .= '<ul>';
    }
    
    foreach ($sub_results as $sub_result) 
    {
        // HTML
        $output .= '<li>';
        
        // Allocate the new sub category
        $new_sub_path = $sub_result['category_id'];
        
        // Get the raw URL and prepare it
        $sub_unrewritten = $unrewritten.'_'. $new_sub_path;
        
        // Rewrite it
        $sub_rewritten = $this->model_tool_seo_url->rewrite($sub_unrewritten);
        
        // Output as usual
        $output .= '<a href="' . $sub_rewritten . '">' . $sub_result['name'] . '</a>';
        
        // Grab the specific category, reset each loop
        $new_sub_path = $sub_result['category_id'];
        
        // Next level deep with the current category as the parameter.
        // Children of
        $sub_sub_results = $this->model_catalog_category->getCategories($new_sub_path);
        
        // If there are subs
        if ($sub_sub_results) 
        {
            // HTML
            $output .= '<ul class="sub-sub-results">';
        }
        else
        {
            $output .= '</li>';    
        }
        
        $counter2 = 0;
        foreach ($sub_sub_results as $sub_sub_result) 
        {
            // HTML
            $output .= '<li class="sub-nav-item-'.$counter2.'">';
            
            // Allocate the new sub category
            $new_sub_path = $sub_result['category_id'];
            
            // Get the raw URL and prepare it
            $sub_sub_unrewritten = $unrewritten.'_'. $new_sub_path;
            
            // Rewrite it
            $sub_sub_rewritten = $this->model_tool_seo_url->rewrite($sub_sub_unrewritten);
            
            // Output as usual
            $output .= '<a href="' . $sub_sub_rewritten . '">' . $sub_sub_result['name'] . '</a>';
            $output .= '</li>';    
            $counter2++;
        }
        
        if ($sub_sub_results) 
        {
            // Close off children UL
            $output .= '</ul>';
        }
        $output .= '</li>';                            
    }
    
    if ($sub_results) 
    {
        // Close off children UL
        $output .= '</ul>';
    }

}

if ($results) 
{
    // Close parent UL
    $output .= '</ul>';
}

echo $output;
 
Ignore the PSEUDO comments, was just for my own benefit whilst reading Craigs code.

Code: Select all

$wpdb->update('users', 'users_nicename' = 'Alistair', 'display_name' = 'troubled', %s); 


User avatar
Newbie

Posts

Joined
Tue Oct 05, 2010 3:21 pm
Location - Edinburgh, Scotland

Post by marcella » Sat Oct 16, 2010 9:23 am

Ignore this for now, it's not passing the correct URL. Be back soon with edit.

Code: Select all

$wpdb->update('users', 'users_nicename' = 'Alistair', 'display_name' = 'troubled', %s); 


User avatar
Newbie

Posts

Joined
Tue Oct 05, 2010 3:21 pm
Location - Edinburgh, Scotland

Post by marcella » Sat Oct 16, 2010 9:33 am

It's really not pretty but here it is.

Code: Select all

$this->load->model('tool/seo_url');

$results = $this->model_catalog_category->getCategories();

if($results)
{
    $output = '<ul>';
}

$output .= '<li><a href="/">Home</a></li>';

$counter = 0;

foreach ($results as $result) 
{                    
    // HTML
    $output .= '<li class="nav-item-'.$counter.'">';
    
    // Grab the specific category, reset each loop
    $new_path = $result['category_id'];
    
    // Prepare the URL
    $unrewritten = HTTP_SERVER.'index.php?route=product/category&path=' . $new_path;
    
    // Pass it to the SEO URL tool
    $rewritten = $this->model_tool_seo_url->rewrite($unrewritten);
    
    // Output the path and category name
    $output .= '<a href="' . $rewritten . '">' . $result['name'] . '</a>';
    
    $counter++;
    
    // Next level deep with the current category as the parameter.
    // Children of
    $sub_results = $this->model_catalog_category->getCategories($new_path);
    
    // If there are subs
    if ($sub_results) 
    {
        // HTML
        $output .= '<ul>';
    }
    
    foreach ($sub_results as $sub_result) 
    {
        // HTML
        $output .= '<li>';
        
        // Allocate the new sub category
        $new_sub_path = $sub_result['category_id'];
        
        // Get the raw URL and prepare it
        $sub_unrewritten = $unrewritten.'_'. $new_sub_path;
        
        // Rewrite it
        $sub_rewritten = $this->model_tool_seo_url->rewrite($sub_unrewritten);
        
        // Output as usual
        $output .= '<a href="' . $sub_rewritten . '">' . $sub_result['name'] . '</a>';
        
        // Grab the specific category, reset each loop
        $new_sub_path = $sub_result['category_id'];
        
        // Next level deep with the current category as the parameter.
        // Children of
        $sub_sub_results = $this->model_catalog_category->getCategories($new_sub_path);
        
        // If there are subs
        if ($sub_sub_results) 
        {
            // HTML
            $output .= '<ul class="sub-sub-results">';
        }
        else
        {
            $output .= '</li>';    
        }
        
        $counter2 = 0;
        foreach ($sub_sub_results as $sub_sub_result) 
        {
            // HTML
            $output .= '<li class="sub-nav-item-'.$counter2.'">';
            
            // Allocate the new sub category
            $new_sub_sub_path = $sub_sub_result['category_id'];                                
            
            // Get the raw URL and prepare it
            $sub_sub_unrewritten = $unrewritten.'_'. $new_sub_path.'_'.$new_sub_sub_path;
            
            // Rewrite it
            $sub_rewritten = $this->model_tool_seo_url->rewrite($sub_unrewritten);
            
            // Rewrite it
            $sub_sub_rewritten = $this->model_tool_seo_url->rewrite($sub_sub_unrewritten);
            
            // Output as usual
            $output .= '<a href="' . $sub_sub_unrewritten . '">' . $sub_sub_result['name'] . '</a>';
            $output .= '</li>';    
            $counter2++;
        }
        
        if ($sub_sub_results) 
        {
            // Close off children UL
            $output .= '</ul>';
        }
        $output .= '</li>';                            
    }
    
    if ($sub_results) 
    {
        // Close off children UL
        $output .= '</ul>';
    }

}

if ($results) 
{
    // Close parent UL
    $output .= '</ul>';
}

echo $output;
 

Code: Select all

$wpdb->update('users', 'users_nicename' = 'Alistair', 'display_name' = 'troubled', %s); 


User avatar
Newbie

Posts

Joined
Tue Oct 05, 2010 3:21 pm
Location - Edinburgh, Scotland

Post by santolla » Thu Apr 28, 2011 1:47 am

Is there a way to make the current menu highlight when on page?

Active Member

Posts

Joined
Mon Mar 08, 2010 8:21 am

Post by exactweb » Thu Jun 02, 2011 7:12 pm

is there a way to do this with 1.5?

New member

Posts

Joined
Tue May 31, 2011 5:06 pm
Who is online

Users browsing this forum: No registered users and 59 guests