$ get multiple variables from url

I make a site for photography. The database has an image table, an event table, and a category table that are linked through foreign keys. At the moment, I am generating events when a photograph was taken from a database and turned 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, the script gets the variable from get in the URL: /images.php?**event**=eventCalledParty

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

My question is if I had to run categories and have a url that looks like this:

?

/images.php event = eventCalledParty & category = categoryCalledFashionPhotography

How would I isolate these two variables from a URL for use in my queries.

Thanks.

+4
source share
5 answers
 $event = mysql_real_escape_string($_GET['event']); $category = mysql_real_escape_string($_GET['category']); 
+5
source

They will be automatically displayed in these variables ...

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

PHP does all this for you.

+6
source

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 } 
+1
source

I'm a little rusty in php, but I think in your foreach you need to get the field name 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); } } 
+1
source

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

+1
source

All Articles