OpenCart PHP Custom Function Not Working - php

I'm having a bit of difficulty including a custom php function in an opencart template. Here's what's going on.
In the header.tpl file, I'm inserting this code in the portion:
<?php
$blah = file_get_contents("my_url_goes_here");
?>
<?php
echo $blah;
?>
Then in the footer.tpl file, I'm inserting this code:
<?php if(!isset($blah)) die; ?>
The purpose of this is to detect whether my blah function in the header is still there, and if it's not there to exit the script.
However, it seems the die function is throwing me for a loop, because if I move it up to the header.tpl file, so all the code is together, it works perfectly as designed.
But if I move it to the footer.tpl file, where I want it to go, it kills the script no matter what, and I end up with a blank page - even if the blah variable is intact in the header.tpl file.
This has got me pretty confused, and I can't figure out why it's not reading my variable if it's in a different file, as both of those files should be included on every page.
Thanks in advance for any thoughts!

They are two separate entities, and as such data from one will not be in the other. I'm guessing this is some kind of detection script for reversed code like many do for wordpress templates. OpenCart is MVC based not linear and as such, $blah in the header is disregarded almost as soon as the code in it is completely executed

Related

Google analytics dynamic php, where to include code?

The question has been asked before but none of the previous answers that I have found solves my problem.
This is the structure for my web page.
index.php calls the loader.
loader.tpl generates <head>, generates <body> by include header.tpl, include $body and include footer.
A separate php page holds the functions and a new page is generated by calling a function that sets $page to a new value matching a .tpl file.
I've created the analyticstracking.php file and placed it in a folder.
I've tried placing<?php include_once("foldername/analyticstracking.php") ?> pretty much in every place I can think of. Either it doesn't do anything at all or it causes an error.
No error code is given, it only returns a blank grey page.
I tried copying the script code straight into the body of the $page template and it only generated an error.
I'm out of ideas.
Where should I place
Since I've tried any number of different locations, it would be great if you could give an answer so foolproof that it can't be miss interpreted.
Thanks

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()

Including file in Joomla - what is the difference?

Can anyone tell me what is the difference between including the js script file in the following two ways,
I made this inside system plugin in joomla and included the js file inside "onAfterInitialise" function.
1)
<script type="text/javascript" src="<?php echo JURI::base(); ?>/plugins/system/test/script/script.js"></script>
This works fine and including the js file correctly, But when I logged-in from the backend the font size from userlisting and listing from other extensions gets enlarged.This is not the issue in my js script.
2)
$document->addScript(JURI::root(). "plugins/system/test/script/script.js");
This works fine without any issues.
Can anyone explain what goes behind this.
Using the second method is simply using Joomla coding standards and adds your script in between the <head> tags.
There isn't much difference except for where the script gets imported on the page.
JURI::base() and JURI::root() are both the same. They both define the root folder for your Joomla site. If you are unsure which one to use, I would recommend using method 2, as it's always good to get used to Joomla coding standards.
You can see the source of addScript() :) Basicly, if you use first method, your srcipt will be added in the same place you wrote the code. Second method will add link to a inner table in $document and will be 'rendered' at the
<head></head>
section at the end of page processing.

include header vs include page

I'm currently using include 'header.php' and include 'footer.php' in every page, and as far as I know that's how most people do it. I thought of a way that I personally thought would be better, however. I thought of making index.php, then in the index include the page. This would both eliminate the need for a footer and eliminate the need for include twice in every page. I'm really new to php, however, so I don't know how I would do this. I tried using POST and GET methods, but it doesn't seem to work. What I want to achieve is including pages in the header using a URL such as http://mysite.com/index.php?page=history and then load history.php. If I need to clarify something, just ask. Sorry if I don't accept an answer right away, I'm really drowsy. I'll get to it when I can.
It is not a problem if you include 2 pages in a file, like header.php and footer.php...
Just writing 2 lines of code in each page is not a matter.
You can decide what pages you want to include dynamically in every page by using if statement, instead of passing the page name in the url.
If you'll do it via index.php, you will no doubt do it wrong.
Nothing bad - every newbie does it this way.
Just because you're thinking of includes, while you should be thinking of templates.
You can make it via index.php, no problem. But there should be not a single HTML tag in this index! As well as in the actual page.
No matter if you're doing it in separate pages or via index.php, the scenario should be the same:
Get all data necessary to display particular page.
Call a template.
Thus, your regular page would look like
code
code
code
include 'template.php';
while index.php would look like
get page name
sanitize page name
include page
include 'template.php';
now you can decide what to choose
First off i agree with Meager... Take a look at soem frameworks. Most will use a two step view which essentially does this althoug in a more complex and flexible way.
With that said it would look something like this:
<?php
$page = isset($_GET['page']) ? $_GET['page'] : 'home'; // default to home if no page
if(file_exists($page.'.php')) {
// buffer the output so we can redirect with header() if necessary
ob_start();
include($page.'.php');
$content = ob_get_clean();
}
else
{
// do something for error 404
}
?>
<html>
<head></head>
<body>
<?php echo $content; ?>
</body>
</html>
You could get more complex than that. One thing you want to do uis make sure you dont blindly assume that the page in the $_GET var is safe... make sure the file exists on your server or otherwise sanitize it...

Can PHP be inserted into a javascript call in my header?

I have a wordpress theme that I like to duplicate. To make things easier on myself, I'm trying to use the bloginfo function to call my jquery file..like this:
<script type="text/javascript" src="<?php echo bloginfo("template_url"); ?>/js/jquery-1.4.2.min.js"></script>
I just can't get it to work. When I check my source file it looks exactly like the code above.
Can this even be done, or should I just forget it? Thanks guys!
Are you sure the above code is actually in a PHP-file and gets parsed by the server? I can't think of a different reason why PHP-code should just be printed and not executed.
See the Referencing Files From a Template chapter in http://codex.wordpress.org/Theme_Development
Whats the name of this file? It should end in .php.
What does the source look like when you view it from the browser?
Are there other places in the file where you use and is that data correct?
Make sure there are no blank lines at the end of the source file. PHP doesn't like blanks lines at the end of the code.
Two problems: No need for the echo in the WP template tag; should be
<?php bloginfo("template_url"); ?>
Template Tags/info « WordPress Codex
And, why are you trying to include jQuery from the template directory, when it resides in wp-includes/js? You should be using Function Reference/wp enqueue script, if your theme doesn't already include jQuery.
You should try to edit your theme's header.php file directly because custom fields of your theme may not be executed by PHP interpreter.
As I've never used PHP, this is only a guess...
PHP is a server side language, and you're tyring to use it on the client side?

Categories