我想根据之前的下拉菜单查询一个下拉菜单的项目列表

发布于 2024-10-28 11:57:50 字数 272 浏览 2 评论 0原文

如何根据另一个查询结果查询下拉列表

,以便我有一个函数 getMakes() ,它返回我放置在下拉列表中的车辆品牌列表,

我在函数中创建了一个新查询getModels() 获取每个车辆品牌的所有可能模型,我想制作一个 where 语句,仅获取所选车辆品牌的品牌

我想拥有该功能 getModels($make) 其中 $make是我们在where子句中想要的值,但是如何使where子句中的值动态化呢?

How do I query a drop down according to another queries result

so I have a function getMakes() that returns a list of vehicle makes that i have placed in a dropdown,

I created a new query in the function getModels() that gets all the possible models for every vehicle make, i want to make a where statement that only grabs the makes for the vehicle make that was selected

I was thinking to have the function getModels($make) where $make is the value we want in the where clause, but how do i make the value in the where clause dynamic?

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

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

发布评论

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

评论(2

Saygoodbye 2024-11-04 11:57:50

如果您希望根据第一个下拉列表中的选择填充第二个下拉列表,则需要使用 ajax 或 javascript。在这种情况下,您不能让 PHP 完成这项工作; PHP 将在页面加载时运行一次,仅此而已。

您可以拥有主控制器:

function index(){
    $l1=$this->db->query('SELECT make FROM cars');
    $l1arr = Array();
    foreach($l1 as $key=>$value){
        $l1arr[$key]=$value;
    }
    $data['list1']=$l1arr;
    $this->load->view('welcome_message',$data);
}

然后是返回模型的另一个控制器:
[假设您在 url 中传递了 make],

function car_models(){
    $make = $this->uri->segment(2);//the make passed in URL e.g. site.com/getcars/honda
    $models=$this->db->query('SELECT models FROM cars WHERE make="',$make,'"');
    $marr = array();
    foreach($marr as $key=>$value){
        $marr[$key]=>$value;
    }
    return form_dropdown('models_dropdown',$marr);
}

然后让 ajax 函数根据初始选择调用模型页面;你可以用 jQuery 来做到这一点,如下所示:

function getModels(make){
    jQuery.ajax({
      type: 'GET',
      url: '/getcars/'+make,
      success: function(data){
          //populate dropdown with models
          jQuery('#models_dropdown').html(data);
      }
    });
    return false;
}         

不确定我的所有细节是否正确,但这至少应该为你指明正确的方向。

You will need to use ajax or javascript if you want to have the second dropdown populate based on a selection from the first dropdown. You can't have PHP do the work in this case; PHP will run once when the page loads and that's it.

You can have your main controller:

function index(){
    $l1=$this->db->query('SELECT make FROM cars');
    $l1arr = Array();
    foreach($l1 as $key=>$value){
        $l1arr[$key]=$value;
    }
    $data['list1']=$l1arr;
    $this->load->view('welcome_message',$data);
}

then another controller that returns the models:
[assuming you pass the make in the url]

function car_models(){
    $make = $this->uri->segment(2);//the make passed in URL e.g. site.com/getcars/honda
    $models=$this->db->query('SELECT models FROM cars WHERE make="',$make,'"');
    $marr = array();
    foreach($marr as $key=>$value){
        $marr[$key]=>$value;
    }
    return form_dropdown('models_dropdown',$marr);
}

then have an ajax function call the models page based on the initial selection; you can do it with jQuery like so:

function getModels(make){
    jQuery.ajax({
      type: 'GET',
      url: '/getcars/'+make,
      success: function(data){
          //populate dropdown with models
          jQuery('#models_dropdown').html(data);
      }
    });
    return false;
}         

not sure I got all the details correct, but this should at least point you in the right direction.

故事和酒 2024-11-04 11:57:50

您不能让 $sql 字符串使用某种字符串连接来生成 SQL 查询吗?应该进行一些检查来防止 SQL 注入攻击,因为从安全角度来看,这样做可能有点风险。

如果您需要更多帮助,PHP 字符串连接 将是一个教程。

Couldn't you make the $sql string use some string concatenation to produce the SQL query? There should be some checking to prevent a SQL injection attack as doing this can be a little risky from a security perspective.

PHP string concatenation would be a tutorial if you want some more help with that.

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