4

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($event);

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.

2
  • Please note that your current code makes no sense. No foreach have to be used Commented Sep 15, 2010 at 13:40
  • I should have figured, i already knew post worked like that :/ thank you Commented Sep 15, 2010 at 13:53

5 Answers 5

6

These will automatically show up in these variables...

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

PHP does all of this work for you.

Sign up to request clarification or add additional context in comments.

2 Comments

I should also note that you shouldn't be escaping your data until you go to use it in your query. Otherwise it won't be of much use to you for anything else.
good point. it worth to add it to the answer, rather in comment
5
$event = mysql_real_escape_string($_GET['event']);
$category = mysql_real_escape_string($_GET['category']);

Comments

1

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
}

2 Comments

You need to be checking if $_GET['something'] is set first! Use empty() or isset().
@Brad most certainly so. I was mostly trying to focus slexAtrukov's attention on how $_GET is used step by step. Good advice, though.
1

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);
  }
}

2 Comments

what's the use of a loop here?
That should be foreach($_GET as $name=>$value).
1

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

1 Comment

register globals will make a funny mess out of this code :) Better to add $event=array(); above

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.