新手提问:YII框架怎么排序

WBOY
Lepaskan: 2016-06-06 20:32:53
asal
1183 orang telah melayarinya

<code>public function actionIndex($id)
    {

        $this->pageTitle = Yii::t('zh_CN', 'PAGE_TITLE_PRODUCT');

        $current_product_category_model = ProductCategory::model()->findByPk($id);


        $criteria = new CDbCriteria();
        $criteria->limit = 5;
        $product_category = ProductCategory::model()->findAll($criteria);

        $product_sub_category = ProductSubCategory::model()->findAll('product_category_id=:product_category_id', array(':product_category_id' => $id));

        $products = Product::model()->findAll('product_status="online" AND product_category_id=:product_category_id', array(':product_category_id' => $id));
        $this->render("index", array(
            "products" => $products,
            "current_product_category_model" => $current_product_category_model,
            "product_category" => $product_category,
            "product_sub_category" => $product_sub_category,
        ));
    }
</code>
Salin selepas log masuk
Salin selepas log masuk

$criteria->order = 'id desc'; 我知道,但是只能对$product_category进行排序,我想对$products也进行排序 怎么排 多谢

回复内容:

<code>public function actionIndex($id)
    {

        $this->pageTitle = Yii::t('zh_CN', 'PAGE_TITLE_PRODUCT');

        $current_product_category_model = ProductCategory::model()->findByPk($id);


        $criteria = new CDbCriteria();
        $criteria->limit = 5;
        $product_category = ProductCategory::model()->findAll($criteria);

        $product_sub_category = ProductSubCategory::model()->findAll('product_category_id=:product_category_id', array(':product_category_id' => $id));

        $products = Product::model()->findAll('product_status="online" AND product_category_id=:product_category_id', array(':product_category_id' => $id));
        $this->render("index", array(
            "products" => $products,
            "current_product_category_model" => $current_product_category_model,
            "product_category" => $product_category,
            "product_sub_category" => $product_sub_category,
        ));
    }
</code>
Salin selepas log masuk
Salin selepas log masuk

$criteria->order = 'id desc'; 我知道,但是只能对$product_category进行排序,我想对$products也进行排序 怎么排 多谢

哥,你都会对$product_category排序了,那你把对$product_category排序的代码套到$products上不就o了吗?

<code> $products = Product::model()->findAll(new CDbCriteria(array(
      "condition" => 'product_status="online" AND product_category_id=:product_category_id',
      "order" => "id desc",
      "params" => array(':product_category_id' => $id)
   )));
</code>
Salin selepas log masuk
Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan