SimplePortal
Customization => Custom Coding => Topic started by: tfs on September 11, 2011, 06:21:56 PM
-
I've been trying to figure out a way to display the content of a TXT file in a block, but have been stymied so far.
I want to allow someone (100% trustworthy) to display information on a forum. They want to do so via a TXT file, which I can then use an FTP script on their machine to put wherever it needs to go on the forum. When they want to change the content they merely publish a new TXT file. So I thought I'd display a PHP or HTML block that pulls the content of that TXT file.
Has anyone done this before? It should be simple, and I've tried a few variations of scripts that are all over the net, to no avail thus far.
-
For SMF 2.0:
<?php
global $smcFunc;
// Your file, should be defined as a txt as you requested
$file = '/path/to/the/file/on/the/system/file.txt';
// Get the information from the file and then clean it
$output = file_get_contents($file);
$output = $smcFunc['htmlspecialchars'](trim($output), ENT_QUOTES);
echo $output;
?>
-
Thanks, that finally got it. I found a tip on another site to maintain the formatting within the TXT file by using "pre" tag, though then it's displayed in a fixed-width font.
<?php
global $smcFunc;
// Your file, should be defined as a txt as you requested
$file = '/homepages/x/helpdesk/license.txt';
// Get the information from the file and then clean it
$output = file_get_contents($file);
$output = $smcFunc['htmlspecialchars'](trim($output), ENT_QUOTES);
echo '<pre>';
echo $output;
echo '</pre>';
?>
-
If you want to keep the format of the text file, you can use nl2br() (http://php.net/manual/en/function.nl2br.php)
<?php
global $smcFunc;
// Your file, should be defined as a txt as you requested
$file = '/homepages/x/helpdesk/license.txt';
// Get the information from the file and then clean it
$output = file_get_contents($file);
$output = $smcFunc['htmlspecialchars'](trim($output), ENT_QUOTES);
$output = nl2br($output);
echo $output;
?>
-
Nice... that works. Now for the hard part. :) What I've been ultimately trying to do is to incorporate this PHP code that displays a TXT file into an existing HTML block, so that it displays inside of a specific grid of a table. This is the raw HTML I'm using.
<span class="upperframe"><span></span></span>
<div class="roundframe"><br>
<div style="text-align: center;"><big>If
your issue is critical, please call the office after filing a ticket
(555)555-1212 and ask for Technical Support. </big><br>
</div>
<br>
<table style="text-align: left; margin-left: auto; margin-right: auto; width: 95%;" cellpadding="6" cellspacing="6">
<tbody>
<tr>
<td style="border: 1px solid rgb(94, 123, 182); background-color: rgb(208, 220, 234); vertical-align: top; width: 24%;"><span style="font-weight: bold;">REMINDER: </span>the
first Thursday of
each month at 10am CT and repeating at 2pm CT, SD2 hosts webinar
discussion/training sessions on hot topics desired by our clients.
Webinar connection instructions are e-mailed to clients two days prior
to each meeting (also posted on sd2.patsong.com). We
hope you can join us! <em></em> </td>
<td style="border: 1px solid rgb(88, 13, 74); background-color: rgb(218, 195, 223); vertical-align: top; width: 24%;"><strong></strong><font><span style="font-weight: bold;"></span></font><span style="font-weight: bold;">Thursday 7/7, is our First Thursday
TechFest (User Forum) for July. </span>The current schedules
calls for the subject to be "Mapping Software" for people with no sense
of direction.<br>
<br>
</td>
</tr>
</tbody>
</table>
<br>
<table style="text-align: left; margin-left: auto; margin-right: auto; width: 95%;" cellpadding="6" cellspacing="6">
<tbody>
<tr>
<td style="border: 1px solid rgb(88, 13, 74); background-color: rgb(218, 195, 223); vertical-align: top; width: 24%;"><span style="font-weight: bold;"></span><font><span style="font-weight: bold;">Check out our main page for important
random filler material!</span></font><br>
<em></em> </td>
<td style="border: 1px solid rgb(94, 123, 182); background-color: rgb(208, 220, 234); vertical-align: top; width: 24%;"><span style="font-weight: bold;">SD2 Patsong, Inc. (SD2)</span><br>
4444 N. Hello Ave., #224<br>
Los Angeles, CA 90503<br>
(555)555-1212 - Voice<br>
(888)555-1212 - Toll Free<br>
(555)555-1211 - Fax<br>
<br>
</td>
</tr>
</tbody>
</table>
<br>
<table style="text-align: left; margin-left: auto; margin-right: auto; width: 95%;" cellpadding="6" cellspacing="6">
<tbody>
<tr>
<td style="border: 1px solid rgb(94, 123, 182); background-color: rgb(208, 220, 234); vertical-align: top; width: 24%;"><span style="font-weight: bold;">REMINDER: </span>the
first Thursday of
each month at 10am CT and repeating at 2pm CT, SD2 hosts webinar
discussion/training sessions on hot topics desired by our clients.
Webinar connection instructions are e-mailed to clients two days prior
to each meeting (also posted on sd2.patsong.com). We
hope you can join us! <em></em> </td>
<td style="border: 1px solid rgb(88, 13, 74); background-color: rgb(218, 195, 223); vertical-align: top; width: 24%;"><strong></strong><font><span style="font-weight: bold;"></span></font><span style="font-weight: bold;"></span>
<?php
global $smcFunc;
// Your file, should be defined as a txt as you requested
$file = '/homepages/x/helpdesk/license.txt';
// Get the information from the file and then clean it
$output = file_get_contents($file);
$output = $smcFunc['htmlspecialchars'](trim($output), ENT_QUOTES);
$output = nl2br($output);
echo $output;
?><br>
<br>
</td>
</tr>
</tbody>
</table>
<br>
<br>
</div>
<span class="lowerframe"><span></span></span>
But I have been unable to use PHP inside of that table to display a TXT file. The PHP is embedded within the bottom right grid of the table, and yet nothing appears. But if I display the PHP via a PHP block, the text appears.
-
Make a new PHP block, and put this as the content:
<?php
echo '
<span class="upperframe"><span></span></span>
<div class="roundframe"><br>
<div style="text-align: center;"><big>If
your issue is critical, please call the office after filing a ticket
(555)555-1212 and ask for Technical Support. </big><br>
</div>
<br>
<table style="text-align: left; margin-left: auto; margin-right: auto; width: 95%;" cellpadding="6" cellspacing="6">
<tbody>
<tr>
<td style="border: 1px solid rgb(94, 123, 182); background-color: rgb(208, 220, 234); vertical-align: top; width: 24%;"><span style="font-weight: bold;">REMINDER: </span>the
first Thursday of
each month at 10am CT and repeating at 2pm CT, SD2 hosts webinar
discussion/training sessions on hot topics desired by our clients.
Webinar connection instructions are e-mailed to clients two days prior
to each meeting (also posted on sd2.patsong.com). We
hope you can join us! <em></em> </td>
<td style="border: 1px solid rgb(88, 13, 74); background-color: rgb(218, 195, 223); vertical-align: top; width: 24%;"><strong></strong><font><span style="font-weight: bold;"></span></font><span style="font-weight: bold;">Thursday 7/7, is our First Thursday
TechFest (User Forum) for July. </span>The current schedules
calls for the subject to be "Mapping Software" for people with no sense
of direction.<br>
<br>
</td>
</tr>
</tbody>
</table>
<br>
<table style="text-align: left; margin-left: auto; margin-right: auto; width: 95%;" cellpadding="6" cellspacing="6">
<tbody>
<tr>
<td style="border: 1px solid rgb(88, 13, 74); background-color: rgb(218, 195, 223); vertical-align: top; width: 24%;"><span style="font-weight: bold;"></span><font><span style="font-weight: bold;">Check out our main page for important
random filler material!</span></font><br>
<em></em> </td>
<td style="border: 1px solid rgb(94, 123, 182); background-color: rgb(208, 220, 234); vertical-align: top; width: 24%;"><span style="font-weight: bold;">SD2 Patsong, Inc. (SD2)</span><br>
4444 N. Hello Ave., #224<br>
Los Angeles, CA 90503<br>
(555)555-1212 - Voice<br>
(888)555-1212 - Toll Free<br>
(555)555-1211 - Fax<br>
<br>
</td>
</tr>
</tbody>
</table>
<br>
<table style="text-align: left; margin-left: auto; margin-right: auto; width: 95%;" cellpadding="6" cellspacing="6">
<tbody>
<tr>
<td style="border: 1px solid rgb(94, 123, 182); background-color: rgb(208, 220, 234); vertical-align: top; width: 24%;"><span style="font-weight: bold;">REMINDER: </span>the
first Thursday of
each month at 10am CT and repeating at 2pm CT, SD2 hosts webinar
discussion/training sessions on hot topics desired by our clients.
Webinar connection instructions are e-mailed to clients two days prior
to each meeting (also posted on sd2.patsong.com). We
hope you can join us! <em></em> </td>
<td style="border: 1px solid rgb(88, 13, 74); background-color: rgb(218, 195, 223); vertical-align: top; width: 24%;"><strong></strong><font><span style="font-weight: bold;"></span></font><span style="font-weight: bold;"></span>';
global $smcFunc;
// Your file, should be defined as a txt as you requested
$file = '/homepages/x/helpdesk/license.txt';
// Get the information from the file and then clean it
$output = file_get_contents($file);
$output = $smcFunc['htmlspecialchars'](trim($output), ENT_QUOTES);
$output = nl2br($output);
echo $output;
echo '<br>
<br>
</td>
</tr>
</tbody>
</table>
<br>
<br>
</div>
<span class="lowerframe"><span></span></span>';
?>
-
I'm truly impressed with your prowess and generosity. Thank you, thank you, and thank you again!!! This is actually something I can really use. :)
I hope I can return the favor some day!
Now... I'm off to study just what's happening in that PHP, and to learn to tweak it here and there.