Listing total plugin

Discussion in 'Templates, HTML, CSS, and Design Help' started by jason1971, Aug 2, 2016.

  1. jason1971 Customer

    Hi all,

    I have created a new total listings plugin which should not only pull the total number of listings from a given category, but also some additional text afterwards.

    If there are any listings in the given category it should show "NUMBER holiday rentals available"

    or if there are no listings "Sorry, no rentals available"

    The output I am using on the site is {total_listings3 category=374}

    The issue I am having is that it is showing both if ($rs['total'] results as opposed to one or the other, depending on whether a listing is in the given category or not.

    Any thoughts please as to where I'm going wrong, please?

    HERE'S THE CODE I'M USING BELOW

    <?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_listings3($params, &$smarty)
    {
    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();

    if ($rs['total'] > "0")
    {
    $total_listings3 = $rs['total'];
    return "$total_listings3 Holiday rentals available";
    }
    if ($rs['total'] < "1")
    {
    $total_listings3 = $rs['total'];
    return "Sorry, no rentals available";
    }
    }
    /* End of file function.total_listings3.php */
    /* Location: ./upload/plugins/function.total_listings3.php */
  2. Mike-N-Tosh Owner

    Hi Jason,

    Somehow I missed this post. Try this instead, it's cleaner and shorter:

    PHP:
    <?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_listings3($params, &$smarty)
    {
          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();
     
        
    $cat = (int) $rs['total'];
     
        
    $msg $cat $cat " Holiday rentals available" "Sorry, no rentals available";
     
        return 
    $msg;
    }
     
    /* End of file function.total_listings3.php */
    /* Location: ./upload/plugins/function.total_listings3.php */
  3. jason1971 Customer

    Thank you very much Mike, as usual, it worked like a charm. Very much appreciated

Share This Page