php 如何通过 URL 传递变量的多个值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11925228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:28:02  来源:igfitidea点击:

How to pass multiple values of a variable through a URL

phpurlurlvariables

提问by mickburkejnr

I've an application that I'm building, and I'm stuck at a certain point.

我有一个正在构建的应用程序,但我遇到了某个问题。

I'm trying to pass a variable that has multiple values. So my URL will look like:

我正在尝试传递一个具有多个值的变量。所以我的网址看起来像:

localhost/report.php?variable=value1, value2, value3

Problem is I'm not sure how I can do this. The variables are to be used to retrieve data from a database. I'm using PHP and no Javascript.

问题是我不确定如何做到这一点。这些变量用于从数据库中检索数据。我正在使用 PHP 而没有 Javascript。

Any help would be great!

任何帮助都会很棒!

EDIT:
Here is the HTML I have in my page where the variables are selected:

编辑:
这是我在页面中选择变量的 HTML:

<select name="types" size="19" multiple>
    <option value="all" selected>All Types</option>
    <option value="book" selected>Books</option>
    <option value="cd" selected>CD</option>
</select>

So a user could select Books and CD, and I would need to pass these two values in the "types" variable.

所以用户可以选择 Books 和 CD,我需要在“types”变量中传递这两个值。

采纳答案by DACrosby

As noted at https://stackoverflow.com/a/2407401/1265817, you can use this method.

https://stackoverflow.com/a/2407401/1265817 所述,您可以使用此方法。

If you want PHP to treat $_GET['select2'] as an array of options just add square brackets to the name of the select element like this: <select name="select2[]" multiple …

如果您希望 PHP 将 $_GET['select2'] 视为一个选项数组,只需在 select 元素的名称中添加方括号,如下所示: <select name="select2[]" multiple …

Then you can acces the array in your PHP script

然后你可以访问你的 PHP 脚本中的数组

<?php
header("Content-Type: text/plain");

foreach ($_GET['select2'] as $selectedOption)
  echo $selectedOption."\n";

回答by jp2code

Use &

&

localhost/report.php?variable=value1&val2=value2&val3=value3

Is that what you are after?

这就是你所追求的吗?

回答by Chris

There is a simple way to do this in PHP by calling http_build_query()and pass your values as an indexed array. You would do something like:

在 PHP 中有一种简单的方法可以通过调用http_build_query()和传递您的值作为索引数组来执行此操作。你会做这样的事情:

$value_array = array('types' => array('book', 'cd'));
$query = http_build_query($value_array);

Then generate the url using $query.

然后使用$query.

回答by DACrosby

I think you have your URL correct

我认为您的网址正确

localhost/report.php?variable=value1,value2,value3

Then use PHP to get all of the values on the report.php page

然后使用PHP获取report.php页面上的所有值

$variable = explode(",", $_GET["variable"]);

// Output
$variable[0] = "value1";
$variable[1] = "value2";
$variable[2] = "value3";

回答by WayneC

try localhost/report.php?variable[]=value1&variable[]=value2will give you an array in php

尝试localhost/report.php?variable[]=value1&variable[]=value2会给你一个 php 数组

回答by MAMProgr

You can use serializelike:

您可以使用序列化,如:

echo '<a href="index.php?var='.serialize($array).'"> Link </a>';

and get data using unserializelike:

并使用反序列化获取数据,例如:

$array= unserialize($_GET['var']);

serialize function giving you storable (string) version of array type, and can be restored by unserialize function.

序列化函数为您提供数组类型的可存储(字符串)版本,并且可以通过反序列化函数恢复。