This is the first time I have looked at a PHP scripting (although I have done some ASP.NET) and I am looking for help with the following.

Can PHP be used to update a HTML page - and if so how or where should I look?

This is for a site with a news section and the user would like a page to be able to update the news secion (add/remove items).

This used to be done in ASP.NET, but they have moved to a hosting company that does not provide ASP.NET.

Many thanks.

Dani AI

Generated

yes — PHP can generate the HTML for your news section so non-tech users can add/remove items without touching markup. ’s approach of echoing variables works, but for ongoing updates it is easier to store the news in one small data file and have the page render from that.

One simple flat-file approach:

  1. Create a text file at data/news.txt where each line is date|title|body (no HTML), for example:
    2008-12-13|Christmas|We would like to wish all our clients...
  2. Rename your news page to news.php and replace the current hard-coded rows with this loop:
<?php
$rows = @file(__DIR__.'/data/news.txt', FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES) ?: [];
$items = array_map(fn($l) => str_getcsv($l, '|'), $rows);
usort($items, fn($a,$b) => strcmp($b[0], $a[0])); // newest first
?>
<div class="news">
<?php foreach ($items as [$date,$title,$body]): ?>
  <div class="item">
    <div><em><?php echo htmlspecialchars($date); ?></em> - <strong><?php echo htmlspecialchars($title); ?></strong></div>
    <p><?php echo nl2br(htmlspecialchars($body)); ?></p>
    <hr />
  </div>
<?php endforeach; ?>
</div>
  1. Add a tiny, password-protected admin form that POSTs to save.php. The handler can append a line to the file:
<?php
// save.php (protect this folder with HTTP auth or at least a strong password)
$file = __DIR__.'/data/news.txt';
$title = str_replace('|','/', trim($_POST['title'] ?? ''));
$body  = str_replace(["\r","\n",'|'], [' ',' ','/'], trim($_POST['body'] ?? ''));
$line  = sprintf("%s|%s|%s\n", date('Y-m-d'), $title, $body);
file_put_contents($file, $line, FILE_APPEND | LOCK_EX);
header('Location: /news.php'); exit;

To remove items later, read all lines, filter out the one you do not want, and rewrite the file. When needs grow, swap the text file for SQLite/MySQL and use prepared statements, but the display logic stays the same.

Recommended Answers

All 9 Replies

What do you mean by update a html page ? Can you be more specific ?

Sorry for not being clear.

They have a news page on the website and they are l0ooking for a page only they can access where they can update the news from.

Does this help?

Is the news coming from the database ? Do you want to update the news in the database using a frontend ? I am sorry, I still can't understand it. Could you show us the code so that we could understand better ?

Please see the code below. The news is not currently coming from a database (I wish it was).

</table></td>
</tr>
<tr>
<td height="51"><img src="images/t-news.gif" width="517" height="51"></td>
</tr>
<tr>
<td height="328" align="center" background="images/bkg-home.gif">
<table width="460" border="0" cellpadding="0" cellspacing="5">
<tr>
<td> <div id="Layer1" style="position:relative; width:480px; height:300px; z-index:1; left: 0; top: 0; overflow: auto;" class="scrolling_content">
<table width="420" border="0" cellspacing="15" cellpadding="0">


<tr>
<td align="left" valign="top" class="txt_j"><em>13/12/2007</em> - <strong><font color="#F26121">Christmas</font></strong></td>
</tr>
<tr>
<td align="left" valign="top" class="txt_j">We would like to wish all our clients and friends a Happy Christmas and a Healthy and prosperous New Year. <br>For those of you yet to contact us for 2008, time is running out.</td>
</tr>
<tr>
<td align="left" valign="top" class="bkg_lines"><img src="images/dotted_line.gif"></td>
</tr>


<tr>
<td align="left" valign="top" class="txt_j"><em>13/12/2007</em> - <strong><font color="#F26121">2008 Coming up FAST............</font></strong></td>
</tr>
<tr>
<td align="left" valign="top" class="txt_j">It is not long before 2008 will be here <br><br>We are now planning for 2009<br><br>Do not forget to contact us on or new telephone number<br><br>The number is: </td>
</tr>
<tr>
<td align="left" valign="top" class="bkg_lines"><img src="images/dotted_line.gif"></td>
</tr>


</table>
</div></td>
</tr>
</table>

okay! now thats a basic html with table and td tags. You forgot to mention "what" you want to update.

Sorry once agin, not doing very well am I. Thank you for your help.

It is the text within the table, e.g. remove the entries about Chirstmas and and something about Easter. Or just to add something about Easter now and remove other items later.

Does this help?

Okay.. The simplest way is, put whatever you want in a variable and print that variable whereever you want. For example,

<?php
$text1="This is some random text ! blah blah blah..<br />I just added a break.";
$text2="One more test.. ";
?>
<html>
<body>
<table>
<tr>
<td>
<?php echo $text1; ?>
</td>
</tr>
<tr>
<td>
<?php echo $text2; ?>
</td>
</tr>
</table>
</body>
</html>

This is just an example. You can do it this way.
Hope that helps.
Cheers,
Naveen

commented: Great Help +1

Thanks Naveen, that is just what I wanted.

You are welcome!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.