如何使用PHP在mysql上对返回值asc命令进行排序?

时间:2015-10-20 11:20:51

标签: php arrays sorting

我有2个表1用于产品猫,其他用于mysql中的产品。如果我的父类别没有任何产品,我必须从mysql获取子类别的产品。我成功实现了它。但在子类产品方法中,产品首先出现;第一类产品,然后是其他类别的产品。我的方法看起来像;

public function getProductsByCat($productCatID) {
    $lang = LanguageController::detectLang();
    $visibility = VIS;
    $this->prepare('SELECT product_details.*, product_details_lang.*, product_cat_lang.*
                    FROM product_details
                    INNER JOIN product_details_lang 
                    ON product_details.product_id = product_details_lang.product_id
                    INNER JOIN product_cat_lang
                    ON product_details.product_cat_id = product_cat_lang.product_cat_id
                    WHERE product_cat_lang.product_cat_id = :productCatID
                    AND product_details_lang.product_lang_iso =  :lang
                    AND product_details_lang.visibility = :visibility
                    ORDER BY `product_details`.`product_name` ASC');
    // Binding Values
    $this->bindParam(':productCatID',$productCatID);
    $this->bindParam(':lang',$lang);
    $this->bindParam(':visibility',$visibility);
    $this->execute();
    // Executing Query
    $this->param = $this->fetchAll();
    return $this->param;

我在视图中使用方法:

$products->getProductsByCat($subProductCat->product_cat_id);
foreach ($products->param as $product) {
    echo $product->product_name'\n';
}

但产品按类别列表顺序排列。我想以product_name订单为ascending订购。当我var_dump($product)结果是;

object(stdClass)[12]
public 'product_id' => string '145' (length=3)
public 'product_cat_id' => string '6' (length=1)
public 'product_name' => string 'TK001' (length=5)
public 'order_id' => string '145' (length=3)
public 'id' => string '6' (length=1)
public 'product_lang_iso' => string 'tr' (length=2)
public 'product_lang_name' => string 'TK001' (length=5)
public 'product_lang_link' => string 'tk001' (length=5)
public 'product_code' => string '' (length=0)
public 'product_type' => string '' (length=0)
public 'product_volume' => string '' (length=0)
public 'product_package' => string '' (length=0)
public 'add_user' => string '1' (length=1)
public 'add_date' => string '2015-04-10 20:46:06' (length=19)
public 'upd_user' => string '1' (length=1)
public 'upd_date' => string '2015-04-10 20:46:06' (length=19)
public 'visibility' => string '1' (length=1)
public 'product_cat_lang_iso' => string 'tr' (length=2)
public 'product_cat_lang_name' => string 'Çelik Tencere Kulpları' (length=24)
public 'product_cat_lang_link' => string 'celik-tencere-kulplari' (length=22)
public 'product_cat_lang_content' => string 'Çelik Tencere Kulpları' (length=24)

任何帮助都会大大增加。

提前谢谢。

0 个答案:

没有答案