如何从元素获取值并使用循环添加到具有特定名称的数组

时间:2022-05-07 14:31:21

I am currently sending a query variable to a php file like this:

我目前正在向这样的php文件发送一个查询变量:

      query = "?price_from=" +price_from+ "&price_to=" + price_to etc etc...

I want to populate this query variable by using a loop to check if the user has chosen any search criteria in dropdown lists that exists on the page... It is only optional to chose the search criterias...

我想通过使用循环来填充此查询变量,以检查用户是否在页面上存在的下拉列表中选择了任何搜索条件...选择搜索标准只是可选的...

How do i do this?

我该怎么做呢?

Remember if the drop lists have the default value of 001, then the user havent entered any criteria (ex price range). So then i want the loop to skip this drop list, and move to the next. And if it finds any value other than 001, I want it to populate the query variable.

请记住,如果下拉列表的默认值为001,则用户没有输入任何条件(除价格范围)。那么我希望循环跳过此下拉列表,然后移动到下一个。如果它找到除001以外的任何值,我希望它填充查询变量。

The main problem is that the php file uses var names such as "price from" and "price to" from the query variable, and if nothing is set in the variable "price to" then it will just print "undefined" or something...

主要问题是php文件使用var名称,例如查询变量中的“price from”和“price to”,如果在“price to”变量中没有设置任何内容,那么它将只打印“undefined”或其他内容。 ..

Help!

帮帮我!


     <html> 
     <?php 

      foreach($_GET as $key => $value) { 
      if($value!='001') { 
            echo $key . ' has a value of ' . $value . '<br>'; 
      } 
      } 

       ?> 
      </html> 

how can i implement a loop into this that takes all values of keys and adds to a query and then checks mysql database for it?

我怎样才能实现一个循环到这个获取键的所有值并添加到查询然后检查mysql数据库呢?

3 个解决方案

#1


0  

<html>
 <?php
     $where = '';
     foreach($_GET as $key => $value) {
        if($value!='001') {
            $where .= ' AND ' . $key . '="' . $value . '"';
        }
     }
     $where = substr($where,4);//get rid of left 'AND'
     $sql = 'SELECT * FROM my_table WHERE ' . $where . ' ORDER BY id ASC';
 ?>
</html>

#2


0  

for the html why dont you put the form method to GET .. and submit the form with AJAX. Why does it bother you the default values ?

对于HTML,为什么不把表单方法放到GET ..并使用AJAX提交表单。为什么它会打扰你的默认值?

You can do verifications on the php side and decide there witch filters are active

您可以在php端进行验证,并确定有过滤器处于活动状态

$price = (isset($_GET['price'])) ? $_GET['price'] : false;

#3


0  

$url = $_SERVER['SCRIPT_NAME'] . '?';

foreach ($_GET as $key => $value)
{
    $url .= $key . '=' . $value . '&';
}

#1


0  

<html>
 <?php
     $where = '';
     foreach($_GET as $key => $value) {
        if($value!='001') {
            $where .= ' AND ' . $key . '="' . $value . '"';
        }
     }
     $where = substr($where,4);//get rid of left 'AND'
     $sql = 'SELECT * FROM my_table WHERE ' . $where . ' ORDER BY id ASC';
 ?>
</html>

#2


0  

for the html why dont you put the form method to GET .. and submit the form with AJAX. Why does it bother you the default values ?

对于HTML,为什么不把表单方法放到GET ..并使用AJAX提交表单。为什么它会打扰你的默认值?

You can do verifications on the php side and decide there witch filters are active

您可以在php端进行验证,并确定有过滤器处于活动状态

$price = (isset($_GET['price'])) ? $_GET['price'] : false;

#3


0  

$url = $_SERVER['SCRIPT_NAME'] . '?';

foreach ($_GET as $key => $value)
{
    $url .= $key . '=' . $value . '&';
}