qiuxis

qiuxis

这家伙有点懒,还没写个性签名!

  • 财富值215
  • 威望值40
  • 总积分835

个人信息

  • 收藏了教程
    Yii 2.0 数据库操作增删改查详解
  • 发布了话题
    系列之四:yii2 下使用memcached(非memcache)
  • 回复了 的评论

    我的为什么会提示

    QQ截图20161028181545.png

    请帮我看一下,我贴上代码

    <?php
    
    namespace backend\models;
    
    use Yii;
    use yii\behaviors\TimestampBehavior;
    use yii\helpers\ArrayHelper;
    use yii\db\ActiveRecord;
    
    /**
     * This is the model class for table "category".
     *
     * @property string $cateid
     * @property string $catename
     * @property string $parentid
     * @property string $createtime
     */
    class Category extends \yii\db\ActiveRecord
    {
        public $tree = [];
        /**
         * @inheritdoc
         */
        public static function tableName()
        {
            return 'category';
        }
    
        /*
         *添加默认填充时间
         */
        public function behaviors() 
        {
            return [
                [
                    'class' => TimestampBehavior::className(),
                    'attributes' => [
                        ActiveRecord::EVENT_BEFORE_INSERT => ['createtime'],
                        ActiveRecord::EVENT_BEFORE_UPDATE => ['createtime'],
                    ],
                ],
            ];
        }
    
        /**
         * @inheritdoc
         */
        public function rules()
        {
            return [
                [['parentid'], 'integer'],
                [['catename'], 'string', 'max' => 32,'tooLong' => '分类名称太长了'],
                ['createtime', 'safe']
            ];
        }
    
        /**
         * @inheritdoc
         */
        public function attributeLabels()
        {
            
            return [
                'cateid' => Yii::t('app', 'Cateid'),
                'catename' => Yii::t('app', 'Catename'),
                'parentid' => Yii::t('app', 'Parentid'),
                'createtime' => Yii::t('app', 'Createtime'),
            ];
        }
        public function getClass()
        {
            return $this->hasOne(Category::className(),['parentid' => 'parentid']);
        }
    
        public static function find()
        {
            return new CategoryQuery(get_called_class());
        }
    
        public function getData()
        {
            $cates = Category::find()->all();
            $cates = ArrayHelper::toArray($cates);
            return $cates;
        }
    
        public function getCatTree($cats,$bclassid= 0, $nu = 0)
        {
            $bx = '---|';
            $nu++;
            foreach ($cats as $cat) {
                $catid = $cat['parentid'];
                $catname = $cat['catename'];
                $catbcid = $cat['bclassid'];
                if ($catbcid == $bclassid) {
                    $this->tree[$catid] = str_repeat($bx,$nu).'~'.$catename.PHP_EOL;
                    $this->getCatTree($cats,$catid,$nu);
                }
            }
        }
    
        public function tree()
        {
            $cats = $this->getData();
            $this->getCatTree($cats,0,0);
            return $this->tree ;
        }
    }
    
    

    这是model的代码,视图中是这样的

    <?= $form = ActiveForm::begin() ?>
    		<?= $form->field($model,'cateid')->dropDownList($model->tree(),['prompt'=> '请选择']) ?>
    	<?= $form->field($model,'catename')->textInput(['maxlength' => true]) ?>
    	<div class="form-group">
    		<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    	</div>
    	<?= ActiveForm::end() ?>
    

    先看看这个吧!
    需要有个父ID,即 bclassid = 0
    http://www.yiichina.com/tutorial/991

    另外建议先打印出结果

  • $old_array = $new_array[] = $ab_array = ['A','B','C','D','E','F'];
    function makeAb($old_array,$new_array,&$ab_array,$num=1)
    {
    $flip=array_flip($old_array);
    $old_num=count($old_array);
    foreach ($old_array as $key => $value ) {
    foreach ($new_array[$num-1] as $value_n) {
    if ($key < $flip[$value_n[0]] ) {
    	$ab_array[] = $new_array[$num][] = $value.$value_n;
    }
    }
    }
    	$num++;
    if ($num < $old_num ) {
        makeAb($old_array,$new_array,$ab_array,$num);
    }
    
    }
     makeAb($old_array,$new_array,$ab_array,1);
     var_dump($ab_array);
    
  • 发布了教程
    一个很简单的无限级分类
  • 发布了教程
    YII2 无限级分类实用
  • 赞了说说
    说实话 我觉得zendstdio sublime 好用多了,但是QQ输入法 太难用。 做编辑器建议大家使用zendstdio
  • 赞了说说
    祝大家中秋快乐,合家欢乐,万事如意!
主管 等级规则
835/1000
资料完整度
10/100
用户活跃度
0/100

Ta的关注

4

Ta的粉丝

21

Ta的访客

39