- This topic is empty.
-
AuthorPosts
-
June 17, 2012 at 9:25 am #38532Al3ksParticipant
Hi
I’m trying to output the whole contents of a php file in a function.
I have this code,function tep_pass_forgot() {
$file = file_get_contents(FILENAME_PASSWORD_FORGOTTEN);
return $file;
}FILENAME_PASSWORD_FORGOTTEN is defined as password_forgotten.php.
But this outputs me this:
add_session('login', SUCCESS_PASSWORD_SENT, 'success'); tep_redirect(tep_href_link(FILENAME_LOGIN, '', 'SSL')); } else { $messageStack->add('password_forgotten', TEXT_NO_EMAIL_ADDRESS_FOUND); } } $breadcrumb->add(NAVBAR_TITLE_1, tep_href_link(FILENAME_LOGIN, '', 'SSL')); $breadcrumb->add(NAVBAR_TITLE_2, tep_href_link(FILENAME_PASSWORD_FORGOTTEN, '', 'SSL')); require(DIR_WS_INCLUDES . 'template_top.php'); ?>
size('password_forgotten') > 0) { echo $messageStack->output('password_forgotten'); } ?>This looks like a section from my file but I want it to output the same way it would if i opened the file from the url.
Any ideas how to make this work to show the output of the php file?
June 17, 2012 at 10:25 am #104492SenffParticipantCan’t you use INCLUDE for this?
include password_forgotten.php;
June 17, 2012 at 12:06 pm #104496AnonymousInactiveInclude wouldn’t work if he wanted to get the output as opposed to the actual php file’s content. The question isn’t clear as to which it is.
If you want the actual file contents, use include. If you want the output as would be seen in a browser querying the script, well, you just need a more intelligent way of producing output. Try to break down how the script works out what to produce at the end, and make sure that the output is ultimately produced by a single function.
Using an OO approach is a good way to be doing this from the start, but if you have a more “procedural” approach, it’s not hard to move your logic into functions instead of them being top level.
June 17, 2012 at 12:45 pm #104498Al3ksParticipant@BenWalker
Yes I want to output as would be seen in a browser so the include won’t work.
I’m not that good in writing php, can you give me a more specific idea with a code example. Would I use a completely different code or do I need to add on to what i have?June 17, 2012 at 4:00 pm #104503SenffParticipantNot sure what’s the difference (maybe I just misunderstand what you want). Do you want to show the contents of the PHP file before it’s parsed (the unprocessed source code) or the output that the file gives after it’s parsed (the result)?
Calling the file straight to the URL would result in parsed, processed code, wouldn’t it? I thought that’s what you wanted, which is why I suggested include (wouldn’t that show the output of the processed PHP file?)
June 17, 2012 at 4:06 pm #104505Al3ksParticipant@Sneff
I tried the include statement you suggested but it outputs nothing (blank).
I want to show the result of the contents in the php file, just like you said.
June 17, 2012 at 5:25 pm #104506AnonymousInactiveInclude pulls the php code, not the output, into the script.
This is a really basic example of what I’m getting at. Let’s say you have a script called show_table.php. show_table.php is a page that contains a table with data in it. The table is an object that is manipulated by other scripts, as it is connected to a database. The table might, therefore, have its own class, contained in MyTable.php:
class MyTable
{
var $output; // output string
function ConstructTable()
{
$this->output = "
";Header One Header Two Column One Column Two
}
function Output()
{
return $this->output;
}
}
?>show_table.php, when displaying the table, will also take advantage of a header and footer page. If the header and footer are always the same, then their codes can be placed in separate files (header.php and footer.php) and simply included, since no processing needs to take place. show_table.php therefore probably looks like this:
include( "header.php" );
include( "MyTable.php" );
$myTable = new MyTable();
$myTable->ConstructTable();
echo $myTable->Output();
include( "footer.php" );
?>If you then needed to include the same table in any other page, you can now include it in exactly the same way. In the same vein, the MyTable class could have other functions than ConstructTable and Output (maybe editing, deleting, etc) that show_table.php doesn’t use, but a new php file called screw_with_data.php does. Do you see?
Compartmentalise your code so it is reusable. The problem you are having stems from your code’s structure.
June 17, 2012 at 6:17 pm #104509AnonymousInactiveSorry — the above post used “$this->output += …” when it should have just been “$this->output = …”. I have edited the post, but apologies to anyone who may have actually tried it out. I wrote it on the page, so hadn’t actually tested it.
@Al3ks
If you want to see a complete example of what I’m trying to describe, I’ve written the code up into a working module with some annotations. -
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.