Jump to content

Help With IF statement


Loryman

Recommended Posts

I'm quite new with PHP, so I'm unsure what do to. But Im guessing what I need is an IF statement, that will display a message saying "The page does not exist" instead of "failed to open stream", for any page that does not exist on the site.

 

This page

 

Can anyone help?

Code of index.php is shown below.

 

<center>
  Displaying page below line:
  <hr>

<?php 
if(isset($_GET['action'])){
include($_GET['action'].".php");
} else {
include("1.php");
}
?>

  <hr>
  <a href="index.php?action=1">Display 1.php</a> | 
  <a href="index.php?action=nonexistant">Display Non-Existant Page</a>
</center>

 

Thanks.

Link to comment
Share on other sites

hehehe,

 

I came up with a variation of the way your doing it and it became cumbersome. Now I would not develop any other way for 95% of my sites.

 

Ok, most pages have a common area on top, and left with sometimes a right side common as well. But they pretty much all have a single area that the content actually loads in. So here is what I do.

 

Create your main page layout. I will use the standard common head/left nav/footer layout. I use tables, but it can be applied to a css layout in the exact same method. So here is my code for my main page I will call header.php

 

/header.php

<!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=iso-8859-1" />
<title><?=$page_title ?></title>
<style type="text/css">
<!--
body {
margin: 0px;
padding: 0px;
}
-->
</style>
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td colspan="2" align="center"><h1>Banner text </h1></td>
  </tr>
  <tr>
    <td width="18%" valign="top">
  <a href="#">Link1</a><br />
      <a href="#">Link2</a><br />
  <a href="#">Link3</a><br />
      <a href="#">Link4</a><br />
      <a href="#">Link5</a><br />
    </td>
    <td width="82%" align="left" valign="top">
<?php function footer(){  // this is what makes it happen ?>
<!--All My Content will load here..-->


    </td>
  </tr>
  <tr>
    <td colspan="2" align="center"><strong>Footer Text </strong></td>
  </tr>
</table>
</body>
</html>
<?php } //end my footer function ?>

 

I make this my wrapper page. I then define a PHP function called footer() shown in above example. Then on each page I want this to wrap on, I use an include.

 

/page1.php

<?php $page_title='Page 1' ; include($_SERVER['DOCUMENT_ROOT'].'/header.php') ?>

Here is my page content, make some db calls etc... then I will close the page with a call to the footer function. By calling the include using the doc root superglobal, you can move this page into any directory and it will find the header.php page in the root dir.

<?php footer() ?>

 

 

I do this all the time.... You define the function, then wrap the pages in the include and footer. The page title is defined before the include as it is used in the include. Then creating new pages using it is as easy as this..

 

/pages/content/testing/page2.php

<?php $page_title='Page 2' ; include($_SERVER['DOCUMENT_ROOT'].'/header.php') ?>

create content here!

<?php footer() ?>

 

Enjoy!

 

Nate

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.