php $从url获取多个变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3718051/
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 10:50:40  来源:igfitidea点击:

$get multiple variables from url

phpget

提问by slexAtrukov

I am making a photography website. In the DB there is an images table, events table and category table which as linked via foreign keys. At the moment i generate events from when a photo was taken from the database and turn them into anchor links.

我正在制作一个摄影网站。在数据库中有一个图像表、事件表和类别表,它们通过外键链接。目前我从数据库中拍摄照片时生成事件并将它们转换为锚链接。

<?php
while ( $a_row = mysql_fetch_row( $result ) ){

 foreach ( $a_row as $field ){

?>
<a href="images.php?event=<?php echo $field;?> "> <php? echo $field; ?> </a>
<?php
 }
}
?>

When the link is clicked a script gets the variable from get in the url: /images.php?**event**=eventCalledParty

单击链接时,脚本会从 url 中的 get 获取变量: /images.php?**event**=eventCalledParty

foreach($_GET as $value)
{
$event = $value;
}
$event = mysql_real_escape_string($event);

My question is - If i was to implement categories and have a url that looks like:

我的问题是 - 如果我要实现类别并有一个如下所示的网址:

/images.php?event=eventCalledParty&category=categoryCalledFashionPhotography

/images.php?event=eventCalledParty& category=categoryCalledFashionPhotography

How would i seperate these two variables from the url to use in my queries.

我将如何将这两个变量与 url 分开以在我的查询中使用。

Thank You.

谢谢你。

回答by Brad

These will automatically show up in these variables...

这些将自动显示在这些变量中...

$_GET['event']
$_GET['category']

PHP does all of this work for you.

PHP 为您完成所有这些工作。

回答by Your Common Sense

$event = mysql_real_escape_string($_GET['event']);
$category = mysql_real_escape_string($_GET['category']);

回答by Dale Hurley

foreach($_GET as $key => $value) { $event[$key] = mysql_real_escape_string($value); }

foreach($_GET as $key => $value) { $event[$key] = mysql_real_escape_string($value); }

回答by Fanis Hatzidakis

Each url parameter becomes a separate key in $_GET.

每个 url 参数成为$_GET.

So, for /images.php?event=eventCalledParty&category=categoryCalledFashionPhotography you will get:

因此,对于 /images.php?event=eventCalledParty&category=categoryCalledFashionPhotography,您将获得:

$_GET['event'] => 'eventCalledParty' 
$_GET['category'] => 'categoryCalledFashionPhotography' 

and you can act accordingly:

你可以采取相应的行动:

if ($_GET['category'] == 'categoryCalledFashionPhotography') {
    //do stuff here
}

回答by davydotcom

Im a bit rusty on php but I think in your foreach what you want is to get the name of teh field as well as its value.

我对 php 有点生疏,但我认为在你的 foreach 中你想要的是获取 teh 字段的名称及其值。

foreach($_GET as $name->$value)
{
  if($name == 'event') {
    $event = mysql_real_escape_string($value);
  }
  else if($name == 'category')
  {
    $category = mysql_real_escape_string($category);
  }
}