在终端上,在 mysql 中,运行以下查询会给出此结果
mysql> SELECT DISTINCT(city) FROM outlets_data;
+-----------+
| city |
+-----------+
| Paris |
| New York |
| Kolkata |
| Moscow |
| Mumbai |
| Hyderabad |
| Delhi |
| Chennai |
+-----------+
8 rows in set (0.00 sec)
我想将这些城市的名称以数组的形式存储在 codeigniter 4 模型类文件中。
模型/DashboardModels.php
<?php
namespace App\Models;
use CodeIgniter\Model;
class DashboardModel extends Model
{
protected $table = 'outlets_data';
protected $primaryKey = 'shop_id';
public function not_defined_yet()
{
$city_names = $this->select('city')->distinct(); // This should be equivalent to "SELECT DISTINCT(city) FROM outlets_data";
return $city_names;
}
}
控制器/Home.php
<?php
namespace App\Controllers;
use App\Models\DashboardModel;
use CodeIgniter\Model;
class Home extends BaseController
{
public function index()
{
$model = new DashboardModel();
$data['undefined'] = $model->not_defined_yet();
echo view('dashboard', $data);
}
}
视图/仪表板.php
<?php echo "<pre>"; print_r($undefined); echo "</pre>"; ?>
我希望在输出数组中获得城市的名称,但我将整个数据库作为关联数组。