I Am currently writing My first big project.
I have a navbar with many links that now are linking to "index.PHP?a=value&b=value"
On My index page i have the following:
if((($_GET['a']) == "value") && (($_GET['b']) == "value")) {
*** soms code ***
}
if((($_GET['a']) == "value") && (($_GET['b']) == "value2")) {
*** soms code ***
}
Etc...
But with 50 are more links My index.PHP file Will get a Little bit clutterd.
I have thinking of changing the code to:
if((($_GET['a']) == "value") && (($_GET['b']) == "value")) {
include("somefile.php");
}
Wat is the best way to go around this ? Maybe the onclick=.. ?
Please your thoughts on this
you can simplify like that
if ($_GET['a'] == "value") {
if ($_GET['b'] == "value") {
*** soms code ***
} elseif ($_GET['b'] == "value2") {
*** soms code ***
} elseif ($_GET['b'] == "value3") {
*** soms code ***
}
// ...
}
or if you want to have your code in more files, try this :
if ( isset($_GET["a"])
&& isset($_GET["b"])
) {
// security : cut all but a-z
$a = preg_replace("![^a-z]!", "", $_GET["a"]);
$b = preg_replace("![^a-z]!", "", $_GET["b"]);
// include the file
include "files/part1-$a-part2-$b.php";
}
You write a big project. Why don't you use a serious framework as Laravel, etc? That make it less complex for you to make a scalable and maintainable application.
Separate routing from your index. Separate responsibilities (presentation, business logic, etc.)
Read more about effective routing in Laravel: http://laravel.com/docs/master/routing#basic-routing
This is a problem solved long ago. In modern frameworks it is called routing, you can look it up.
A simplified example would be to have an array mapping values to files on the system, and then call a function to include the appropriate file.
$routes = [
'value1' => ['value1' => 'file1.php'],
'value2' => ['value2' => 'file2.php']
]
function route($value1, $value2) {
global $routes;
require($routes[$value1][$value2];
}
Then in index.php you just call
route($_GET['a'], $_GET['b']);
This example is not to be taken literally, it is just an illustration
As suggested in another answer, it is probably a good idea to consider using a framework. They exist for a reason.