Zen Cart Logo
Forums / Templates, Stylesheets, Page Layout / extracting the first category from array

extracting the first category from array

Views: 3,038

Results 1 to 9 of 9
20 Dec 2017, 18:52
#1
keneso avatar

keneso

Totally Zenned

Join Date:
May 2009
Posts:
1,273
Plugin Contributions:
3

extracting the first category from array

Hi
Working on 1.55e in local.

I would need to append a class to the first item of category array list in modules/category_row.php which then I am going to use in templates/CUSTOM/common/tpl_columnar_display.php
I guess is the following (in case it's somewhere else please advise)

$list_box_contents[$row][$col] = array('params' => 'id="category' . $categories->fields['categories_id'] . '" class="categoryListBoxContents"',
'text' => '<a href="' . zen_href_link(FILENAME_DEFAULT, $cPath_new) . '">' . 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>');

but don't know how to do so.

I am aware of the id for the styling purpose, but need to add a class.

Thank you

20 Dec 2017, 19:21
#2
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,065
Plugin Contributions:
56

Re: extracting the first category from array

Is it always the same class? Is the class used just for styling or do you have some javascript/jQuery that will be looking for the class?

The reason I ask is that if you're just trying to style the first category in the list differently, you could use CSS pseudo-classes (see https://www.w3schools.com/css/css_pseudo_elements.asp) to style the first element (:first-child) without additional HTML.

20 Dec 2017, 19:25
#3
keneso avatar

keneso

Totally Zenned

Join Date:
May 2009
Posts:
1,273
Plugin Contributions:
3

Re: extracting the first category from array

Thank you.
It is for bootstrap's slider, I want to display categories in slider.

20 Dec 2017, 19:31
#4
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,065
Plugin Contributions:
56

Re: extracting the first category from array

Can you point me to a public URL where this slider is installed?

20 Dec 2017, 19:36
#5
keneso avatar

keneso

Totally Zenned

Join Date:
May 2009
Posts:
1,273
Plugin Contributions:
3

Re: extracting the first category from array

Like said is on local machine.
I can post the code if you want to see it.

20 Dec 2017, 21:13
#6
carlwhat avatar

carlwhat

zennedOut

Join Date:
Nov 2005
Location:
los angeles
Posts:
2,953
Plugin Contributions:
8

Re: extracting the first category from array

we are looking at includes/modules/category_row.php, true?

i think you are in the right spot, but look at the whole while statement:

// add indicator
$first_cat = true;
  while (!$categories->EOF) {
    if (!$categories->fields['categories_image']) !$categories->fields['categories_image'] = 'pixel_trans.gif';
// new code
    if ($first_cat) {
       $first_cat = $false;
       // now you can create you class or whatever
       $new_class = $categories;
    }
// end of new code
    $cPath_new = zen_get_path($categories->fields['categories_id']);
    // strip out 0_ from top level cats
    $cPath_new = str_replace('=0_', '=', $cPath_new);
    //    $categories->fields['products_name'] = zen_get_products_name($categories->fields['products_id']);
    $list_box_contents[$row][$col] = array('params' => 'class="categoryListBoxContents"' . ' ' . 'style="width:' . $col_width . '%;"',
                                           'text' => '<a href="' . zen_href_link(FILENAME_DEFAULT, $cPath_new) . '">' . 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>');
    $col ++;
    if ($col > (MAX_DISPLAY_CATEGORIES_PER_ROW -1)) {
      $col = 0;
      $row ++;
    }
    $categories->MoveNext();
  }

is this what you are looking for? it will give you the first category in the array.

no doubt there are other perhaps more elegant ways to do it.... but....

good luck.

22 Dec 2017, 13:44
#7
keneso avatar

keneso

Totally Zenned

Join Date:
May 2009
Posts:
1,273
Plugin Contributions:
3

Re: extracting the first category from array

carlwhat:

we are looking at includes/modules/category_row.php, true?
i think you are in the right spot, but look at the whole while statement:
is this what you are looking for? it will give you the first category in the array.
Thank you.
I guess that is what I am looking for ... I problem is I don't know how to use the "new code" not knowing php I just can guess what is going on there, but not how to actually add the value for $new_class

Following is the (I believe) relevant part of the code - I have removed the width part as using bootstrap I am not setting the width.

// new code
    if ($first_cat) {
       $first_cat = $false;
       // now you can create you class or whatever
       $new_class = $categories;
    }
// end of new code
    $cPath_new = zen_get_path($categories->fields['categories_id']);
    // strip out 0_ from top level cats
    $cPath_new = str_replace('=0_', '=', $cPath_new);
    //    $categories->fields['products_name'] = zen_get_products_name($categories->fields['products_id']);
    $list_box_contents[$row][$col] = array('params' => 'id="category' . $categories->fields['categories_id'] . '" class="item categoryListBoxContents"',

The "item" class is part of bootstrap carousel's code

<div id="gallery">
  <div id="bs-gallery-slider" class="carousel slide" data-ride="carousel" data-interval="3600" data-pause="false">
    <div class="carousel-inner">
      <div class="item active">
      First Slider content
      </div>
      <div class="item">
      Nth Slider content
      </div>
    </div>
  </div>
</div>

So now I need to add "active" for the first category, but regardless of your kind help I can't figure how.

22 Dec 2017, 16:31
#8
carlwhat avatar

carlwhat

zennedOut

Join Date:
Nov 2005
Location:
los angeles
Posts:
2,953
Plugin Contributions:
8

Re: extracting the first category from array

i'm sorry, perhaps i'm having a tough time understanding what you are trying to do.

in addition, the last line of your code is incomplete as well. all lines of php code end with a semi-colon.

if you are trying to add the active class to the first element of your array, you can modify as below. note that i am copying your code and the incomplete line, but i am also adding the info before the while statement, as you need to set the first_cat indicator to true prior to entering the while loop:

// add indicator
$first_cat = true;
while (!$categories->EOF) {
    if (!$categories->fields['categories_image']) {
        !$categories->fields['categories_image'] = 'pixel_trans.gif';
    }
// new code
    if ($first_cat) {
        $first_cat = $false;
        $active = ' active';
    } else {
        $active = '';
    }
// end of new code
    $cPath_new = zen_get_path($categories->fields['categories_id']);
// strip out 0_ from top level cats
    $cPath_new = str_replace('=0_', '=', $cPath_new);
//    $categories->fields['products_name'] = zen_get_products_name($categories->fields['products_id']);
    $list_box_contents[$row][$col] = array('params' => 'id="category' . $categories->fields['categories_id'] . '" class="item categoryListBoxContents' . $active . '"',

good luck.

22 Dec 2017, 17:00
#9
keneso avatar

keneso

Totally Zenned

Join Date:
May 2009
Posts:
1,273
Plugin Contributions:
3

Re: extracting the first category from array

Thank you.
It works as expected.

carlwhat:

i'm sorry, perhaps i'm having a tough time understanding what you are trying to do.
At all, my apologies for not being able to explain my problem.

carlwhat:

in addition, the last line of your code is incomplete as well. all lines of php code end with a semi-colon.
I just copied the code to where it was pertinent, indeed the line was incomplete.