1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Top level category listings count

Discussion in 'Templates, HTML, CSS, and Design Help' started by jason1971, Apr 24, 2014.

  1. jason1971 Customer

    Hi all,

    I am looking for some way to show the total number of listings for my individual top level categories (the same as {total_listings}), so that I can use it elsewhere on the site.

    EG: We have 100 cottages to rent

    and say calling it like this We have {categoryid2_total} cottages to rent

    or is it going to be a major headache to do ?

    can anybody help ?

    thank you

    Jason
  2. Mike-N-Tosh Owner

    I bet that you could do it, Jason. Look at the plugin's that you are currently using that I already made for you as guidance.

    You only need one "parameter"/variable which is the categoryid (in the database table known as "section").

    Make a copy of the total_listings plugin. Add the "section" (e.g. category ID) to the database query.
  3. jason1971 Customer

    Mike does this look right ?

    <?php
    /**
    * @copyright 68 Classifieds
    *
    * @author 68 Classifieds
    * @package 68 Classifieds
    * @link http://www.68classifieds.com
    *
    */
    /**
    * Show the total number of listings.
    *
    * @param return int Total number.
    */
    function smarty_function_total_listings1($params, &$smarty)
    {
    global $db;

    $category = '';

    foreach ($params as $_key=>$_value)
    {
    switch ($_key)
    {
    case 'category':
    $$_key = (int)$_value;
    break;
    }
    }

    $sSQL="SELECT COUNT(*) AS total FROM ".PREFIX."listings WHERE expiration > NOW() AND display = 'Y'";
    if ($category != '')
    {
    $cSQL = "SELECT name FROM ".PREFIX."categories WHERE id = ".$section;
    $cResult = $db->query($cSQL);
    $crs = $cResult->fetch();
    if ($crs['name'] <> "") {
    $tmp['cat'] = $crs['name'];
    } else {
    $tmp['cat'] = "";
    }
    }
    $loop[$j++] = $tmp;
    }
    }
    $result=$db->query($sSQL);
    $rs=$result->fetch();
    $total_listings1=$rs['total'];
    return $total_listings1;
    }
    /* End of file function.total_listings1.php */
    /* Location: ./upload/plugins/function.total_listings1.php */
  4. Mike-N-Tosh Owner

    Jason,

    Close, but I think that you're over complicating it.

    Just add the additional parameter ($category) to the original search query. You don't need to query the categories table.

    Code:
    $sql = 'SELECT COUNT(*) AS total FROM '.PREFIX.'listings WHERE expiration > NOW() AND display = "Y" AND section = '.$category;
    You don't need any of the if/else or loop. (use the original total_listings and just add the code you have above the query and the extra param in the query).

    You will need to use the plugin call for each category that you want the total for in the template.
  5. jason1971 Customer

    Thank Mike.. fantastic as ever..... would I call it like this {total_listings1=="1"} ?
  6. Mike-N-Tosh Owner

    {total_listings1 category=X} where X=category id.
  7. jason1971 Customer

    Hi Mike,

    I am being told there is an issue, would you be kind enough to take a look at my code and see if I have done anything wrong (although I guess I must have)

    <?php
    /**
    * @copyright 68 Classifieds
    *
    * @author 68 Classifieds
    * @package 68 Classifieds
    * @link http://www.68classifieds.com
    *
    */
    /**
    * Show the total number of listings.
    *
    * @param return int Total number.
    */
    function smarty_function_total_listings1($params, &$smarty)
    {
    global $db;

    $category = '';

    foreach ($params as $_key=>$_value)
    {
    switch ($_key)
    {
    case 'category':
    $$_key = (int)$_value;
    break;
    }
    }

    $sSQL="SELECT COUNT(*) AS total FROM ".PREFIX."listings WHERE expiration > NOW() AND display = "Y" AND section = '.$category;
    $cSQL = "SELECT name FROM ".PREFIX."categories WHERE id = ".$section;
    $cResult = $db->query($cSQL);
    $crs = $cResult->fetch();
    if ($crs['name'] <> "") {
    $tmp['cat'] = $crs['name'];
    } else {
    $tmp['cat'] = "";
    }
    }
    $loop[$j++] = $tmp;
    }
    }
    $result=$db->query($sSQL);
    $rs=$result->fetch();
    $total_listings1=$rs['total'];
    return $total_listings1;
    }
    /* End of file function.total_listings1.php */
    /* Location: ./upload/plugins/function.total_listings1.php */




    PS: On a happier note (for me anyway :) I actually made £30 from my website today)
  8. Mike-N-Tosh Owner

    Jason,

    You still have all of that other stuff in there. As I said previously, you only need the new parameter, "category" and the switch before the query. Then just add the additional parameter to the existing query. NOT an additional separate query with a different variable assigned to it or any of the other stuff you have after it.

    Code:
    global $db;
     
    $category = '';
     
    foreach ($params as $_key=>$_value)
    {
      switch ($_key)
      {
            case 'category':
            $$_key = (int)$_value;
            break;
      }
    }
     
    $sql = 'SELECT COUNT(*) AS total FROM '.PREFIX.'listings WHERE expiration > NOW() AND display = "Y" AND section = '.$category;
     
    $result = $db->query($sql);
     
    $rs = $result->fetch();
     
    $total_listings1 = $rs['total'];
     
    return $total_listings1;
    
  9. jason1971 Customer

    Mike what can I say... your the man ! sorted ! god I love my website :)

Share This Page