Zen Cart Logo
Forums / Templates, Stylesheets, Page Layout / Display Current Category Image in Custom Menu

Display Current Category Image in Custom Menu

Views: 936

Results 1 to 3 of 3
27 Jun 2014, 2:15 AM
#1
enzo avatar

enzo

New Zenner

Join Date:
May 2014
Location:
Planet Earth
Posts:
18
Plugin Contributions:
0

Display Current Category Image in Custom Menu

Hello!

I am working on a new menu for my as yet unpublished site and I am customizing a new categories_ul_generator.php that I have modified based on code I believe (I've been working on it for a while now) I acquired from one of Picaflor-Azul's menus.

I am working with ZenCart v151 and am working locally with XAMPP. I am currently using the stock Zen Cart Template.

Everything works correctly. However, I need to pull the current category image so that it displays in the category link.

$result .= str_repeat($this->spacer_string, $this->spacer_multiplier * 1) . '<a href="' . zen_href_link(FILENAME_DEFAULT, 'cPath=' . $category_link) . '">';

// The following code in BLUE is what I would like to replicate as it does exactly what I need:
// $result .= '<img src="images/image.jpg" alt="image"/><span>" "</span>';
// where <img src="current categories_image"/>
// The following code in RED is what I need corrected.

$result .= '<img src="php code that calls current categories_image"';
$result .='" alt="';
$result .= 'php code that calls current categories_image alt name';
$result .='"/><span>';
$result .= $category['name'];
$result .= '</span>';
$result .= '</a>';

I've tried various methods to insert the proper code but I am obviously doing it wrong. I'm not even sure I need the alt code as I've seen snippets that calls the category image along with it's height and width etc and div's that echo the appropriate call to categories path and image info but I cannot find a way to assign anything with echo (so I replaced it with print which doesn't work in the manner I would desire) to a $result so that it displays in the correct order or at all.

Thank you for any assistance!

28 Jun 2014, 3:22 PM
#2
enzo avatar

enzo

New Zenner

Join Date:
May 2014
Location:
Planet Earth
Posts:
18
Plugin Contributions:
0

Re: Display Current Category Image in Custom Menu

I've modified the code to read as follows:

$result .=  str_repeat($this->spacer_string, $this->spacer_multiplier * 1) .  '<a href="' . zen_href_link(FILENAME_DEFAULT, 'cPath=' .  $category_link) . '">';                

$result .=  zen_image(DIR_WS_IMAGES . $categories->fields['categories_image'],  $categories->fields['categories_name'], SUBCATEGORY_IMAGE_WIDTH,  SUBCATEGORY_IMAGE_HEIGHT) . '<br />' .  $categories->fields['categories_name'] . '</a>';

However, the result is as follows:

<li><a href="http://localhost/test/index.php?main_page=index&cPath=1"><img width="100" height="80" alt="" src="images/no_picture.gif"><br></a></li>

I'm not sure why it does not acquire the correct path for the category image in SRC or why the ALT and the category's title are left blank as categories_name is omitted entirely.

Any ideas?

29 Jun 2014, 2:50 PM
#3
enzo avatar

enzo

New Zenner

Join Date:
May 2014
Location:
Planet Earth
Posts:
18
Plugin Contributions:
0

Re: Display Current Category Image in Custom Menu

Problem solved.

In my efforts to understand why the code I had been using failed to render properly, I led myself on a weeks long wild goose chase in my obsession to discover the answer.

I was so focused on the idea that the code I introduced should have worked, I overlooked the fact that I failed to introduce a new image element to my array AND in the process of trying all manner of variations of the code I thought should work, ignored the existing category name element that I knew did work.

Here is the solution in the hope I spare some other person looking to do something similar, valuable time. (I am only including the parts of categories_ul_generator.php that needed changes for my solution to work and to give a reference point for those interested.)

 global $languages_id, $db;
        $this->data = array();
  
       // I am using * as it fetches all fields.        
        
        $categories_query = "select * 
                                        from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd
                                        where c.categories_id = cd.categories_id
                                        and c.categories_status=1 " .
                                        " and cd.language_id = '" . (int)$_SESSION['languages_id'] . "' " .
                                        " order by c.parent_id, c.sort_order, cd.categories_name";
        $categories = $db->Execute($categories_query);
        while (!$categories->EOF) {

// The following is the ORIGINAL bit of code I needed to change.  Compare it to the next line to see the solution.

//            $this->data[$categories->fields['parent_id']][$categories->fields['categories_id']] = array('name' => $categories->fields['categories_name'], 'count' => 0);    

// In order to display each category image, a new element must be included in the array.  In this case the element "image" is introduced.
 
        $this->data[$categories->fields['parent_id']][$categories->fields['categories_id']] = array('name' => $categories->fields['categories_name'], 'image' => $categories->fields['categories_image'], 'count' => 0);    
        $categories->MoveNext();
        }
    }

Now that the element has been added to the array, I can complete my solution by including both the 'name' and 'image' elements in my result.

    function buildBranch($parent_id, $level, $submenu=true, $parent_link='')
    {
        $result = sprintf($this->parent_group_start_string, ($submenu==true) ? ' class="level'. ($level+1) . '"' : '' );
            {
            foreach($this->data[$parent_id] as $category_id => $category) {
                $category_link = $parent_link . $category_id;
                if (($this->data[$category_id])) {
                    $result .= sprintf($this->child_start_string, ($submenu==true) ? ' class="submenu"' : '');
                } else {
                    $result .= sprintf($this->child_start_string, '');
                }

$result .= str_repeat($this->spacer_string, $this->spacer_multiplier * 1) . '<a href="' . zen_href_link(FILENAME_DEFAULT, 'cPath=' . $category_link) . '">' . zen_image(DIR_WS_IMAGES . $category['image'], $category['name'], SUBCATEGORY_IMAGE_WIDTH, SUBCATEGORY_IMAGE_HEIGHT) . '<br />' . $category['name'] . '</a>';

If I had included all this information from the get go, someone may have seen the problem and advised me on a solution when I first posted. My bad, lesson learned.

All this said, if someone knows the proper code to specify the height and width of each image in the $result I posted, please advise, it would be greatly appreciated.

I realize I am using SUBCATEGORY_IMAGE_WIDTH and SUBCATEGORY_IMAGE_HEIGHT but I am only using the top category for my menu and I'm not sure this is the correct code. If I am using the correct code, I'd like it clarified.

Thanks!