我可以在PHP函数中插入mySQL语句吗?

时间:2015-03-31 02:12:34

标签: php html mysql database magento

所以我有如下面的PHP函数

<?php
class Mage_Checkout_Helper_Data extends Mage_Core_Helper_Abstract
{
    const XML_PATH_GUEST_CHECKOUT = 'checkout/options/guest_checkout';
    const XML_PATH_CUSTOMER_MUST_BE_LOGGED = 'checkout/options/customer_must_be_logged';

        protected $_agreements = null;

 public function formatPrice($price)
    {   
    return $this->getQuote()->getStore()->formatPrice($price);
    }
}

和MySQL连接这样的语句:

<?php 

        $prices = Mage::helper('core')->currency($_product->getPrice(), $formatPrice, $html);
        echo $this->getChildHtml('product_type_data');
        if(Mage::getSingleton('customer/session')->isLoggedIn()) {
        $customerData = Mage::getSingleton('customer/session')->getCustomer();

        $conn = Mage::getModel('core/resource')->getConnection('core_read');


        $sql = 'select sl.discount_amount, scg.customer_group_id, sl.stop_rules_processing from salesrule sl
                join salesrule_customer_group scg on scg.rule_id = sl.rule_id
                join customer_entity ce on ce.group_id = scg.customer_group_id
                where ce.email = "'.$customerData->getemail().'" 
                order by sl.discount_amount desc';

        $results = $conn->fetchAll($sql);

        $Abowl = '<p id="finalprice"></p>';
        echo $Abowl; //only applied when not logged in
        $discountRate = 1;
        foreach ($results as $result)
        {   

            $currentRate = $result['discount_amount'];
            $discountRate = $discountRate*(100 - $currentRate)/100;

            if($result['stop_rules_processing']==1){
                break;
            }

        }

        }   
        $prices = Mage::helper('core')->currency($_product->getPrice(), $formatPrice, $html); // store this long shit in a val
        echo $prices;
        ?> 

我厌倦了实现SQL连接语句;把它放在最上面把它放在函数内部,但它的任何一个都可以工作,它只会使我的整个页面消失。

那么如何将这个SQL语句包含在php函数中呢?

这不起作用:

      public function formatPrice($price)
{
    $prices = Mage::helper('core')->currency($_product->getPrice(), $formatPrice, $html);
            echo $this->getChildHtml('product_type_data');
            if(Mage::getSingleton('customer/session')->isLoggedIn()) {
            $customerData = Mage::getSingleton('customer/session')->getCustomer();

            $conn = Mage::getModel('core/resource')->getConnection('core_read');


            $sql = 'select sl.discount_amount, scg.customer_group_id, sl.stop_rules_processing from salesrule sl
                    join salesrule_customer_group scg on scg.rule_id = sl.rule_id
                    join customer_entity ce on ce.group_id = scg.customer_group_id
                    where ce.email = "'.$customerData->getemail().'" 
                    order by sl.discount_amount desc';

            $results = $conn->fetchAll($sql);

            $Abowl = '<p id="finalprice"></p>';
            echo $Abowl; //only applied when not logged in
            $discountRate = 1;
            foreach ($results as $result)
            {   

                $currentRate = $result['discount_amount'];
                $discountRate = $discountRate*(100 - $currentRate)/100;

                if($result['stop_rules_processing']==1){
                    break;
                }

            }

            }   
            $prices = Mage::helper('core')->currency($_product->getPrice(), $formatPrice, $html); // store this long shit in a val
            echo $prices;

    //die( 'here!' );
    //require_once('/var/www/html/app/design/frontend/neighborhood/default/template/catalog/category');
    //echo $price*0.2;
    return $this->getQuote()->getStore()->formatPrice($price);

我的结果如下图所示: enter image description here

0 个答案:

没有答案