Jump to content

PHP Includes and IF Statement Help


lilwingman

Recommended Posts

Ok, so I'm VERY new to PHP, and this will probably seem like a hugely amateur problem.  I read a few tutorials on templating with PHP includes and tried to customise it to suit my needs (you'll see in a moment what I mean).  It hasn't worked out as well as I had hoped.

 

When viewing on my local computer everything worked fine and showed up exactly how I wanted it to.  However, I uploaded it to my host today (I use HostMonster) and it didn't turn out too well at all - Here's a screenshot.

 

Here's my index page's code:

 

<?PHP

$booksNumber=$_GET['b'];
$clothingNumber=$_GET['cl'];
$collectiblesNumber=$_GET['c'];
$gamesNumber=$_GET['g'];
$miscNumber=$_GET['mi'];
$moviesNumber=$_GET['m'];
$postersNumber=$_GET['p'];
$toysNumber=$_GET['t'];
$pageName=$_GET['pa'];
$default = "content/news/news.php"; if(!$id){$id = $default;}
include ("$id");
include('content/'.$pageName.'.php');
include('products/books/'.$booksNumber.'.php');
include('products/clothing/'.$clothingNumber.'.php');
include('products/collectibles/'.$collectiblesNumber.'.php');
include('products/games/'.$gamesNumber.'.php');
include('products/misc/'.$miscNumber.'.php');
include('products/movies/'.$moviesNumber.'.php');
include('products/posters/'.$postersNumber.'.php');
include('products/toys/'.$toysNumber.'.php');
include('layout/before.php');
echo $page_Header;
include('layout/space.php');
echo $page_Text;
include('layout/after.php');

?>

 

I'm a little confused as to why it didn't work on my local computer, but I realise now that I might need some IF statements in there for the host (unless my hosting is configured differently than my home server?), telling it which to include for each $_GET, but I don't really understand IF/ELSE statements all that well.  Anyone have any advice? 

 

 

Link to comment
https://forums.phpfreaks.com/topic/116107-php-includes-and-if-statement-help/
Share on other sites

something like this will help you get going:

<?php
if (!isset($_GET['b'])){
$booksNumber = ""; //set it to a default
}
else{
$booksNumber=$_GET['b'];
}
if (!isset($_GET['c'])){
$clothingNumber = ""; //set it to a default
}
else{
$clothingNumber=$_GET['cl'];
}
if (!isset($_GET['g'])){
$clothingNumber = ""; //set it to a default
}
else{
$gamesNumber=$_GET['g'];
}
if (!isset($_GET['mi'])){
$miscNumber = ""; //set it to a default
}
else{
$miscNumber=$_GET['mi'];
}
if(!isset($moviesNumber)){
$moviesNumber = ""; //set it to a default
}
else{
$moviesNumber=$_GET['m'];
}
if(!isset($_GET['p'])){
$postersNumber = ""; //set it to a default
}
else{
$postersNumber=$_GET['p'];
}
if (!isset($_GET['t'])){
$toysNumber = ""; //set it to a default
}
else{
$toysNumber=$_GET['t'];
}
if(!isset($_GET['pa'])){
$pageName = ""; //set it to a default
}
else{
$pageName=$_GET['pa'];
}
$default = "content/news/news.php"; 
if(!isset($id)){
$id = $default;
}
/* some more if else statements need to be made */
include ("$id");
include('content/'.$pageName.'.php');
include('products/books/'.$booksNumber.'.php');
include('products/clothing/'.$clothingNumber.'.php');
include('products/collectibles/'.$collectiblesNumber.'.php');
include('products/games/'.$gamesNumber.'.php');
include('products/misc/'.$miscNumber.'.php');
include('products/movies/'.$moviesNumber.'.php');
include('products/posters/'.$postersNumber.'.php');
include('products/toys/'.$toysNumber.'.php');
include('layout/before.php');
echo $page_Header;
include('layout/space.php');
echo $page_Text;
include('layout/after.php');

?>

Ok, so I edited my code and updated it with the default pages for each include like you mentioned...

 

<?PHP

$default = "content/news/news.php"; 
if(!isset($id)){
$id = $default;
}
if (!isset($_GET['b'])){
$booksNumber = "index"; //set it to a default
}
else{
$booksNumber=$_GET['b'];
}
if (!isset($_GET['cl'])){
$clothingNumber = "index"; //set it to a default
}
else{
$clothingNumber=$_GET['cl'];
}
if (!isset($_GET['c'])){
$collectiblesNumber = "index"; //set it to a default
}
else{
$collectiblesNumber=$_GET['cl'];
}
if (!isset($_GET['g'])){
$gamesNumber = "index"; //set it to a default
}
else{
$gamesNumber=$_GET['g'];
}
if (!isset($_GET['mi'])){
$miscNumber = "index"; //set it to a default
}
else{
$miscNumber=$_GET['mi'];
}
if(!isset($_GET['m'])){
$moviesNumber = "index"; //set it to a default
}
else{
$moviesNumber=$_GET['m'];
}
if(!isset($_GET['p'])){
$postersNumber = "index"; //set it to a default
}
else{
$postersNumber=$_GET['p'];
}
if (!isset($_GET['t'])){
$toysNumber = "index"; //set it to a default
}
else{
$toysNumber=$_GET['t'];
}
if(!isset($_GET['pa'])){
$pageName = "news/news"; //set it to a default
}
else{
$pageName=$_GET['pa'];
}

/* some more if else statements need to be made */
include ("$id");
include('content/'.$pageName.'.php');
include('products/books/'.$booksNumber.'.php');
include('products/clothing/'.$clothingNumber.'.php');
include('products/collectibles/'.$collectiblesNumber.'.php');
include('products/games/'.$gamesNumber.'.php');
include('products/misc/'.$miscNumber.'.php');
include('products/movies/'.$moviesNumber.'.php');
include('products/posters/'.$postersNumber.'.php');
include('products/toys/'.$toysNumber.'.php');
include('layout/before.php');
echo $page_Header;
include('layout/space.php');
echo $page_Text;
include('layout/after.php');

?>

 

... and the errors dissappeared on the home page.  I rejoiced... prematurely.  Only problem now is that the home page I had designated is gone (maybe I'm calling the default home page incorrewctly?).  The site seems to only be able to load the $toysNumber index page.  No matter what I direct the browser to, the $toysNumber index shows up.  Also, for all the other default pages, if I direct the browser to any of them, like index.php?pa= for example, the toys index page still comes up, and I get an error at the top of the screen like I was getting earlier. 

 

I know I still need a few more if/else statements like you mentioned (not sure if it would fix my problem or not), but I have no idea how to write one that will only include files from a particular directory when that directory's variable is presented (cl, g, etc.) and I can't seem to find any sources online for this problem.

$default = "content/news/news.php"; 
if(!isset($id)) { 
$id = $default;
}

$booksNumber = (isset($_GET['b'])) ? $_GET['b'] : "index";
$clothingNumber = (isset($_GET['cl'])) ? $_GET['cl'] : "index";
$collectiblesNumber = (isset($_GET['c'])) ? $_GET['cl'] : "index"; // <-- Was that meant to be $_GET['c']? You said $_GET['cl']
$gamesNumber = (isset($_GET['g'])) ? $_GET['g'] : "index";
$miscNumber = (isset($_GET['mi'])) ? $_GET['mi'] : "index";
$moviesNumber = (isset($_GET['m'])) ? $_GET['m'] : "index";
$postersNumber = (isset($_GET['p'])) ? $_GET['p'] : "index";
$toysNumber = (isset($_GET['t'])) ? $_GET['t'] : "index";
$pageName = (isset($_GET['pa'])) ? $_GET['pa'] : "news/news";

/* some more if else statements need to be made */
include ("$id");
include('content/'.$pageName.'.php');
include('products/books/'.$booksNumber.'.php');
include('products/clothing/'.$clothingNumber.'.php');
include('products/collectibles/'.$collectiblesNumber.'.php');
include('products/games/'.$gamesNumber.'.php');
include('products/misc/'.$miscNumber.'.php');
include('products/movies/'.$moviesNumber.'.php');
include('products/posters/'.$postersNumber.'.php');
include('products/toys/'.$toysNumber.'.php');
include('layout/before.php');
echo $page_Header;
include('layout/space.php');
echo $page_Text;
include('layout/after.php');

 

Bit tidier imo. And works the same.

 

Also, what's the error you're getting when you include a file and toys comes up?

 

----------------

Now playing: Linkin Park & Jay-Z - Points Of Authority / 99 Problems / One Step Closer

via FoxyTunes

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.