[Solved] Content management

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

[Solved] Content management

Post 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.
Last edited by vigge89 on Thu Oct 16, 2003 3:12 pm, edited 2 times in total.
logand
Forum Newbie
Posts: 2
Joined: Mon Oct 13, 2003 3:11 pm

Post 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
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post 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 :)
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post 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.
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post 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
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post 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') ?>
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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
Last edited by McGruff on Wed Aug 10, 2005 3:07 pm, edited 1 time in total.
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

McGruff wrote: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
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 = "
&lt;a href="test.php"&gt;Hello&lt;/a&gt; and welcome to my page etc...&lt;br&gt;
&lt;?php include('news/news.txt') ?&gt;&lt;br&gt;
end of news.....
";
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

What exactly is in news.txt? Is it just text or does it contain PHP?

Mac
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post 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>
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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.
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post 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
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post 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...
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post 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?
Post Reply