One-Page Site

Hi all, I’m designing my page using one page, kind of. I have a switch statement on my index.php page which includes the relevant page depending on the URL. Having an issue with the head section. By head section in included in the function ‘globalAssets’ - though my switch statement is below this so therefore my page names, etc are not displaying in the head.

What’s the best way to get around this? Below is all of my code from my index.php page.

<?php
include('includes/config.php')//The function globalAssets is created in this file;
globalAssets() //My head section is included within this function;
?>
<body>
<!-- BOF Wrapper -->
<div id="wrapper">
	<!-- BOF Header -->
		<?php include('includes/nav.php'); ?>
	<!-- EOF Header -->
<?php
switch($_GET['main_page']):
	case "lists":
	include('includes/listpage.php');
	break;
	
	case "product":
	include('includes/prodinfo.php');
	break;
	
	case "cart":
	include('includes/cart.php');
	break;
endswitch;
include('includes/footer.php');
?>
</div>
<!-- EOF Wrapper -->
</body>
</html>

put another switch statement inside the globalassets function and select your header from inside that

@Mandes you are right, but that guy is a newbie so he may not get it if we write in words… I’ll try to put it in form of code after testing it on my local wordpress :slight_smile:

Hey dudes, thanks for your messages. The only thing that would be changing in my head is the page title, meta-description and meta-keyboard…nothing else would change so would this still be the best solution to this problem?

@omthoke

I didnt consider anyone that has mastered the use of the switch statement & PHP functions to be a newbie :wink:

@coxdabb

Yes if your sticking to the switch method to choose content then why not use the same method to select your META tags.

If youve got a problem fitting the switch into your function, post the function code and one of us will code it up :smiley:

Hi Mandes, I agree, I didn’t quite class myself as a newbie really anymore, still learning, but think that’s something many developers find - when do you ever stop!? :lol:

Sure thing, my function is below:

function globalAssets(){
include('includes/head.php');
include('includes/connection.php'); 
}

My head file is as below, actually not liking it being included, could get complicated, may just incorporate this directly into the function. Anyhow, here is the current head.php contents:

<?php
session_start();
if(!$_SESSION['SESS_ID']){
$_SESSION['SESS_ID'] = rand();
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
	<head>
	<title><?php echo ($title) ? $title." at Example" : STORE_META_NAME ?></title>
	<link type="text/css" rel="stylesheet" href="/css/default.css" />
	<link rel="stylesheet" type="text/css" href="/magiczoom.css" />
	<script type="text/javascript" src="/test.js"></script>
	<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
	</head>


I think your overdoing the layers of functions and includes :wink:

Why not consider putting your connection.php include at the top of your main page folowed by your session code.

Follow this with the code from header.php (you can do away with the function all together, its not doing anything)

Is there any good reason why youve seperated the header ?

Definitely, I’ve slightly over-complicated things. I’ll indeed make those changes to try and simplify things slightly. So once this has been re-jigged, how would I manage to change the page title if the included files are still below the head section?

Something like this

index.php


<?php
include('includes/connection.php'); 
session_start();
if(!$_SESSION['SESS_ID']){
 $_SESSION['SESS_ID'] = rand();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html>
    <head>
    <title><?php echo ($title) ? $title." at Example" : STORE_META_NAME ?></title>
    <link type="text/css" rel="stylesheet" href="/css/default.css" />
    <link rel="stylesheet" type="text/css" href="/magiczoom.css" />
    <script type="text/javascript" src="/test.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
<?php
switch($_GET['main_page']):
         case "lists":?>
          <meta name="keywords" content="you keywords" />
          <meta name="description" content="Your description" />
<?php    break;
 
         case "product":?>
          <meta name="keywords" content="you keywords" />
          <meta name="description" content="Your description" />
<?php    break;
 
         case "cart":?>
          <meta name="keywords" content="you keywords" />
          <meta name="description" content="Your description" />
<?php    break;
endswitch;
?>
</head> 
<body>
<!-- BOF Wrapper -->
<div id="wrapper">
    <!-- BOF Header -->
        <?php include('includes/nav.php'); ?>
    <!-- EOF Header -->
<?php
switch($_GET['main_page']):
    case "lists":
      include('includes/listpage.php');
    break;
 
    case "product":
      include('includes/prodinfo.php');
    break;
 
    case "cart":
      include('includes/cart.php');
    break;
endswitch;
include('includes/footer.php');
?>
</div>
<!-- EOF Wrapper -->
</body>
</html> 

However if I was doing this myself Id go one stage further and include the header in the individual page files, that way all the info for the page would be in one php file. and you only have one switch statement

your index.php


<?php
include('includes/connection.php'); // get connection
session_start();  // sort sessions
if(!$_SESSION['SESS_ID']){
 $_SESSION['SESS_ID'] = rand();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<?php  // select page contents
switch($_GET['main_page']):
    case "lists":
      include('includes/listpage.php');
    break;
 
    case "product":
      include('includes/prodinfo.php');
    break;
 
    case "cart":
      include('includes/cart.php');
    break;
endswitch;
?>

Then your individual php page files starting with the html tag


<html>
    <head>
    <title><?php echo ($title) ? $title." at Example" : STORE_META_NAME ?></title>
    <link type="text/css" rel="stylesheet" href="/css/default.css" />
    <link rel="stylesheet" type="text/css" href="/magiczoom.css" />
    <script type="text/javascript" src="/test.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
    <meta name="keywords" content="you keywords" />
    <meta name="description" content="Your description" />
</head> 
<body>
 <div id="wrapper">
        <?php include('includes/nav.php');  // onclude common navigation file ?>
 
<!-- YOUR PAGE CONTENT HERE -->
 
<?php include('includes/footer.php'); // common page footer ?>
</div> <!-- End Div Wrapper -->
</body>
</html> 

Hi Mandes, thank you for your indepth reply, makes a lot of sense. I think you are right, I’ll restructure my code slightly, better to get it right now before the site expands any further. Thanks again, have a good afternoon :slight_smile:

Good morning :wink:

feel free to use the two last working examples if you like the method.

Good luck