Advertisement:

Author Topic: Number of ads in drop-down menu  (Read 390 times)

Gilou26

  • Newbie
  • *
  • Posts: 10
Number of ads in drop-down menu
« on: November 05, 2018, 06:57:50 pm »
Hello.
I would like to add the number of ads in the drop-down menu "Select a category". Is it possible?
Osclass 3.8. Theme Bender.
Excuse my English, this is a Google translation.
Thanks in advance.

Gilou26

  • Newbie
  • *
  • Posts: 10
Re: Number of ads in drop-down menu
« Reply #1 on: November 08, 2018, 09:13:00 am »
Hello,
Nobody knows if this is possible?  :(

patrickFromCroatia

  • Sr. Member
  • ****
  • Posts: 447
Re: Number of ads in drop-down menu
« Reply #2 on: November 08, 2018, 03:59:16 pm »
Hello,

On header search, search page or item post page? What theme do you use?

Regards.

Gilou26

  • Newbie
  • *
  • Posts: 10
Re: Number of ads in drop-down menu
« Reply #3 on: November 08, 2018, 04:10:35 pm »
Hello and thank you for your answer.
I would like to add the number of ads in the drop-down menu on header search.
I use the theme bender under Osclass 3.8.

patrickFromCroatia

  • Sr. Member
  • ****
  • Posts: 447
Re: Number of ads in drop-down menu
« Reply #4 on: November 08, 2018, 06:45:09 pm »
There's a way with editing a core file which is simpler, but we don't want to modify Osclass core files as they get overwritten with a update.

Put this code in Bender's functions.php, before ?>:

Code: [Select]
function cs_category_select_with_count($name = "sCategory", $category = null, $default_str = null) {
    if($default_str == null) $default_str = __('Select a category');
    $categories = Category::newInstance()->toTree();
    echo '<select name="' . $name . '" id="' . $name . '">';
    if(isset($default_str)) {
        echo '<option value="">' . $default_str . '</option>';
    }
    foreach($categories as $c) {
        echo '<option value="' . $c['pk_i_id'] . '"' . ( ($category['pk_i_id'] == $c['pk_i_id']) ? 'selected="selected"' : '' ) . '>' . $c['s_name'] . ' (' .$c['i_num_items']. ')</option>';
        if(isset($c['categories']) && is_array($c['categories'])) {
            cs_subcategory_select_with_count($c['categories'], $category, $default_item, 1);
        }
    }
    echo '</select>';
}

function cs_subcategory_select_with_count($categories, $category, $default_item = null, $deep = 0) {
    $deep_string = "";
    for($var = 0;$var<$deep;$var++) {
        $deep_string .= '&nbsp;&nbsp;';
    }
    $deep++;
    foreach($categories as $c) {
        echo '<option value="' . $c['pk_i_id'] . '"' . ( ($category['pk_i_id'] == $c['pk_i_id']) ? 'selected="selected"' : '' ) . '>' . $deep_string.$c['s_name'] . ' (' .$c['i_num_items']. ')</option>';
        if(isset($c['categories']) && is_array($c['categories'])) {
            cs_subcategory_select_with_count($c['categories'], $category, $default_item, $deep);
        }
    }
}

Now change this in header.php:

Code: [Select]
                    <?php osc_categories_select('sCategory'null__('Select a category''bender')) ; ?>

With this:

Code: [Select]
                    <?php cs_category_select_with_count('sCategory'null__('Select a category''bender')) ; ?>

Regards.

Gilou26

  • Newbie
  • *
  • Posts: 10
Re: Number of ads in drop-down menu
« Reply #5 on: November 09, 2018, 01:22:42 pm »
Hello patrickFromCroatia,
It works perfectly  :) :) :)
Many thanks for your answer.
Regards

patrickFromCroatia

  • Sr. Member
  • ****
  • Posts: 447
Re: Number of ads in drop-down menu
« Reply #6 on: November 09, 2018, 05:48:12 pm »
You're welcome. Please put "SOLVED" to the title of the topic by editing the first post in it.

Regards.

Gilou26

  • Newbie
  • *
  • Posts: 10
Re: Number of ads in drop-down menu
« Reply #7 on: November 09, 2018, 06:04:32 pm »
Hello,
I will still enjoy your experience.
I'm trying to apply a style on this drop-down menu. I can not do it  :-[ :-[
I would like the categories to be bold and underlined. And sub-categories in italics. (I have only one level of sub categories)
Do you think that is possible ?

marius-ciclistu

  • issues
  • Hero Member
  • *
  • Posts: 1527
  • "BE GRATEFUL TO THOSE THAT SUPPORTED YOU"
Re: Number of ads in drop-down menu
« Reply #8 on: November 09, 2018, 06:21:48 pm »
This would be nicer if in search sidebar the rafine category would contain the number of ads from the result of the search.
These can be inserted in bender_print_sidebar_category_search function from functions.php. Any Ideea how to get the search results count?

PS. Thank you for this solution.
« Last Edit: November 09, 2018, 06:55:41 pm by marius-ciclistu »

patrickFromCroatia

  • Sr. Member
  • ****
  • Posts: 447
Re: Number of ads in drop-down menu
« Reply #9 on: November 10, 2018, 12:07:19 am »
Hello,
I will still enjoy your experience.
I'm trying to apply a style on this drop-down menu. I can not do it  :-[ :-[
I would like the categories to be bold and underlined. And sub-categories in italics. (I have only one level of sub categories)
Do you think that is possible ?

Bold and italic is possible, but I think I can't underline the select option. I will provide code that has bold parent categories and italic subcategories. Just replace the old code in functions.php I gave you yesterday with this one:

Code: [Select]
function cs_category_select_with_count($name = "sCategory", $category = null, $default_str = null) {
    if($default_str == null) $default_str = __('Select a category');
    $categories = Category::newInstance()->toTree();
    echo '<select name="' . $name . '" id="' . $name . '">';
    if(isset($default_str)) {
        echo '<option value="">' . $default_str . '</option>';
    }
    foreach($categories as $c) {
        echo '<option value="' . $c['pk_i_id'] . '"' . ( ($category['pk_i_id'] == $c['pk_i_id']) ? 'selected="selected"' : '' ) . ' style="font-weight: bold;">' . $c['s_name'] . ' (' .$c['i_num_items']. ')</option>';
        if(isset($c['categories']) && is_array($c['categories'])) {
            cs_subcategory_select_with_count($c['categories'], $category, $default_item, 1);
        }
    }
    echo '</select>';
}

function cs_subcategory_select_with_count($categories, $category, $default_item = null, $deep = 0) {
    $deep_string = "";
    for($var = 0;$var<$deep;$var++) {
        $deep_string .= '&nbsp;&nbsp;';
    }
    $deep++;
    foreach($categories as $c) {
        echo '<option value="' . $c['pk_i_id'] . '"' . ( ($category['pk_i_id'] == $c['pk_i_id']) ? 'selected="selected"' : '' ) . ' style="font-style: italic;">' . $deep_string.$c['s_name'] . ' (' .$c['i_num_items']. ')</option>';
        if(isset($c['categories']) && is_array($c['categories'])) {
            cs_subcategory_select_with_count($c['categories'], $category, $default_item, $deep);
        }
    }
}

Regards.

patrickFromCroatia

  • Sr. Member
  • ****
  • Posts: 447
Re: Number of ads in drop-down menu
« Reply #10 on: November 10, 2018, 12:27:02 am »
This would be nicer if in search sidebar the rafine category would contain the number of ads from the result of the search.
These can be inserted in bender_print_sidebar_category_search function from functions.php. Any Ideea how to get the search results count?

PS. Thank you for this solution.

Maybe tracking a number of ads in "osc_has_items()" loop? Here's what I tried, seems to work:

First add this function in functions.php:

Code: [Select]
<?php
function cs_parent_category($cat) {
    
$aCategory osc_get_category('id'$cat);
    
$parentCategory osc_get_category('id'$aCategory['fk_i_parent_id']);

    if(
count($parentCategory) == || $parentCategory == null || $parentCategory == '') {
        return 
null;
    }
    return 
$parentCategory['pk_i_id'];
}
?>


Then replace default bender_print_sidebar_category_search with this:

Code: [Select]
function bender_print_sidebar_category_search($aCategories, $current_category = null, $i = 0)
{
    $category_items = array();
    while(osc_has_items()) {
        if($category_items[osc_item_category_id()] == '' || $category_items[osc_item_category_id()] == null) {
            $parent = cs_parent_category(osc_item_category_id());
            if($parent != null) {
                $category_items[$parent] = 1;
            }
            $category_items[osc_item_category_id()] = 1;
        } else {
            $parent = cs_parent_category(osc_item_category_id());
            if($parent != null) {
                $category_items[$parent]++;
            }
            $category_items[osc_item_category_id()]++;
        }
    }
    osc_reset_items();

    $class = '';
    if(!isset($aCategories[$i])) {
        return null;
    }

    if($i===0) {
        $class = 'class="category"';
    }

    $c   = $aCategories[$i];
    $i++;
    if(!isset($c['pk_i_id'])) {
        echo '<ul '.$class.'>';
        if($i==1) {
            echo '<li><a href="'.osc_esc_html(osc_update_search_url(array('sCategory'=>null, 'iPage'=>null))).'">'.__('All categories', 'bender')."</a></li>";
        }
        foreach($c as $key => $value) {
    ?>
            <li>
                <a id="cat_<?php echo osc_esc_html($value['pk_i_id']);?>" href="<?php echo osc_esc_html(osc_update_search_url(array('sCategory'=> $value['pk_i_id'], 'iPage'=>null))); ?>">
                <?php if(isset($current_category) && $current_category == $value['pk_i_id']){ echo '<strong>'.$value['s_name'].'</strong>'; }
                else{ echo 
$value['s_name']; } ?>
(<?php echo ($category_items[$value['pk_i_id']] == '' '0' $category_items[$value['pk_i_id']]); ?>)
                </a>

            </li>
    <?php
        
}
        if(
$i==1) {
        echo 
"</ul>";
        } else {
        echo 
"</ul>";
        }
    } else {
    
?>

    <ul <?php echo $class;?>>
        <?php if($i==1) { ?>
        <li><a href="<?php echo osc_esc_html(osc_update_search_url(array('sCategory'=>null'iPage'=>null))); ?>"><?php _e('All categories''bender'); ?></a></li>
        <?php ?>
            <li>
                <a id="cat_<?php echo osc_esc_html($c['pk_i_id']);?>" href="<?php echo osc_esc_html(osc_update_search_url(array('sCategory'=> $c['pk_i_id'], 'iPage'=>null))); ?>">
                <?php if(isset($current_category) && $current_category == $c['pk_i_id']){ echo '<strong>'.$c['s_name'].'</strong>'; }
                      else{ echo 
$c['s_name']; } ?>
<?php echo ($category_items[$c['pk_i_id']] == '' $category_items[$c['pk_i_id']]); ?>
                </a>
                <?php bender_print_sidebar_category_search($aCategories$current_category$i); ?>
            </li>
        <?php if($i==1) { ?>
        <?php ?>
    </ul>
<?php
    
}
}

Regards.
« Last Edit: November 10, 2018, 12:29:33 am by patrickFromCroatia »

marius-ciclistu

  • issues
  • Hero Member
  • *
  • Posts: 1527
  • "BE GRATEFUL TO THOSE THAT SUPPORTED YOU"
Re: Number of ads in drop-down menu
« Reply #11 on: November 10, 2018, 09:19:53 am »
Thank you.
I'll give it a try.
« Last Edit: November 10, 2018, 10:00:38 am by marius-ciclistu »

Gilou26

  • Newbie
  • *
  • Posts: 10
Re: Number of ads in drop-down menu
« Reply #12 on: November 10, 2018, 09:34:39 am »
Hello,
It does not work. Should something be changed in the main.css file?
« Last Edit: November 10, 2018, 09:38:34 am by Gilou26 »

marius-ciclistu

  • issues
  • Hero Member
  • *
  • Posts: 1527
  • "BE GRATEFUL TO THOSE THAT SUPPORTED YOU"
Re: Number of ads in drop-down menu
« Reply #13 on: November 10, 2018, 10:00:06 am »
Hello,
It does not work. Should something be changed in the main.css file?

I made that kind of changes too. It only works on some devices/browsers.

Gilou26

  • Newbie
  • *
  • Posts: 10
Re: Number of ads in drop-down menu
« Reply #14 on: November 10, 2018, 10:10:53 am »
OK, too bad.
The result is the same, whether I use Internet Explorer, Firefox or Edge.
I have cleared the caches and delete cookies, but it does not work.
I have the ad number and this is the main one.
Thanks again.