Jump to content

reverse array.. confused


ShugNorris

Recommended Posts

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

<?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

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.

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

<?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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.