Joomla query from database and from module example

<?php
defined('_JEXEC') or die('Restricted access');
use Joomla\CMS\Factory;

public static function getMaxPrice()
    {
        $db = Factory::getDBO();
        $query = $db->getQuery(true);
        $query->select('MAX(' . $db->quoteName('price') . ')')
            ->from($db->quoteName('#__osrs_properties'));
        $db->setQuery($query);
        return $db->loadResult();
    }

//Another example
public static function getPriceRangeAjax() {
        if (isset($_POST['category_id'])) {
            $categoryId = $_POST['category_id'];
            $priceRange = self::getPriceRangeForCategory($categoryId);
            echo json_encode($priceRange);
            exit;
        }
    }

// Method to get min and max price for a category
    public static function getPriceRangeForCategory($categoryId) {
        $db = Factory::getDBO();
        $query = $db->getQuery(true);

        // Adjust the query to fetch min and max price based on $categoryId
        $query->select('MIN(price) as minPrice, MAX(price) as maxPrice')
              ->from($db->quoteName('#__osrs_properties'))
              ->where($db->quoteName('category_id') . ' = ' . $db->quote($categoryId));

        $db->setQuery($query);
        return $db->loadAssoc();
    }

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *