I am looking for the best way to 'skip' a PHP script to pass directly to my HTML code. Here's an example:
<?php
include 'ws/main.php';
include 'ws/database/connection.php';
$error = false;
$routes = getCurrentParameters();
if (!isset($routes[0]) && !$error)
{
$error = traduction("No promotion entered in the URL");
goto end;
}
else if (!isset($routes[1]) && !$error)
{
$error = traduction("No page entered in the URL");
goto end;
}
if (!$error)
{
$result = $connection->query("SELECT * FROM promotion WHERE name = '$routes[0]'");
$promo = $result->fetch_row();
if (empty($promo) && !$error)
{
$error = traduction("The promotion '". $routes[0] ."' do not exist");
goto end;
}
else if (!file_exists("page/" . $routes[1] . ".php"))
{
$error = traduction("The page '". $routes[1] ."' do not exist");
goto end;
}
$result = $connection->query("SELECT * FROM traduction WHERE promotion_id = $promo[0]");
$traduction = $result->fetch_all();
}
end:
?>
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Start CSS import -->
<link rel='stylesheet' href='<?php echo BASE_PATH ?>assets/css/bootstrap.css' />
<link rel='stylesheet' href='<?php echo BASE_PATH ?>assets/css/font-awesome.css' />
<link rel='stylesheet' href='<?php echo BASE_PATH ?>assets/css/animate.css' />
<link rel='stylesheet' href='<?php echo BASE_PATH ?>assets/css/main.css' />
<!-- End CSS import -->
</head>
<body>
<?php if (!$error) // If the app do not catch any error
{
include "page/$routes[1].php";
}
else // If the app already catch an error
{
// Include the error.php page
include "page/error.php";
} ?>
<!-- Start JS import -->
<script type='application/javascript' src='<?php echo BASE_PATH ?>assets/js/jquery.js'></script>
<script type='application/javascript' src='<?php echo BASE_PATH ?>libs/js/tether/dist/js/tether.js'></script>
<script type='application/javascript' src='<?php echo BASE_PATH ?>assets/js/bootstrap.js'></script>
<script type='application/javascript' src='<?php echo BASE_PATH ?>assets/js/moment.js'></script>
<script type='application/javascript' src='<?php echo BASE_PATH ?>assets/js/bootbox.js'></script>
<script type='application/javascript' src='<?php echo BASE_PATH ?>assets/js/sweetalert.js'></script>
<script type='application/javascript' src='<?php echo BASE_PATH ?>assets/js/main.js'></script>
<!-- End JS import -->
<script>
$(".img").css("background", "url(<?php echo $promo[2]; ?>) no-repeat center center")
</script>
</body>
</html>
So, if you have read all my code, wich is not complicated at all, I am just looking for the best way to quit my PHP script, in the top, to go dirrectly to my HTML code when I have an issue in my database or the information just do not match. Currently I use the 'goto' method with a tag 'end:' in the end of the script. I know that 'goto' method isn't optimal at all, so I'm looking for a better way to do it cuz I'm pretty sure there's already an existing method.
If you put the code you potentially want to run into a function, or as a method in a class, then it's a simple matter to run that code - or choose to return out of it early, without doing anything.
<?php
function doThings($error, $routes) {
{
$traduction = array();
if (!isset($routes[0]) && !$error) {
return $traduction; // leave the function without doing anything
}
// the rest of the checks and actions, to get the database results
return $traduction;
}
$traduction = doThings($error, $routes);
// loop over the (maybe empty $traduction variable) to output
?>