Page 1 of 2
[Solved] Content management
Posted: Mon Oct 13, 2003 2:53 pm
by vigge89
Ive got a problem with placing a include function in a variable, this is my code for start.php:
Code: Select all
<?php
//HEADER
$cheader = "title.....";
$cheader2 = "undertitle....";
//CONTENT
$cmain = '
'.include('news/news.txt').'
';
?>
but when i run the site (using site.php, which will print out the variables where they should be, i get this error:
Code: Select all
Warning: main(news/news.txt ): failed to open stream: No such file or directory in C:\xampp\htdocs\test_ny\start.php on line 12
Warning: main(): Failed opening 'nphp/news.txt ' for inclusion (include_path='.;C:\xampp\php\pear'') in C:\xampp\htdocs\test_ny\start.php on line 12
the news.txt does exist, but i think ive forgotten something. Im just a beginner at PHP, so I have got no idea on whats wrong.
Posted: Mon Oct 13, 2003 3:18 pm
by logand
include acts as if you were copy and pasting the news.txt file within the start.php file. So if you write
echo "string".includ("x.txt")."other string";
it means :
echo "string".hi guys this is the news file !."other string";
which is not a valid PHP syntax.
simplest way to include file content :
$fileContent = file_get_contents ( "news/news.txt" );
echo $fileContent;// or anything else with the file content
Posted: Tue Oct 14, 2003 12:55 am
by vigge89
logand wrote:include acts as if you were copy and pasting the news.txt file within the start.php file. So if you write
echo "string".includ("x.txt")."other string";
it means :
echo "string".hi guys this is the news file !."other string";
which is not a valid PHP syntax.
simplest way to include file content :
$fileContent = file_get_contents ( "news/news.txt" );
echo $fileContent;// or anything else with the file content
oh, i didn't think about that

, thx, ill test it now

Posted: Tue Oct 14, 2003 9:43 am
by vigge89
it worked well, but is it possible to have php code in a varibale?
something like this:
Code: Select all
$cmain = '
<?php include(''nphp/news.txt'') ?>
';
but that doesnt work, it wil lprint out the code as html, and not as php.
Posted: Tue Oct 14, 2003 9:57 am
by m3rajk
you can't include to a variable
assume you have this simple php file (note what using php tags instead of code tags does)
Code: Select all
<?php
/* included universal variables file */
$red="#ff0000";
$wysywyg="damn i'm uncreative today";
?>
you include that file at the begining of EVERY page. now you just call red $red to access it.
you can't set the file to a variable, but you can have variables in the file. on the other hand, creating a filehandle, you can read the file into a filehandle, and use the filehandle, but you can't have variables IN the file.
it sounds to me like you don't understand the difference. i
STRONGLY suggest that yu go to php.net and look up include() require() fopen() file_get_contents() and the rest of the file reading functions
Posted: Tue Oct 14, 2003 10:51 am
by vigge89
m3rajk wrote:you can't include to a variable
assume you have this simple php file (note what using php tags instead of code tags does)
Code: Select all
<?php
/* included universal variables file */
$red="#ff0000";
$wysywyg="damn i'm uncreative today";
?>
you include that file at the begining of EVERY page. now you just call red $red to access it.
you can't set the file to a variable, but you can have variables in the file. on the other hand, creating a filehandle, you can read the file into a filehandle, and use the filehandle, but you can't have variables IN the file.
it sounds to me like you don't understand the difference. i
STRONGLY suggest that yu go to php.net and look up include() require() fopen() file_get_contents() and the rest of the file reading functions
im not trying to include a file in a variable. I have a navigation system that uses variables to load content, like $cheader is the title of tha page, $cheader2 is the undertitle and $cmain is the content of the page. What i want is to have the whole content in the $cmain variable, hopefully without any extra code. So when site.php loads the content-variables from start.php, it should output the code in the var, just as it is, using php-functions, html or whatever it is in the variable. But instead of including the news.txt-file when executing site.php, it just prints out the code <?php include('nphp/news.txt') ?>
Posted: Tue Oct 14, 2003 5:30 pm
by McGruff
Set up the include file to return a value, and then:
Code: Select all
<?php
$cmain = include('path/to/file.htm');
?>
http://uk2.php.net/manual/en/function.include.php
Posted: Wed Oct 15, 2003 7:39 am
by vigge89
i dont want the var to only have the contents of the included file, i want to be able to write HTML-commands, PHP-functions, Normal text and so on, something like:
Code: Select all
$cmain = "
<a href="test.php">Hello</a> and welcome to my page etc...<br>
<?php include('news/news.txt') ?><br>
end of news.....
";
Posted: Wed Oct 15, 2003 9:04 am
by twigletmac
What exactly is in news.txt? Is it just text or does it contain PHP?
Mac
Posted: Wed Oct 15, 2003 9:43 am
by vigge89
twigletmac wrote:What exactly is in news.txt? Is it just text or does it contain PHP?
Mac
news.txt is a auto-generated news-file which looks like this (not php code right now, but maybe later):
Code: Select all
<div class="c2box">
<table width="480" cellspacing="0" cellpadding="0">
<tr><td class="tblheader">
<font class="txtheader">new server</font>
</tr></td>
<tr><td class="tblmain" style="padding-left: 4;">
<font class="txtnormal">Testing.........<BR>1.2.3.4.5.6.7.8.9.0<BR>:D</font>
</tr></td>
<tr><td class="tblbtm">
<font class="txtnormal">posted at 21:33 on Wed 06/08 2003. by <A HREF="MAILTO:vigge19@hotmail.com">vigge</A></font>
</tr></td>
</table>
</div>
<br>
Posted: Wed Oct 15, 2003 5:54 pm
by McGruff
Perhaps eval() might be a solution.
In saying that, if I found myself thinking about eval() I'd usually take that as a sign that I need to redesign the script. Personally, I would be thinking about making a News class to encapsulate the news content.
Posted: Thu Oct 16, 2003 8:03 am
by vigge89
McGruff wrote:Perhaps eval() might be a solution.
In saying that, if I found myself thinking about eval() I'd usually take that as a sign that I need to redesign the script. Personally, I would be thinking about making a News class to encapsulate the news content.
Well, what if i made something like if start.php?id=content was included, then the main content is the output, and if start.php?id=vars is included, the vars will be included?
Maybe its stupid, i don't know, but in that case i wouldn't need to do anything complicated
Posted: Thu Oct 16, 2003 10:27 am
by vigge89
I came up with another idea.
the start.php will consist of the variables for the headers and stuff, and also the main content, without any vars or something.
now my question: if i include a file with variables after the echo command which echos the variables in the file, will the output be null, or will it be the variables? I don't think that's gonna work, but I want to be sure first...
Posted: Thu Oct 16, 2003 10:31 am
by volka
if they're not set before echo is called you can't print/access them.
Probably you will get an undefined variable notice
Posted: Thu Oct 16, 2003 10:32 am
by vigge89
volka wrote:if they're not set before echo is called you can't print/access them.
Probably you will get an undefined variable notice
ok, got any ideas on how to solve this in an easy way?