Jump to content

How do I build off this?


Klance

Recommended Posts

Ok so I have a page called index.php that has this:

<?php
if(!empty($_GET['id']) && file_exists("./$_GET[id].php")){
include("./$_GET[id].php");
}
?>
<a href="?id=hello">Hello Page</a>

 

then that makes a page called index.php?id=hello

 

Now, I want a page to come off of that so that it would be index.php?id=hello&id2=goodbye

 

I've tried to just put this in hello.php:

 

<?php
if(!empty($_GET['id2']) && file_exists("./$_GET[id2].php")){
include("./$_GET[id2].php");
}
?>
<a href="?id2=goodbye">Goodbye Page</a>

 

and i figured it would just build off of index.php?id=hello but it just makes index.php?id=goodbye

 

Should I use the $_SERVER['PHP_SELF'] feature? I'm not too sure what to do here.

Link to comment
Share on other sites

Lots of solutions here.

 

You *could* append to $_SERVER[REQUEST_URI].

 

<a href="<?=$_SERVER[REQUEST_URI]?>&id2=goodbye">Goodbye Page</a>

 

... It looks like you may just be learning.  But you may want to modify your logic so "id" doesn't change (also, perhaps the variable name "action" maybe better) then use switch statements.

 

<?php
if(!empty($_GET['id']) && file_exists("./$_GET[id].php")){
include("./$_GET[id].php");
}

switch($_GET['id']) {
     case 'hello': 
         echo "<a href=\"?id=hello\">Hello Page</a>";
         break;
     case 'goodbye': 
         echo "<a href=\"?id=goodbye\">Goodbye Page</a>";
         break;
}

?>

 

Best, Nathan

Link to comment
Share on other sites

Lots of solutions here.

 

You *could* append to $_SERVER[REQUEST_URI].

 

<a href="<?=$_SERVER[REQUEST_URI]?>&id2=goodbye">Goodbye Page</a>

 

... It looks like you may just be learning.  But you may want to modify your logic so "id" doesn't change (also, perhaps the variable name "action" maybe better) then use switch statements.

 

<?php
if(!empty($_GET['id']) && file_exists("./$_GET[id].php")){
include("./$_GET[id].php");
}

switch($_GET['id']) {
     case 'hello': 
         echo "<a href=\"?id=hello\">Hello Page</a>";
         break;
     case 'goodbye': 
         echo "<a href=\"?id=goodbye\">Goodbye Page</a>";
         break;
}

?>

 

Best, Nathan

 

The reason I don't want to use the switch-case method is because i'm trying to get it to build off eachother, to get an effect like this: http://www.scorehero.com/top_scores.php

Link to comment
Share on other sites

In that case, you can use the REQUEST_URI, as explained in the first example.

 

However if it is to be architected like the example link, I would still use the switch case to determine what main "action" they are performing and then in the suburl's just include what is relevant from the current information.

 

<?php
if(!empty($_GET['id']) && file_exists("./$_GET[id].php")){
include("./$_GET[id].php");
}

print("<a href=\"?id=hello\">Hello Page</a> | <a href=\"?id=goodbye\">Goodbye Page</a><br><br>");

switch($_GET['id']) {
     case 'hello': 
         break;
         echo "<a href=\"?id=".$_GET['id']."&sub_id=Bob\">Hello Bob</a>";
         echo "<a href=\"?id=".$_GET['id']."&sub_id=John\">Hello John</a>";
     case 'goodbye': 
         echo "<a href=\"?id=".$_GET['id']."&sub_id=Bob\">Goodbye Bob</a>";
         echo "<a href=\"?id=".$_GET['id']."&sub_id=John\">Goodbye John</a>";
         break;
}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.