Jump to content

Creating links


Unspoiled
Go to solution Solved by Barand,

Recommended Posts

Hi guys, i'm making a system with linking like this:

<?php   
	$link=$_GET['page'];
        if ($link == '1'){
            include ('page.php');
        }
        if ($link == '1-1'){
           include ('page.php');
        }
        if ($link == '1-2'){
            include ('page.php');
        }
        if ($link == '1-3'){
            include ('page.php');
        }
		if ($link == '3'){
            include ('page.php');
        }
		if ($link == '4'){
            include ('page.php');
        }
		if ($link == '5'){
            include ('page.php');
        }
		if ($link == '6'){
            include ('page.php');
		}
		else{
			include_once ('pages/home.php');
		 }
?>

And at the end I have the 

else{
    include_once ('pages/home.php');
}

But I want to have the else statement for all the if statements, someone has a solution?

Link to comment
Share on other sites

  • Solution

if ($link == '1'){
include ('page.php');
}
elseif ($link == '1-1'){
include ('page.php');
}
elseif ($link == '1-2'){
include ('page.php');
}
else {
include ('home.php');
}

// alternatively

switch ($link) {
case '1': include ('page.php');
break;
case '1-1': include ('page.php');
break;
case '1-2': include ('page.php');
break;
default: include ('home.php');
break;
}
Link to comment
Share on other sites

What are you trying to accomplish here? You're linking the same file in all cases. What is the point of all of those if statements?

I just changed the names to page.php, in my real code it's different ;)

 

 

if ($link == '1'){
   include ('page.php');
}
elseif ($link == '1-1'){
   include ('page.php');
}
elseif ($link == '1-2'){
    include ('page.php');
}
else {
    include ('home.php');
}

// alternatively

switch ($link) {
    case '1':   include ('page.php');
                break;
    case '1-1': include ('page.php');
                break;
    case '1-2': include ('page.php');
                break;
    default:    include ('home.php');
                break;
}

This solved it, used the elseif ;)

Link to comment
Share on other sites

For what it's worth, you could avoid all the elseifs and cases by creating an array of possible values...and then testing for the existence of a value. For example:

<?php
$pageReferences = array(
    '1'   => 'page.php',
    '1-1' => 'page.php',
    '1-2' => 'page.php'
);
 
$link = $_GET['page'];
if(isset($pageReferences[$link])){
    include $pageReferences[$link];
} else {
    include 'home.php';
}
?>
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.