第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定

如何做一個點(diǎn)擊標(biāo)簽 顯示使用標(biāo)簽的文章

求一個具體點(diǎn)的思路

正在回答

5 回答

<?php
namespace?frontend\widgets\post;
/**
?*?文章列表組件
?*/
use?common\models\PostModel;
use?common\models\RelationPostTagsModel;
use?common\models\TagsModel;
use?frontend\models\TagForm;
use?frontend\models\PostForm;
use?Yii;
use?yii\base\Widget;
use?yii\data\Pagination;
use?yii\db\Query;
use?yii\helpers\Url;

class?PostWidget?extends?Widget
{
????/**
?????*?文章列表的標(biāo)題
?????*?@var?string
?????*/
????public?$title?=?'';

????/**
?????*?顯示條數(shù)
?????*?@var?int
?????*/
????public?$limit?=?3;

????/**
?????*?是否顯示更多
?????*?@var?bool
?????*/
????public?$more?=?true;

????/**
?????*?是否顯示分頁
?????*?@var?bool
?????*/
????public?$page?=?true;


????public?function?run()
????{
????????$tag?=?Yii::$app->request->get('tag');
????????$curPage?=?Yii::$app->request->get('post',1);

????????//查詢條件
????????if(!empty($tag)){
????????????$ids?=?[];

????????????//方法一:使用with關(guān)聯(lián)表
????????????//$tags?=?new?TagForm();
????????????//$ids?=?$tags->getIdsByTag(['tag_name'=>$tag]);

????????????//方法二:先查詢標(biāo)簽id,再查詢文章id數(shù)組
????????????//$data?=?TagsModel::find()->where(['tag_name'=>$tag])->asArray()->one();
????????????//$ids?=?RelationPostTagsModel::getPostIds($data['id']);

????????????//方法三:使用join關(guān)聯(lián)表
????????????$data?=?(new?Query())
????????????????->select('a.post_id')
????????????????->from(['a'=>RelationPostTagsModel::tableName()])
????????????????->join('LEFT?JOIN',['b'=>TagsModel::tableName()],'a.tag_id=b.id')
????????????????->where(['b.tag_name'=>$tag])
????????????????->orderBy(['a.post_id'=>SORT_DESC])
????????????????->all();

????????????if(!empty($data)){
????????????????foreach?($data?as?$value){
????????????????????$ids[]?=?$value['post_id'];
????????????????}
????????????}

????????????//print_r($ids);exit;
????????????$cond?=?['id'=>$ids,'is_valid'=>PostModel::IS_VALID];
????????}else{
????????????$cond?=?['=','is_valid',PostModel::IS_VALID];
????????}

????????$res?=?PostForm::getList($cond,$curPage,$this->limit);
????????$result['title']?=?$this->title?:'最新文章';//組件標(biāo)題支持自定義
????????$result['more']?=?Url::to(['post/index']);
????????$result['body']=?$res['data']?:[];

????????//是否顯示分頁
????????if($this->page)
????????{
????????????$pages?=?new?Pagination(['totalCount'=>$res['count'],'pageSize'=>$res['pageSize']]);
????????????$result['page']?=?$pages;
????????}

????????return?$this->render('index',['data'=>$result]);
????}
}


0 回復(fù) 有任何疑惑可以回復(fù)我~
//文章列表組建里面
$curPage?=?\Yii::$app->request->get('page',?1);
$tagName?????=?\Yii::$app->request->get('tag');

$cond????=?[];
if?($tagName)?{
????$tags????=?new?Tags();
????$tag?=?$tags
????????->find()
????????->with('postid')
????????->where(['tag_name'=>$tagName])
????????->asArray()
????????->one();

????if?(isset($tag['postid'])?&&?!empty($tag['postid']))?{
????????foreach?($tag['postid']?as?$v)?{
????????????$id[]?=?$v['post_id'];
????????}
????????$cond?=?['id'?=>?$id,?'is_valid'?=>?Posts::IS_VALID];
????}
}
//?tagModel數(shù)據(jù)模型類
public?function?getPostid()
{
????return?$this->hasMany(RelationPostTags::className(),?['tag_id'=>'id']);
}


0 回復(fù) 有任何疑惑可以回復(fù)我~

請問下也是放在hotwidget里嗎


0 回復(fù) 有任何疑惑可以回復(fù)我~

ptmodel是什么

0 回復(fù) 有任何疑惑可以回復(fù)我~

在組件里添加條件。

$tags = Yii::$app->request->get('tag');
if($tags) {
? ?$tag = new PtModel();
? ?$post_id = $tag->getPostid($tags);

? ?if ($post_id) {
? ? ? ?foreach ($post_id as $v) {
? ? ? ? ? ?$id[] = $v['post_id'];
? ? ? ?}
? ? ? ?$cond = ['id' => $id,'is_valid'=>PostsModel::IS_VALID];
? ?}
}

$res = PostsModel::getList($cond,$curPage,$this->limit);

/*關(guān)聯(lián)表*/

public function getPostid($id){
? ?$res = PtModel::find()->select('post_id')->where('tag_id ='.$id)->asArray()->all();
? ?return $res;
}

2 回復(fù) 有任何疑惑可以回復(fù)我~

舉報

0/150
提交
取消
PHP之Yii2框架搭建完整博客系統(tǒng)
  • 參與學(xué)習(xí)       22587    人
  • 解答問題       291    個

Yii是PHP快速開發(fā)的最佳實(shí)踐之一,一起領(lǐng)略yii2快速開發(fā)的風(fēng)采

進(jìn)入課程

如何做一個點(diǎn)擊標(biāo)簽 顯示使用標(biāo)簽的文章

我要回答 關(guān)注問題
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號