在函数内部创建函数以发送电子邮件

时间:2015-09-23 07:54:51

标签: php wordpress class

我的功能有问题。我创建了发送电子邮件的功能。我试图创建函数,例如:我用变量($ product_id)调用函数,并依赖于此产品ID,我想发送电子邮件。

<?php
$product_id = 2000;
echo select_users_to_send($product_id);

function get_product_type($id_product){
    // Define product group
    global $wpdb;
    $sql = "SELECT n_products.Product_Group
FROM tsales_funnel_mrecord
LEFT JOIN n_products on tsales_funnel_mrecord.Product_type = n_products.Product_Code
WHERE ID = $id_product";
    $type_product = $wpdb->get_var($sql);
    return $type_product;
}
function select_users_to_send($type = get_product_type($product_id)){
    return $type.' user';
}

?>

我写了这段代码,但它并不适合我。显示错误:

Parse error: syntax error, unexpected '(', expecting ')'

也许需要使用课程?但我不明白如何写课。

3 个答案:

答案 0 :(得分:2)

试试这个

Order by 

答案 1 :(得分:1)

试试这个:

  

此代码解决了错误&#34;解析错误:语法错误,意外&#39;(&#39;,   期待&#39;)&#39;&#34;

<?php
$product_id = 2000;
echo select_users_to_send($product_id);

function get_product_type($id_product){
    // Define product group
    global $wpdb;

    $sql = "SELECT n_products.Product_Group
FROM tsales_funnel_mrecord
LEFT JOIN n_products on tsales_funnel_mrecord.Product_type = n_products.Product_Code
WHERE ID = $id_product";

    $type_product = $wpdb->get_var($sql);

    return $type_product;
}

// getting output from the function
$type = get_product_type($product_id);

// passing the $type var
function select_users_to_send($type){
    return $type.' user';
}

?>

答案 2 :(得分:1)

您可以像这样创建类。但是您可以使用第一个答案解决您的问题,这只是您对类

的了解的示例

select_user.php

    class select_user{
    # Start function get_product_type
    function get_product_type($id_product){
        // Define product group
        global $wpdb;
        $sql = "SELECT n_products.Product_Group
        FROM tsales_funnel_mrecord
        LEFT JOIN n_products on tsales_funnel_mrecord.Product_type = n_products.Product_Code
        WHERE ID = $id_product";
        $type_product = $wpdb->get_var($sql);
        return $type_product;
    } 
    # Start function select_users_to_send

    function select_users_to_send($product_id){
        $type = get_product_type($product_id);
        return $type.' user';
    }
}# End Class

现在创建用于调用类

的PHP文件
# include class file path
include ('select_user.php');
# create class object
$user_create = new select_user();

# Call methord of class class_name 
$product_id = 2000;

$return_val = $user_create->select_users_to_send($product_id);

echo $return_val;