Jump to content

[SOLVED] website include function


claffin

Recommended Posts

Hey, I feel a bit stupid asking this question but i've drawn a blank im using the include function to make a website having heading/nav/footer but I cannot for the life of me remember the code thats needed to open the links in the nav bar in the main section i remember doing it years ago but just cant seem to remember now. http://handweaving.seitservices.com.au/index3.php is the site im testing it on. I know the main.php isnt suppose be that just cant remember what it is suppose to be.

 

Here is what I have.

 

<table border="1" width="100%" height="0">

<tr>

<td height="120" width="100%" colspan="2"><?php include('heading.php');?> </td>

</tr>

<tr>

<td height="0" width="20%"><?php include('leftmenu.php');?> </td>

<td height="0" width="80%"><?php include('main.php');?> </td>

</tr>

<tr>

<td height="50" width="100%" colspan="2"><?php include('bottom.php');?> </td>

</tr>

</table>

 

 

leftmenu.php (not suppose to be what it is either)

 

<a href="../index.php">Home</a><p>

<a href="../aboutus.php">About Us</a><p>

<a href="../products.php">Products</a><p>

<a href="../gallery.php">Gallery</a><p>

<a href="../contactus.php">Contact Us</a><p>

 

Link to comment
https://forums.phpfreaks.com/topic/149622-solved-website-include-function/
Share on other sites

Hi!

You must change the links and pass the page names through them. And then fetch the value and store it in a variable, $page.

You should also have an array with all you files and check the array up against the value returned by the url.

 

Hope this helps you out.

 

 

Then you must change this:

 

<?php include('main.php');?>

 

To this:

 

<?php include($page);?>

 

Here is the code i came up with:

 

<?php
$page = 'main.php';   // Default page
$pages = array('main', 'aboutus','products','gallery','contactus');

// Check if p is set
if(isset($_GET['p'])) {

    // Check if the page in the url is in the defined array
    if(in_array($_GET['p'])) {
        
        $page = $_GET['p'].'.php';

    } else {

        $page = 'main.php';

    }

}
?>

<table border="1" width="100%" height="0">
   <tr>
      <td height="120" width="100%" colspan="2"><?php include('heading.php');?> </td>
   </tr>
   <tr>
      <td height="0" width="20%"><?php include('leftmenu.php');?> </td>
      <td height="0" width="80%"><?php include($page);?> </td>
   </tr>
   <tr>
      <td height="50" width="100%" colspan="2"><?php include('bottom.php');?> </td>
   </tr>
</table>

 

In your leftmenu.php you must change your links to:

 

<a href="../index.php?p=main">Home</a>

Ok i have put the following code in

 

index.php

<head>
<title>test</title>
</head>

<body>
<?php
$page = 'main.php';   // Default page
$pages = array('main', 'about','products','gallery','contact');

// Check if p is set
if(isset($_GET['p'])) {

    // Check if the page in the url is in the defined array
    if(in_array($_GET['p'])) {
        
        $page = $_GET['p'].'.php';

    } else {

        $page = 'main.php';

    }

}
?>

<table border="1" width="100%" height="0">
   <tr>
      <td height="120" width="100%" colspan="2"><?php include('heading.php');?> </td>
   </tr>
   <tr>
      <td height="0" width="20%"><?php include('leftmenu.php');?> </td>
      <td height="0" width="80%"><?php include($page);?> </td>
   </tr>
   <tr>
      <td height="50" width="100%" colspan="2"><?php include('bottom.php');?> </td>
   </tr>
</table>

</body>

 

and in leftmenu.php

<a href="../index.php?p=main">Home</a><p>
<a href="../index.php?p=about">About Us</a><p>
<a href="../index.php?p=products">Products</a><p>
<a href="../index.php?p=gallery">Gallery</a><p>
<a href="../index.php?p=contact">Contact Us</a><p>

 

you can see results at

http://handweaving.seitservices.com.au/

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.