如何从数据库中的 table 中获取数据并显示在 header 小部件中?

How to fetch data from a table in database and show in header widget?

我有一个 table 名称类别,其中包含所有类别的商家。我想在我的 header 小部件中显示这些类别?但我不知道如何从数据库中获取类别数据并显示在我的 header 小部件中?请任何帮助?

<header id="main-header">
<div class="header-top">
<div class="container">
<div class="row">
<div class="col-md-3">
    <a class="logo" href="<?php echo Yii::app()->request->baseUrl;?>/index.php">
        <img src="<?php echo Yii::app()->request->baseUrl;?>/img/abc.png" alt="Image Alternative text" title="Image Title" />
    </a>
</div>

<div class="col-md-6 col-md-offset-3">
    <div class="top-user-area clearfix">
        <ul class="top-user-area-list list list-horizontal list-border">
                    <?php if(Yii::app()->user->isGuest){?>
<li><a href="<?php echo Yii::app()->request->baseUrl;?>/user/registration">Sign up</a></li>
            <li><a href="<?php echo Yii::app()->request->baseUrl;?>/user/login">login</a></li>
<?php }?>

                 <?php if(!Yii::app()->user->isGuest){?>
                               <ul class="slimmenu" id="slimmenu">

             <li><a href="#"><span style="color:#fff;font-size: 12px;">&nbsp;<?php echo //yii::app()->user->name; 
           Yii::app()->getModule('user')->user()->profile->firstname?> </span></a>
    <ul>
         <li><a href="<?php echo Yii::app()->request->baseUrl;?>/user/profile">Profile</a>
<li><a href="<?php echo Yii::app()->request->baseUrl;?>/user/logout">logout</a>
<?php 
$roles=Rights::getAssignedRoles(Yii::app()->user->Id);
foreach($roles as $role)
if($role->name == 'Authenticated')
{
?> 
        </li><?php }?>
</ul>
    </ul>

<?php }?>


        </ul>
    </div>
</div>
</div>
</div>
</div>
<div class="container">
<div class="nav">
<ul class="slimmenu" id="slimmenu">
<li class="active"><a href="<?php echo Yii::app()->request->baseUrl;?>/index.php"><span><i class="fa fa-home"></i>&nbsp;Home</span> </a>

</li>
<li><a href="<?php echo Yii::app()->request->baseUrl;?>/about/"><span><i class="fa fa-info-circle"></i>&nbsp;About Us</span></a>

</li>
<li><a href="#"><span><i class="fa fa-briefcase"></i>&nbsp;Businesses</span></a>
    <ul>
        <li><a href="<?php echo Yii::app()->request->baseUrl;?>/restaurants.php">Restaurants</a>

        </li>
        <li><a href="<?php echo Yii::app()->request->baseUrl;?>/fast-food.php">Fast-Food</a>

        </li>
        <li><a href="<?php echo Yii::app()->request->baseUrl;?>/wedding-halls.php">Wedding Halls</a>

        </li>
        <li><a href="<?php echo Yii::app()->request->baseUrl;?>/catering.php">Catering</a>


        </li>
        <li><a href="<?php echo Yii::app()->request->baseUrl;?>/photography.php">Photography</a>

        </li>

        <li><a href="<?php echo Yii::app()->request->baseUrl;?>/spaandbeauty.php">Spa and Beauty</a>

        </li>
        <li><a href="<?php echo Yii::app()->request->baseUrl;?>/realestate.php">Real Estate</a>

        </li>

    </ul>
</li>

<li><a href="<?php echo Yii::app()->request->baseUrl;?>/write-review.php"><span><i class="fa fa-pencil"></i>&nbsp;Write Review<span></a>

                </li>
                <li><a href="<?php echo Yii::app()->request->baseUrl;?>/utility-finder.php"><span><i class="fa fa-pencil"></i>&nbsp;Utility Finder<span></a>

                                </li>

                                </ul>
                                </div>
                                </div>
                                </header>

上面是我的 header 小部件视图,您可以在业务中看到,我想从我的数据库中获取类别,这是静态的,我想使其成为动态的。

<?php

/**
 * This is the model class for table "category".
 *
 * The followings are the available columns in table 'category':
 * @property integer $id
 * @property string $category_name
 *
 * The followings are the available model relations:
 * @property SubCategoryBusiness[] $subCategoryBusinesses
 */
class Category extends CActiveRecord
{
    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'category';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('category_name', 'length', 'max'=>45),
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            array('id, category_name', 'safe', 'on'=>'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'subCategoryBusinesses' => array(self::HAS_MANY, 'SubCategoryBusiness', 'category_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'id' => 'ID',
            'category_name' => 'Category Name',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     *
     * Typical usecase:
     * - Initialize the model fields with values from filter form.
     * - Execute this method to get CActiveDataProvider instance which will filter
     * models according to data in model fields.
     * - Pass data provider to CGridView, CListView or any similar widget.
     *
     * @return CActiveDataProvider the data provider that can return the models
     * based on the search/filter conditions.
     */
    public function search()
    {
        // @todo Please modify the following code to remove attributes that should not be searched.

        $criteria=new CDbCriteria;

        $criteria->compare('id',$this->id);
        $criteria->compare('category_name',$this->category_name,true);

        return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
        ));
    }

    /**
     * Returns the static model of the specified AR class.
     * Please note that you should have this exact method in all your CActiveRecord descendants!
     * @param string $className active record class name.
     * @return Category the static model class
     */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }
}

以上是我的品类模型

我是通过 cdbcriteria 完成的,这是我的 headerwidget 视图

<ul>
       <?php $criteria = new CDbCriteria(array('order'=>'id ASC'));
$model = Category::model()->findAll($criteria);
$name=array();
$id=array();

foreach ($model as $list) {

          $names[] = $list->category_name;
          $id[]=$list->id;
                }  

        for($i=0;$i<8;$i++){
    ?>
        <li>
            <a href="<?php echo Yii::app()->request->baseUrl;?>/<?php echo $id[$i];?>.php"><?php echo $names[$i];?> </a>

        </li>

      <?php  }?>




    </ul>