0

how do i create the url of the links? I want when users click on action it will redirect users to the action page.But i don't want to create pages for the links i have provided below.what i want to do is to display different genres type content on the same page;

here is my html

<ul class="snav">
    <li><a href="#">Action</a></li>
    <li><a href="#">Art and Experimental</a></li>
    <li><a href="#">Comedy</a></li>
    <li><a href="#">Crime and Mystery</a></li>
    <li><a href="#">Documentary</a></li>
    <li><a href="#">Drama</a></li>
    <li><a href="#">Epic and Historical</a></li>
    <li><a href="#">Family</a></li>
    <li><a href="#">Fantasy</a></li>
    <li><a href="#">Foreign</a></li>
    <li><a href="#">Horror</a></li>
    <li><a href="#">Live Performances</a></li>
    <li><a href="#">Musicals</a></li>
    <li><a href="#">Romance</a></li>
    <li><a href="#">Science Fiction</a></li>
    <li><a href="#">Special Interest</a></li>
    <li><a href="#">Sports</a></li>
    <li><a href="#">Thriller</a></li>
    <li><a href="#">War</a></li>
    <li><a href="#">Westerns</a></li>
</ul>
Nahydrin
  • 13,197
  • 12
  • 59
  • 101
Sunny
  • 17
  • 3
  • 1
    Are you familiar with [query strings](http://en.wikipedia.org/wiki/Query_string) (ie, `/test/demo_form.asp?name1=value1&name2=value2`)? That's probably what you're looking for. – admdrew Dec 03 '13 at 18:41
  • 1
    What you're trying to do isn't very clear. Could you possible rephrase your question? Also - Giving examples of what you've already tried is always good, then people are more likely to (be able to) help you. – Pete Hamilton Dec 03 '13 at 18:41
  • @admdrew no i am new to php – Sunny Dec 03 '13 at 18:44
  • @PeterHamilton what i want to do is to create the genre links url dynamically. – Sunny Dec 03 '13 at 18:45
  • @Sunny that was a bullshit vote but whatever. – will Dec 03 '13 at 19:01
  • @WillHarrison your answer was also right so does ross.but i cant accept two answer and i found ross's answer easy to understand that is why . – Sunny Dec 03 '13 at 19:18

3 Answers3

1

One way to accomplish this is to use query strings to form a url based off your base url.

http://domain.tld/index.php?genre=crime

index.php

if ($_GET['genre'] == 'crime') {
    // display crime genre
} else {
    // display index
}

You'll also want to include some validation/error checking when you're doing this. You can check whether or not the genre param is set using isset(). It is also important to escape the input to 'genre' so that injection attacks are less likely.

Community
  • 1
  • 1
will
  • 1,491
  • 1
  • 19
  • 28
1

To generate the links and change page content all in one go, you can try:

<?php
  $actions = array("Action", "Art and Experimental", "Comedy", "Crime and Mystery", "Documentary", "Drama", "Epic and Historical", "Family", "Fantasy", "Foreign", "Horror", "Live Performances", "Musicals", "Romance", "Science Fiction", "Special Interest", "Sports", "Thriller", "War", "Westerns");
?>

<ul class="snav">
<?php
foreach($actions as $action) {
    $url_token = str_replace(' ', '_', strtolower($action));
    echo '<li><a href="?genre=' . $url_token . '">' . $action . '</a></li>';
}
?>
</ul>

<?php
$genre = $_GET['genre'];

switch ($genre) {
    case 'action':
        echo 'Woo Action!';
        break;
    case 'drama':
        echo 'GASP!';
        break;
    case 'art_and_experimental':
        echo 'Weird...';
        break;
    default:
        echo 'There is no action called ' . $genre . ' :(';
}
?>

This will generate html of the form:

<ul class="snav">
    <li><a href="?genre=action">Action</a></li>
    <li><a href="?genre=art_and_experimental">Art and Experimental</a></li>
    ...
</ul>
Pete Hamilton
  • 7,730
  • 6
  • 33
  • 58
0

You're going to have to use $_GET variables using PHP and have something like:

Links look like

<a href="?genre=action">Action</a>

PHP file:

if (isset($_GET['genre'])) {
switch ($_GET['genre']) {
   case 'action':
      echo 'something here';
      break;
   case 'horror':
      echo 'something here';
      break;

  //etc
}
}

or you could use jQuery to dynamically load the data into the page.

Hope this helps!

Rwd
  • 34,180
  • 6
  • 64
  • 78
  • well it works fine.but i am getting an undefined index error:genre. – Sunny Dec 03 '13 at 19:01
  • I've just edited the answer. When working with REQUEST variables you should always preform some sort of validation to make sure they exist (at least). – Rwd Dec 03 '13 at 19:08
  • well sorry i unintentionally press the button! – Sunny Dec 03 '13 at 19:25
  • Haha, fair enough. No worries, dude. I just thought someone had said it was wrong or something. :p – Rwd Dec 03 '13 at 19:44
  • Saying that, I think I edited it after it was downvoted...if you wouldn't mind you could you try and upvote it? Cheers. – Rwd Dec 03 '13 at 19:46