开发者

$get multiple variables from url

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

foreach($_GET as $value)
{
$event = $value;
}
$event = mysql_real_escape_string($ev开发者_JAVA技巧ent);

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

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

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

Thank You.


These will automatically show up in these variables...

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

PHP does all of this work for you.


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


Each url parameter becomes a separate key in $_GET.

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

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

and you can act accordingly:

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


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.

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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜