PHP include

Status
Niet open voor verdere reacties.

Just1

Gebruiker
Lid geworden
18 jan 2010
Berichten
118
Ik heb deze code in het bestand index.php:
PHP:
<?php
$presets = array(
            "a" => "1.htm",
            "b" => "2.htm",
            "c" => "3.htm",
            );
       
$startpagina = 'start.htm';

// Pagina bepalen
$x = $_GET['pagina'];
 
if(isset($presets["$x"])) 
{
    if(strpos($presets["$x"], 'http://')) 
    {
    $pagina = $presets["$x"];
    } 
    else 
    {
    $pagina = 'http://' . $presets["$x"];
    }    
} 
else 
{
    $pagina = $startpagina;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Titel</title>
<link href="css.css" rel="stylesheet" type="text/css" />
</head>
<body>
<iframe class="frame" name="frame" frameborder="0" src="<?php echo $pagina; ?>" width="200" height="200"></iframe>
</body>
</html>
Dit zorgt er dus voor dat als je naar index.php?pagina=a de pagina 1.htm in het frame krijgt enz.
En ja, ik weet het, frames zijn oud, daarom zou ik graag in plaats van dat frame een php include neerzetten, zoiets als <?php include $pagina; ?> (dat werkt dus niet) maar hoe doe ik dat?
Just
 
Zet dit in index.php op de plek waar de pagina´s moeten laten:

Code:
<?php
include('inc/home.php');
include('inc/pagina1.php');
include('inc/pagina2.php');

switch($_GET['action'])
{
case 'pagina1': pagina1(); break;
case 'pagina2': pagina2(); break;

default: home(); break;

}


?>

en dit in de pagina die wordt geinclud:

Code:
<?php function pagina1() { ?>
De pagina code.
<?php } ?>

en dit ook voor de andere pagina´s bijv:
Code:
<?php function home() { ?>
Welkom op mijn site.<br />
<?php } ?>

<html><head> en dat soort dingen alleen in index.php

en vergeet niet dat volgens dit script de pagina´s die worden geinclud in het mapje inc moeten staan en met de .php ext
 
Laatst bewerkt:
Bedankt, ik ga het straks even proberen, maar is het nu niet zo dat index.php?pagina=... niet meer werkt?
Just
 
als je in
Code:
switch($_GET['action'])
action veranderd in pagina blijft het hetzelfde.

zo zou index.php er uit kunnen zien:

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Titel</title>
<link href="css.css" rel="stylesheet" type="text/css" />
</head>
<body>
<?php
include('inc/home.php');
include('inc/pagina1.php');
include('inc/pagina2.php');

switch($_GET['pagina'])
{
case 'pagina1': pagina1(); break;
case 'pagina2': pagina2(); break;

default: home(); break;

}


?>
</body>
</html>
 
elke pagina includen/een functie maken is een beetje overbodig:
PHP:
$page = isset($_GET['pagina']) ? $_GET['pagina'] : '';

switch($page)
{
   default:
   case 'pagina1':   include('1.html');   break;

   case 'pagina2':   include('2.html');   break;

   case 'pagina3':   include('3.html');   break;
}
 
Sorry voor de veel te late reactie, maar het werkt!
Moet ik echt in iedere pagina dit zetten:
PHP:
<?php function pagina1() { ?>
De pagina code.
<?php } ?>
Want het werkt ook zonder.
Wat ik nu heb:
PHP:
<?php
$page = isset($_GET['pagina']) ? $_GET['pagina'] : '';

switch($page)
{
   default:
   case 'home':   include('home.php');   break;
   case 'pagina2':   include('pagina2.php');   break;
   case 'contact':   include('contact.php');   break;
}?>
 
Laatst bewerkt:
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan