Posting/Updating System - how to delete html section?

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

Post Reply
User avatar
Sindarin
Forum Regular
Posts: 521
Joined: Tue Sep 25, 2007 8:36 am
Location: Greece

Posting/Updating System - how to delete html section?

Post by Sindarin »

Okay, I am trying to create a simple news system for a site. Nothing complex, the layout is going to be all css.
Now what I need for it is a posting form page (for posting new posts) a news page (for viewing the news) and a delete form page (for deleting any useless posts).

Now I have a postnew.php file which is a simple form with 3 fields and the php script writes those values in the "news.html" file.

Code: Select all

<?php

$from= $_POST['from'];
$title= $_POST['title'];
$content= $_POST['editor_1'];

//filename to write

$filename="news.html";
$handle = fopen($filename, 'a');
fwrite($handle, "<br>FROM <b>$from</b> : <i>$title</i> <br><br>");
fwrite($handle, "$content <br><br>");
echo "<center><a href='news.html'>News updated, click here to view the news page</a>"
?>
The news.html page simply displays all the news entries.

Now, how can I do it that the news are listed in a page (let's say deletepost.php) using php_include but have a delete button below them that can delete each individual entry?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Is there any chance you can shift your storage medium to a database? Using text files seriously over complicates this problem.
User avatar
Sindarin
Forum Regular
Posts: 521
Joined: Tue Sep 25, 2007 8:36 am
Location: Greece

Post by Sindarin »

I've thought of it but I don't know MySql and I also don't believe the web hosting company have a database installed on their end.
The goal is to be made very simple with php and flat files.

Actually I have thought if it would be possible to add some kind of identification tag to the start and end of each post and recognize this with php in order to remove individual posts. Just an idea...
User avatar
phpdevuk
Forum Contributor
Posts: 220
Joined: Mon Jul 04, 2005 5:31 am
Location: UK
Contact:

Post by phpdevuk »

I did something similar a while back though it was stored in a database, basically I wrapped each news item in a container div and displayed a delete tick next to each one, when clicked that div and its contents were removed. The final HTML was then posted to a page which merely took hidden form fields containing the HTML and stored them. When ever an update was made or an item deleted the hidden form fields on the first page were either set to blank or contained the html for the new entry. You need a pretty good knowledge of javascript to do it but it works quite well.
User avatar
Sindarin
Forum Regular
Posts: 521
Joined: Tue Sep 25, 2007 8:36 am
Location: Greece

Post by Sindarin »

Isn't there any functions to search for two strings and remove that particular piece between of them?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Sindarin wrote:Isn't there any functions to search for two strings and remove that particular piece between of them?
strpos() for searching for a string within a string.

or usage of preg_match() (to search for the string(s)) and preg_replace() to replace content between the strings.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Sindarin
Forum Regular
Posts: 521
Joined: Tue Sep 25, 2007 8:36 am
Location: Greece

Post by Sindarin »

Gah, I am trying to figure this out, but no luck.
I am trying to create a simple uploader which the user can upload a file and write a title for the link.
The link (with the user defined title) is going to be added to an archives.php file through fwrite.
The target is, in case of an error to be able to delete a link.

I am making simple tests in order to learn about preg_replace but php spits out errors all the time,

The posts/links are going to be written into the archives.php in this way:

Code: Select all

<style>
.hid
{
visibility:hidden;
}
</style>
<div class="hid">post1</div>
<div><a href="interview-part1.zip">get the file here</a></div>
<div class="hid">end of post1</div>
<div class="hid">post2</div>
<div><a href="interview-part2.zip">get the file here</a></div>
<div class="hid">end of post2</div>
so if I want to delete post1, I'll have to execute this, right?:

Code: Select all

<?php
$post1='<div class="hid">post1</div>'
$post1end='<div class="hid">end of post1</div>'
$delete=''
echo preg_replace($post1, $post1end, $delete);
?>
User avatar
Sindarin
Forum Regular
Posts: 521
Joined: Tue Sep 25, 2007 8:36 am
Location: Greece

Post by Sindarin »

Gah, I quit that way.
Is there a way to read and return all text files in a directory?

Like there are 2 text files

post1.txt contains:

Code: Select all

welcome to this site!
post2.txt contains:

Code: Select all

<object width="425" height="350"><param name="movie" value="http://www.youtube.com/v/gR3cRdRhk6Q"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/gR3cRdRhk6Q" type="application/x-shockwave-flash" wmode="transparent" width="425" height="350"></embed></object>
So the php script returns a simple text and a youtube video ordered by their date of creation?
User avatar
phpdevuk
Forum Contributor
Posts: 220
Joined: Mon Jul 04, 2005 5:31 am
Location: UK
Contact:

Post by phpdevuk »

look at the php function readdir and possibley the fread function or similar file functions?
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Post by Stryks »

Sorry to jump back a few posts, but have you *really* considered using a database?

I mean, mySQL isn't your only option (though undoubtedly the best).

phpinfo() can tell you in pretty short order of you have sqlite running on your sever, and once you get the hang of it, it's pretty straight forward to use.

Just a thought.
User avatar
Sindarin
Forum Regular
Posts: 521
Joined: Tue Sep 25, 2007 8:36 am
Location: Greece

Post by Sindarin »

Maybe I'll go with database if it doesn't require too much time to learn.
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Post by Stryks »

Well ... to really get good at it takes a while .. .especially on the design side, but for basic data selects, inserts, updates and deletes you can get away with next to no knowledge at all. Especially when you have access to the wealth of information and expertise this forum provides.

The basics of SQL are surprisingly simple, and as for handling the data you pull back from the database, if you aren't scared off by arrays, you should have no major problems.

If it turns out that mySQL isn't available, create a blank php containing

Code: Select all

<?php
phpinfo();
?>
and give it a run.

SQLite is an option if that page has sections for PDO and pdo_sqlite, and / or SQLite (preferably all). It's a little non-standard, but I've seen it around on a lot of host servers. You might be lucky.

The first step in going further with this would be to find out what you *can* do. Then you can decide what is going to be the best option.

Or you could persist with the file manipulation. Personally though, I think the learning curve is steeper with that route.
Post Reply