using include with a word from database - php

Im having a little problem and cant get it to resolved for some reason. I have built a website and got it running but i have now passed some of the files over to a folder to build more themes. What i am doing is trying to change themes back end and for this instance i will use the theme "smooth"
this is what i have, if i put this:
<?php
include "config_inc.php";
?>
include "themes/<?=$aset['Theme']?>/index.php]";
it will display on the page as the correct path "themes/smooth/index.php" so i know it is connecting to the database correctly. But now if i put it in this format:
<?php
include "config_inc.php";
?>
<?php
include "themes/<?=$aset['Theme']?>/index.php]";
?>
It just shows a blank page.
I have tested the link as in "my_domain/themes/smooth/index.php" and every is displaying correctly
Hope someone can help. Thanks

You shouldn't use php tags inside php tags to insert variable in string. Just use {} to do it:
include "themes/{$aset['Theme']}/index.php";
Or
include 'themes/' . $aset['Theme'] . '/index.php';

Related

Execute the Code in a PHP Page And Echo the HTML Output

I absolutely don't post a question here in SO unless I really can't find a way to solve my problem myself. I did a lot of googling and was not able to find a solution for this one problem I am about to describe.
Here is the problem. I am creating a templated php website. With templated I mean something like below:
<?php include("header.php");?>
<div id="content">
<div id="main">
<h2><?php echo($page_title);?></h2>
<?php
echo ($page_content);
?>
</div>
<?php include("sidebar.php");?>
</div>
<?php include("footer.php");?>
As you can see here page template ECHOES the content of the $page_content variable between header and footer sections to build the page.
To keep the code clean and separated (in my own way) I have been placing the html content in .txt files (let's say page1_content.txt) and assigning the txt content to this variable ($page_content) as below:
$page_content = file_get_contents("page1_content.txt");
My problem starts when I place some php code in page1_content.txt, lets' call this file page2_content.php (yes, I change the file from .txt to .php). Then I assign the content of this file to $page_content variable as below as usual:
$page_content = file_get_contents("page2_content.php");
Now, when the page template ECHOES page2_content.php contents the php code in it is also echoed as string and not executed, but I am trying to query a database and do some stuff in this file with some php code. I mean, I want the php code inside page2_content.php to be executed and the cumulative html code to be echoed by the "echo" line inside the template file.
How can I achieve this?
Please ask me any questions if you need more info/clarification.
Thanks
EDİT:
As many people here suggested the solution was including the file. Actually, I tried including the file before but it didn't look like it was working, it broke my template, so I though I was on the wrong track and quit the "include" way of doing this. Since everybody here is advising to use include I tried that again. I replaced the php code in "page2_content.php" with a basic 1-line code just to see if it gets executed before adding generated html code without breaking the template and it worked. Apparently my php code had a problem at first place and hence broke my template execution.
Now I have changed the template structure slightly and pages using the template, and it seems to work nicely. Thanks a lot everybody. I have up-voted every answer suggesting that I use include :)
As #Ali suggested, you could include the files. The other option which I highly suggest you do not use is the eval() function.
I think what you want to do is to include your content PHP file, not echo it (as you are doing with header.php and footer.php).
echo($page_content);
Would become as below:
include("page2_content.php");
You've already done this in your footer and sidebar, just use include()

PHP require_once not working for footer

I want to add a common code for footer on all my web pages. I created a new php file named commonFooter.php and have the below code in it:
<?php
echo '--footer-code-in-html--';
?>
On my home page I added the below code:
<?php require_once('/static/v3/commonFooter.php'); ?> // /static/v3 is the path of the commonFooter.php
But somehow the footer is not showing up on the webpage. Can anyone help me out what am I missing.
The require_once statement is identical to require except PHP will check if the file has already been included, and if so, not include (require) it again.
See the include_once documentation http://php.net/manual/en/function.include-once.php
Try
<?php echo('/static/v3/commonFooter.php'); ?>
If that works and the statement is printed out in the page , Try
<?php require_once('static/v3/commonFooter.php'); ?>
If that fails, then take a share the folder structure of your project.

Why won't my function include work?

I'm trying to include other page from other folder. and i tried this and it won't show the "side.php"
<?php
include 'side.php';
?>
And the "side.php" has:
home
Well you have the answer to your question inside your question.
" Im trying to include other page from other folder"
It means that you have to go inside that folder in order to access the file
<?php
include 'folder_name/side.php'
?>
or just copy the side.php file where your working file is so that all files are in one place
Maybe I'm wrong because I don't know php so it might not work but you could check this out
Include PHP file into HTML file

Relative pathing

Creating a wordpress sight for work to display some info. For convience i have included the following php code
<?php include 'wp-includes/fancyboxstart.php'; ?>
that file loads all the JS, and defines the div id for my fancybox img display. Recently i have changed the permalinks on my WordPress to get rid of the awful page=1 html links. Now i know to fix all my images simply by adding a "/" to the front on my img srcs. My problem is now that i have changed the permalinks it is no longer loading the JS page from above and my Image displayer no longer works =[
I have tried directly linking to it with
`<?php include 'http://premiumshotguns.com/wp-includes/fancyboxstart.php'; ?>`
but no luck. I know it's probably a simple fix but i am not seeing it! any help would be appreciated!
You probably need to include your theme path.
<?php include bloginfo('template_directory').'/wp-includes/fancyboxstart.php'; ?>

PHP Include - full url

I've been using the php include function for my navbar for my website. It works well but....
My HTML says
<?php include 'include/navbar.html'; ?>
Now if I have a HTML page in /techpages/toptech.html so I would like to change my php include to <?php include 'http://example.com/include/navbar.html'; ?>. The problem is I get heaps of errors then.
Can someone help me?
Can someone answer one of these questions?
How can I make the PHP include function work with a full URL?
HTML has ../ to go into the parent folder. Is there a CSS equivilant?
Any help would be much appreciated.
Here are two approaches which I think will work for what I imagine you are trying to achieve:
You could include via include $_SERVER['DOCUMENT_ROOT'].'/include/navbar.html'; - this will always include the same file regardless of in what file in what directory you put the include
PHP has an include_path which specifies where to look for file-includes, you can add the /include directory in this include_path and from there on always include via include 'navbar.html'. But to be able to do this you have to have permission to access/modi the php.ini...
One of the options of doing that is:
include($_SERVER['DOCUMENT_ROOT']."/include/navbar.php");

Categories