ShugNorris Posted July 22, 2009 Share Posted July 22, 2009 Hi there, I'm trying to make a simple news poster, I'm really struggling with the array_reverse though. <?php $title = $_POST["title"]; $newsy = $_POST["news"]; $postedbyy = $_POST["postedby"]; $date = date("l jS F Y, g:i A"); $postedby = "<h1>$title</h1><br>$newsy</font>"; $news = "<p><small><font color=\"black\">Posted by <font color=\"red\">$postedbyy</font><font color=\"black\"><br>On $date<p></font>"; $myFile = "news1.html"; $fh = fopen($myFile, 'a+') or die("can't open file"); $stringData = "$postedby\n"; fwrite($fh, $stringData); $stringData = "$news\n"; fwrite($fh, $stringData); fclose($fh); echo "Your news has been posted<br><a href=\"news1.html\">View it here</a>"; php?> This is what I've come up with so far and it works to post basic news, the news.html file will be an include on my page. Obviously i'd like to use the array_reverse so new news is at the top? I have been searching for hours and found a lot, if someone could incorporate that into my script and explain a little why they did it that would be great. Like I said, I'm not being lazy, just can't find what I want to know. I know there's the implode option too, but I'd actually like to know how to write to the beginning of a file without overwriting information already there. Once again, help appreciated. - Shug Link to comment https://forums.phpfreaks.com/topic/166959-reverse-array-confused/ Share on other sites More sharing options...
Adam Posted July 22, 2009 Share Posted July 22, 2009 Why not use a database? Make things a hell of a lot simpler for yourself. Edit: By the way try searching Google for "writing text to the start of a file without overwriting php" - top result! Link to comment https://forums.phpfreaks.com/topic/166959-reverse-array-confused/#findComment-880294 Share on other sites More sharing options...
ShugNorris Posted July 22, 2009 Author Share Posted July 22, 2009 <?php $title = $_POST["title"]; $newsy = $_POST["news"]; $postedbyy = $_POST["postedby"]; $date = date("l jS F Y, g:i A"); $postedby = "<h1>$title</h1><br>$newsy</font>"; $news = "<p><small><font color=\"black\">Posted by <font color=\"red\">$postedbyy</font><font color=\"black\"><br>On $date<p></font>"; $myFile = "news1.html"; $old_content = file_get_contents($myFile); $new_content = "<h1>$title</h1><p>$news<p>Posted by $postedby<br>$date"; $fh = fopen($myFile, 'r+') or die("can't open file"); $stringData = "$new_content\n"; fwrite($fh, $stringData, $old_content); fclose($fh); echo "Your news has been posted<br><a href=\"news1.html\">View it here</a>"; php?> I hoped that this would work, using r+ to overwrite the beginning.. unfortunately not. Looking through this It seems fine but obviously I've screwed up somewhere as it doesn't edit anything ... As for the mySQL, I'm doing this as a learning step for PHP, not ready for mySQL to fry my head yet! I've spent all day trying to sort this out already. Thanks again. - Shug Link to comment https://forums.phpfreaks.com/topic/166959-reverse-array-confused/#findComment-880449 Share on other sites More sharing options...
Adam Posted July 22, 2009 Share Posted July 22, 2009 Trust me MySQL is much simpler than this, but, if you want to take this approach, I'd recommend storing the posts in a different format. Perhaps in JSON, or to make it easier split bits up using delimiters. This way it won't matter in which order they are stored because you could explode them or parse them into an array that you can work with far easier; sorting by date, alphabetically... however you want. Then you just have to format the data and output it. Will make changing the design, style, etc. later on much easier for yourself as well. Link to comment https://forums.phpfreaks.com/topic/166959-reverse-array-confused/#findComment-880459 Share on other sites More sharing options...
ShugNorris Posted July 22, 2009 Author Share Posted July 22, 2009 Ok, I'll look into mySQL, the most experience I've had with mySQL was installing phpBB and osCommerce.. perhaps scared me off a little bit! Anyway, thanks again MrAdam. Link to comment https://forums.phpfreaks.com/topic/166959-reverse-array-confused/#findComment-880520 Share on other sites More sharing options...
ShugNorris Posted July 22, 2009 Author Share Posted July 22, 2009 Question still remains though, what's wrong with the code? Could I have used both array_reverse and getfile? I'm just a bit confused because I can't imagine this being that complicated a task once you understand what's going on! I'm not trying to make a brilliant news poster, just want to understand what I've done wrong before I move on. Thanks. - Shug Link to comment https://forums.phpfreaks.com/topic/166959-reverse-array-confused/#findComment-880547 Share on other sites More sharing options...
ShugNorris Posted July 22, 2009 Author Share Posted July 22, 2009 <?php $title = $_POST["title"]; $news = $_POST["news"]; $postedby = $_POST["postedby"]; $date = date("l jS F Y, g:i A"); //Gathering info $myFile = "news1.html"; $old_content = file_get_contents($myFile); $new_content = "<br><b><h3><u>$title</u></h3></b><p>$news<p>Posted by <font color=\"red\">$postedby</font><br>$date<br>"; //Opening and Writing... $fh = fopen($myFile, 'r+') or die("can't open file"); $stringData = "$new_content\n"; $stringData1 = "$old_content\n"; fwrite($fh, $stringData. $stringData1); fclose($fh); echo "Your news has been posted<br><a href=\"news1.html\">View it here</a>"; php?> You put me in the right direction mate, sorted it out, working now. Thanks. - Shug Link to comment https://forums.phpfreaks.com/topic/166959-reverse-array-confused/#findComment-880655 Share on other sites More sharing options...
husin Posted July 23, 2009 Share Posted July 23, 2009 ya sql is so useful and very easy to learn! if u wanna learn it, check out this link. it's my whole semester on MySQL databasing: http://www.megaupload.com/?d=EB3HRALZ Anyone may download! Link to comment https://forums.phpfreaks.com/topic/166959-reverse-array-confused/#findComment-880894 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.