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
https://forums.phpfreaks.com/topic/99312-how-do-i-build-off-this/
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

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

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

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.