如何在 Yii 中渲染 216,912 行城市数据而不挂起?

发布于 2024-10-04 16:48:18 字数 727 浏览 0 评论 0原文

我在城市下拉菜单方面遇到问题。情况是这样的, 我有3张桌子。即国家、州、城市,我能够生成一个下拉菜单 对于国家、州..我也能够为城市生成下拉菜单,但是问题是,当我将代码推送到生产服务器时,由于内存消耗过多,页面变成白色,这是我的代码

//model
    public function fetchCityName()
    {
        $connection = Yii::app()->db->createCommand("SELECT CityName from gg_t_worldareascities ORDER BY CityName ASC")->queryColumn();
        return $connection;

    }

//view
        <?php 
            $this->widget('CAutoComplete', array(
                'model' => $model,
                'attribute' => 'cityID',
                'data' => $model->fetchCityName(),
                'multiple' => false,
                'htmlOptions' => array('size' => 25),
            ));
        ?>

I am having a problem with a city dropdown menu. the situation is like this,
I have 3 tables. namely countries, states, cities, I was able to generate a dropdown menu
for Countries, States.. I was able to generate a dropdown menu for Cities also but, the problem is, when I pushed the code to production server, the page turned white due to too much memory consumption, here's my code

//model
    public function fetchCityName()
    {
        $connection = Yii::app()->db->createCommand("SELECT CityName from gg_t_worldareascities ORDER BY CityName ASC")->queryColumn();
        return $connection;

    }

//view
        <?php 
            $this->widget('CAutoComplete', array(
                'model' => $model,
                'attribute' => 'cityID',
                'data' => $model->fetchCityName(),
                'multiple' => false,
                'htmlOptions' => array('size' => 25),
            ));
        ?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

世俗缘 2024-10-11 16:48:18

如果您有 216,912 个 option 元素,它应该敲响警钟,告诉您有更好的方法:)

我可以建议

  • Autocomplete 使用 AJAX 查询您的服务器。
  • 首先选择邮政编码,然后显示匹配的郊区。

If you have 216,912 option elements, it should ring an alarm bell that there is a better way :)

May I suggest

  • Autocomplete that queries your server with AJAX.
  • Pick postcode first, then show matching suburbs.
深白境迁sunset 2024-10-11 16:48:18

我不做 Yii,但是国家、州、城市之间应该有某种关系吧?没有人会选择台北市然后选择美国加利福尼亚州作为国家和州。

对于州和城市,我建议仅在用户选择国家/州后通过 Javascript/AJAX 获取可接受的选择。

I don't do Yii, but there should be some kind of relationships between countries, states, cities right? No one would select city of Taipei then choose CA, U.S.A. as the country and state.

For the States and Cities, I would suggest fetch the acceptable choices via Javascript/AJAX only after the user have selected the Country/State.

枕花眠 2024-10-11 16:48:18

请参阅有关在 Yii 中创建依赖下拉菜单的 wiki:

http://www.yiiframework.com/wiki/24/creating-a-dependent-dropdown/" yiiframework.com/wiki/24/creating-a-dependent-dropdown/

我认为你最好使用自动完成功能 - 文档在这里:

http://www.yiiframework.com/doc/api/1.1/CJuiAutoComplete

它是广泛使用的 jQuery UI Autocomplete。

See this wiki on creating dependent dropdowns in Yii:

http://www.yiiframework.com/wiki/24/creating-a-dependent-dropdown/

I think you'd be better going with an autocomplete though - the docs are here:

http://www.yiiframework.com/doc/api/1.1/CJuiAutoComplete

It is the jQuery UI Autocomplete which is widely used.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文