We help IT Professionals succeed at work.

php issue when using the include function

NBSHOMES
NBSHOMES asked
on
I'm currently loading an include file "includes/omniPro/profile-info.php" into a php page "about/index.php". This file contains many strings, variables and alike. I have another file "includes/omniPro/head-elements.php" that I would like to include on this same "about/index.php" page with the site menu and a few other things that directly relates to this "profile-info.php" file.

The problem is that, this only works when I place the code directly on the "about/index.php" page. When I try to do the same by including the file, it doesn't work. I already checked if it was something related to file path and it's not. Some elements of the include file are loaded but not the piece of code I need.

The file structure is as follow:
about/index.php (this is the webpage I'm loading the files into)
includes/omniPro/profile-info.php (This is the file containing many variables, strings and etc)
includes/omniPro/head-elements.php (This is the file containing the peace of code)

The Syntax:
<link rel="stylesheet" href="<?php echo 'http://' . $_SERVER['HTTP_HOST']; ?>/css/colors.php?color=<?php if($color {main} == '') { foreach ($page as $key => $item) if ($page[$key][4] == basename(__DIR__)) echo $page[$key][2];} else echo $color {main}; ?>" type="text/css" />

Open in new window


When included, the rendered HTML page shows:
<link rel="stylesheet" href="http://localhost:8888/css/colors.php?color=" type="text/css" />

Open in new window


This is the supposed rendered HTMLs - showing a color code:
<link rel="stylesheet" href="http://localhost:8888/css/colors.php?color=FCC444" type="text/css" />

Open in new window

This works just fine if I place this code directly on about/index.php.

I have attached the files for your review. So how can I solve this problem? Any help is very much appreciated. Thanks for the help.
profile-info.php
head-elements.php
index.php
Comment
Watch Question

Most Valuable Expert 2011
Top Expert 2016
Commented:
PHP is a server-side scripting language that generates HTML (and by inference CSS and JavaScript) documents that get sent to the client, where the client loads up web pages and does "client-side" things.  As such, it may be useful to use templates to create the documents rather than trying to create the documents with conditional statements.

I think there may be some syntax hiccups in your scripts so I urge you to add error_reporting(E_ALL) to the top of all of your scripts.  If you do this, PHP will tell you when you accidentally rely on an undefined variable.  When I copied the script above and ran it, the server sent this:
<link rel="stylesheet" href="http://iconoun.com/css/colors.php?color=<br />
<b>Notice</b>:  Use of undefined constant main - assumed 'main' in <b>/home/iconoun/public_html/demo/temp_nsbhomes.php</b> on line <b>6</b><br />
<br />
<b>Notice</b>:  Undefined variable: color in <b>/home/iconoun/public_html/demo/temp_nsbhomes.php</b> on line <b>6</b><br />
<br />
<b>Notice</b>:  Undefined variable: page in <b>/home/iconoun/public_html/demo/temp_nsbhomes.php</b> on line <b>6</b><br />
<br />
<b>Warning</b>:  Invalid argument supplied for foreach() in <b>/home/iconoun/public_html/demo/temp_nsbhomes.php</b> on line <b>6</b><br />
" type="text/css" />

Open in new window

The code I used was this:
<?php // demo/temp_nsbhomes.php
error_reporting(E_ALL);

// DROP INTO HTML TO SEE WHAT HAPPENS
?>
<link rel="stylesheet" href="<?php echo 'http://' . $_SERVER['HTTP_HOST']; ?>/css/colors.php?color=<?php if($color {main} == '') { foreach ($page as $key => $item) if ($page[$key][4] == basename(__DIR__)) echo $page[$key][2];} else echo $color {main}; ?>" type="text/css" />
<?php // back to PHP

Open in new window

PHP is very "lenient" about errors -- it allows you to use undefined variables; it allows you to use variables wrong, silently filling in values or converting formats, etc.  As a programmer you must guard against these weaknesses of the language.  There is no magic bullet, but getting the error reporting levels raised up is a good first step.
Most Valuable Expert 2011
Top Expert 2016

Commented:
Since it looks like you're at the threshold (the Lorem Ipsum is the clue) you might want to step back and consider hiring a professional programmer to help with this project.  If you want to try to do this all yourself, maybe this article can help you get started.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html

If you're in a major metropolitan area, you might use Meetup to see if there are PHP programmers in your area.  Some Meetup groups can be very helpful to new programmers -- certainly the DC PHP group has been helpful to me!  When the work has economic value, it's worth considering professional support, even if it costs money.

In the instant case, the templating process would be easy if you use the HEREDOC notation.   Please heed, but do not be put off by, the warning labels in the man page.
https://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

And most importantly, best of luck with your project! ~Ray

Author

Commented:
As you probably noticed, I'm not an expert on this. I'm just trying to get this done. I'm not even sure about these errors reporting. Is there any other way you can help? Correcting the syntax maybe? Thank you.
Scott FellDeveloper & EE Moderator
Fellow 2018
Most Valuable Expert 2013

Commented:
I wonder if you just need to get your include files in the include path http://php.net/manual/en/function.set-include-path.php
Brian TaoOwner
Top Expert 2014

Commented:
What is "$color {main}"?  It's not a valid expression in PHP.
What are you trying to do with "$color {main}"?
Web Developer/Designer
Commented:
If it works on that one page and not others then it's possible that a variable isn't defined on the other pages.
Most Valuable Expert 2011
Top Expert 2016

Commented:
@brandon: Exactly - that's why it's so important to raise the level of PHP error_reporting() to E_ALL.