Hey all! Thanks in advance for any replies.
I am somewhat new to serverside programming. I started with ASP.net, and recently decided that i would try out PHP to see which i liked better. So far, PHP has been better for me.
In asp the solution i found for templating a website so all pages look similar and to centralize the coding of navigation and such was/is called "master pages".
I did some google search "research" and found most people saying that php's solution to this is to create a template page w/ some php varriables in it, then set the values of those varriables in a new page and include() the template page...
This works great for me!... BUT i have run into an issue now.
I would like to run a php script on a single page that uses this template... If i wanted to run the script on all pages, i could easily just throw it into the template, but that's not the desired goal.
My issue comes in the fact that i cannot set the varriables values to have the php script in it and have the server actually process the script... Time for examples:
template.php :
<html>
<?php echo $content ?>
</html>
workingpage.php :
<?php $content="My content for page goes here."
include('template.php')?>
problempage.php :
<?php $content="my content goes here.. and my script i want to run <?php include('script.php') ?> "
include('template.php')?>
So i can kind of understand WHY it doesn't work, i mean, i would think that the php "engine" if you will is not really parsing the varriable for php code.. instead it's just handling it as a string... but what i don't understand is how to put a script like that on a single page with out doing something crazy like putting it in the template w/ and if statement to go w/ the title of the page. Also, i think that i could easily include the script where i'm including the template, but what if i need to put something in a <div> ? i guess the question would be how to control the location of the output of the script.. i need to put it in my "content' <div>
Hopefully i made myself clear enough to achieve an answer.. If not please feel free to question something and i'll answer best i can..
Again, thanks in advance for any replies..
Tom
PHP include (replacement for masterpage)
Moderator: General Moderators
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: PHP include (replacement for masterpage)
If I understand you correctly, there are several ways to do this and maybe a change in the way you do things, but for the immediate question you can buffer the output and save it to a var:
Then use $script_output where ever you need it. You can even write a function to do this so that you just call $script_output = getOutput($filename):
Code: Select all
ob_start();
include('script.php');
$script_output = ob_get_clean();Code: Select all
$script_output = getOutput('script.php');
$content = "my content goes here.. and my script i want to run $script_output";
function getOutput($filename) {
ob_start();
include($filename);
return ob_get_clean();
}mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Re: PHP include (replacement for masterpage)
so throw this in my template.php...AbraCadaver wrote:Code: Select all
function getOutput($filename) { ob_start(); include($filename); return ob_get_clean(); }
then use $var = getOutput('script.php') and call $var in my $conten="... $var"?
Sounds good.. i'll give it a try. Pretty sure you solved my problem, i'll look to see if i can mark an 'answer' to my post..
Thanks!
Edit: actually before i let ya get away easily.. you say "maybe a change in the way you do things" .... is this the change you're talking about? or am i doing something 'wrong'?
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: PHP include (replacement for masterpage)
You're not doing anything wrong, there's just several other design methodologies that you can use. As for where you put the function, I would put it in a file with other functions that I need and include it on every page. Not the only way, but what many people do is have a header.php at the top and footer.php at the bottom that is included in every file and the header.php for example may include a functions.php that has commonly used functions, etc... Once you get familiar with PHP, you might want to look into a framework. I prefer CakePHP, but there are several other good ones that give you a structure and keep you from writing repetitive code.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Re: PHP include (replacement for masterpage)
Another question, I understand that this is personal pref. just like using CakePHP, but what 'editor' do you use? I believe they call them an IDE.AbraCadaver wrote: I prefer CakePHP, but there are several other good ones that give you a structure and keep you from writing repetitive code.
I have been using notepad.. It works but there's no intelesense or any kind of syntax checking.
Again, thank you for all your help.
Tom
PS what you suggestion worked.
-
thinsoldier
- Forum Contributor
- Posts: 367
- Joined: Fri Jul 20, 2007 11:29 am
- Contact:
Re: PHP include (replacement for masterpage)
notepad? seriously?
Just use ANYTHING with color coding. It's a life saver.
Once you graduate to using lots of functions you've written yourself or OOP you should look into something line Eclipse+PDT or Netbeans PHP or Zend Studio or phpedit... basically anything besides notepad.
I was using zendStudio 6.1 but after a while it felt really slow. I upgraded to zendStudio 7 and although it initially felt faster it seemed to lack a feature or some features changed their behavior and it annoyed me and then it started getting slow too.
I've been using Netbeans for the last couple of weeks and I like it. It's not perfect but it works. The best thing about netbeas for me was I was able to figure out how to connect the IDE to my local mysql and to my local XDEBUG without having to go look for instructions.
It literally ... (DON'T LAUGH)... It literally took me YEARS to figure out how to connect Zend Studio (6/6.1/7/7.0.1) to XDEBUG.
Personally I'd say go for the IDE with the most active PHP specific forums. Zend Studio's forums are LIFELESS, DEAD, USELESS.
Netbeans (php) forum isn't much better but they do have a Netbeans PHP blog that is very good. I haven't looked for the Eclipse+PDT forums yet. FYI: Eclipse+PDT is almost the same as ZendStudio 6/7
Just use ANYTHING with color coding. It's a life saver.
Once you graduate to using lots of functions you've written yourself or OOP you should look into something line Eclipse+PDT or Netbeans PHP or Zend Studio or phpedit... basically anything besides notepad.
I was using zendStudio 6.1 but after a while it felt really slow. I upgraded to zendStudio 7 and although it initially felt faster it seemed to lack a feature or some features changed their behavior and it annoyed me and then it started getting slow too.
I've been using Netbeans for the last couple of weeks and I like it. It's not perfect but it works. The best thing about netbeas for me was I was able to figure out how to connect the IDE to my local mysql and to my local XDEBUG without having to go look for instructions.
It literally ... (DON'T LAUGH)... It literally took me YEARS to figure out how to connect Zend Studio (6/6.1/7/7.0.1) to XDEBUG.
Personally I'd say go for the IDE with the most active PHP specific forums. Zend Studio's forums are LIFELESS, DEAD, USELESS.
Netbeans (php) forum isn't much better but they do have a Netbeans PHP blog that is very good. I haven't looked for the Eclipse+PDT forums yet. FYI: Eclipse+PDT is almost the same as ZendStudio 6/7
Warning: I have no idea what I'm talking about.
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: PHP include (replacement for masterpage)
I'm on linux and I use kate for single files and either zend or eclipse for projects. Zend and eclipse are good on windows, or I think notepad++ (free) has color coding, code folding and maybe syntax checking. As long as you have syntax checking and color coding it should be fine.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.