Jump to content

fopen appending issue


michael.davis

Recommended Posts

Hi!  Thank you in advance for the time and assistance.

 

I am writing a form and some php code that writes to a text file.  I have that accomplished.  :)  My issue is that my form that writes to the text file appends to the bottom of the text file and I need it reversed.  The latest form entry needs to be on top... and then the older posts to follow. I have tried the w+ r+ and c+ but that is not doing it.  Is there something that I am missing?

 

I appreciate the assistance!

Mike

Link to comment
https://forums.phpfreaks.com/topic/233507-fopen-appending-issue/
Share on other sites

<?php

session_start();

$guide = $_POST['guide'];

$lake = $_POST['lake'];

$date = $_POST['date'];

$time = $_POST['time'];

$watertemp = $_POST['watertemp'];

$waterconditions = $_POST['waterconditions'];

$airtemp = $_POST['airtemp'];

$weathercond = $_POST['weathercond'];

$lures = $_POST['lures'];

$depth = $_POST['depth'];

$notes = $_POST['notes'];

$text = " \n";

 

if ($guide == "" || $lake == "" || $date == "" || $time == "" || $watertemp == "" || $waterconditions == "" || $airtemp == "" || $weathercond == "" || $lures == "" || $depth == "" || $notes == "") {

print "<p><font color=\"red\"><b>ERROR! You must enter all of the required fields!

<a href = \"javascript:history.back()\">\n\n Back To Fishing Report Form</a> </b></font>\n";

exit;

}

 

 

 

//$date = date('l F d Y h:i:s A');

//echo date ('l F d Y h:i:s A');

 

$file = "2011_reports.txt";

$open = fopen($file, "a+");

$text = " \n";

$dash = "-";

$pipe = "|";

$space = " ";

$timer = ":";

$comma = ", ";

$deg = "°";

 

if ($guide == "Mike") {

$guide = "images/staff/mike_tn.jpg";

}

if ($guide == "Zane") {

$guide = "images/staff/zane_tn.jpg";

}

if ($guide == "Zach") {

$guide = "images/staff/zane_tn.jpg";

}

 

if ($lake == "ohl") {

$lake = '<iframe width="200" height="200" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q=old+hickory+lake&sll=37.0625,-95.677068&sspn=38.775203,79.013672&ie=UTF8&hq=&hnear=Old+Hickory+Lake&t=h&ll=36.281645,-86.562824&spn=0.020757,0.025578&z=13&iwloc=A&output=embed"></iframe><br /><small><a href="http://maps.google.com/maps?f=q&source=embed&hl=en&geocode=&q=old+hickory+lake&sll=37.0625,-95.677068&sspn=38.775203,79.013672&ie=UTF8&hq=&hnear=Old+Hickory+Lake&t=h&ll=36.281645,-86.562824&spn=0.020757,0.025578&z=13&iwloc=A" style="color:#0000FF;text-align:left">View Larger Map</a></small>';

}

 

if ($lake == "pp") {

$lake = '<iframe width="200" height="200" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q=percy+priest+lake&aq=&sll=36.281623,-86.562785&sspn=0.077216,0.154324&ie=UTF8&hq=&hnear=Percy+Priest+Lake&t=h&ll=36.156935,-86.617498&spn=0.01386,0.017252&z=14&iwloc=A&output=embed"></iframe><br /><small><a href="http://maps.google.com/maps?f=q&source=embed&hl=en&geocode=&q=percy+priest+lake&aq=&sll=36.281623,-86.562785&sspn=0.077216,0.154324&ie=UTF8&hq=&hnear=Percy+Priest+Lake&t=h&ll=36.156935,-86.617498&spn=0.01386,0.017252&z=14&iwloc=A" style="color:#0000FF;text-align:left">View Larger Map</a></small>';

}

if ($lake == "lb") {

$lake = '<iframe width="200" height="200" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q=lake+barkley&aq=&sll=36.150524,-86.61252&sspn=0.019336,0.038581&ie=UTF8&hq=&hnear=Lake+Barkley&t=h&ll=36.999262,-88.192749&spn=0.109677,0.136642&z=11&iwloc=A&output=embed"></iframe><br /><small><a href="http://maps.google.com/maps?f=q&source=embed&hl=en&geocode=&q=lake+barkley&aq=&sll=36.150524,-86.61252&sspn=0.019336,0.038581&ie=UTF8&hq=&hnear=Lake+Barkley&t=h&ll=36.999262,-88.192749&spn=0.109677,0.136642&z=11&iwloc=A" style="color:#0000FF;text-align:left">View Larger Map</a></small>';

}

if ($lake == "kl") {

$lake = '<iframe width="200" height="200" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q=kentucky+lake&aq=&sll=36.999487,-88.192541&sspn=0.305999,0.617294&ie=UTF8&hq=&hnear=Kentucky+Lake&t=h&ll=36.886211,-88.179016&spn=0.439359,0.54657&z=9&iwloc=A&output=embed"></iframe><br /><small><a href="http://maps.google.com/maps?f=q&source=embed&hl=en&geocode=&q=kentucky+lake&aq=&sll=36.999487,-88.192541&sspn=0.305999,0.617294&ie=UTF8&hq=&hnear=Kentucky+Lake&t=h&ll=36.886211,-88.179016&spn=0.439359,0.54657&z=9&iwloc=A" style="color:#0000FF;text-align:left">View Larger Map</a></small>';

}

//$event = stripslashes($event);

 

$table = "<table width=100% border=1>";

$t1 = "<tr>";

$t2 = "<td rowspan=4>";

$t3 = "</td>";

$t4 = "<td>Date & Time:";

$t5 = "</td>";

$t6 = "</tr>";

$t7 = "<tr>";

$t8 = "<td>Water Temperature:";

$t9 = "</td>";

$t10 = "</tr>";

$t11 = "<tr>";

$t12 = "<td>Water Conditions:";

$t13 = "</td>";

$t14 = "</tr>";

$t15 = "<tr>";

$t16 = "<td>Weather Conditions:";

$t17 = "</td>";

$t18 = "</tr>";

$t19 = "<tr>";

$t20 = "<td rowspan=3>Lake";

$t21 = "</td>";

$t22 = "<td>Air Temperature:";

$t23 = "</td>";

$t24 = "</tr>";

$t25 = "<tr>";

$t26 = "<td>Lures Used:";

$t27 = "</td>";

$t28 = "</tr>";

$t29 = "<tr>";

$t30 = "<td>Depth:";

$t31 = "</td>";

$t32 = "</tr>";

$t33 = "<tr>";

$t34 = "<td colspan=2>Notes:";

$t35 = "</td>";

$t36 = "</tr>";

$endtable = "</table>";

$hr = "<hr>";

fwrite($open, $table);

fwrite($open, $t1);

fwrite($open, $t2);

fwrite($open, $guide);

fwrite($open, $t3);

fwrite($open, $t4);

fwrite($open, $date);

fwrite($open, $dash);

fwrite($open, $time);

fwrite($open, $t5);

fwrite($open, $t6);

fwrite($open, $t7);

fwrite($open, $t8);

fwrite($open, $watertemp);

fwrite($open, $deg);

fwrite($open, $t9);

fwrite($open, $t10);

fwrite($open, $t11);

fwrite($open, $t12);

fwrite($open, $waterconditions);

fwrite($open, $t13);

fwrite($open, $t14);

fwrite($open, $t15);

fwrite($open, $t16);

fwrite($open, $weathercond);

fwrite($open, $t17);

fwrite($open, $t18);

fwrite($open, $t19);

fwrite($open, $t20);

fwrite($open, $lake);

fwrite($open, $t21);

fwrite($open, $t22);

fwrite($open, $airtemp);

fwrite($open, $deg);

fwrite($open, $t23);

fwrite($open, $t24);

fwrite($open, $t25);

fwrite($open, $t26);

fwrite($open, $lures);

fwrite($open, $t27);

fwrite($open, $t28);

fwrite($open, $t29);

fwrite($open, $t30);

fwrite($open, $depth);

fwrite($open, $t31);

fwrite($open, $t32);

fwrite($open, $t33);

fwrite($open, $t34);

fwrite($open, $notes);

fwrite($open, $t35);

fwrite($open, $t36);

fwrite($open, $endtable);

fwrite($open, $hr);

fwrite($open, $text);

fclose($open);

?>

It would be easier if you used a db to store the info; however, psuedo code

1. create variable 1 with newest data

2. read file into  variable 2

3. concantenate variable 1 and variable 2 into variable 3

4 . write variable 3 to the file

 

First off, that is horrendous.  Secondly, append means at the end.  Probably the only way is to read the file and pre-pend your data.  To clean up that mess try using just one variable to hold all of your data ($content).  I have shown this partially in the code, though there is a lot of other cleanup to be done:

 

$file = "2011_reports.txt";

if ($guide == "Mike") {
   $content = "images/staff/mike_tn.jpg";
}
if ($guide == "Zane") {
   $content = "images/staff/zane_tn.jpg";
}
if ($guide == "Zach") {
   $content = "images/staff/zane_tn.jpg";
}

if ($lake == "ohl") {
   $content .= '<iframe width="200" height="200" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q=old+hickory+lake&sll=37.0625,-95.677068&sspn=38.775203,79.013672&ie=UTF8&hq=&hnear=Old+Hickory+Lake&t=h&ll=36.281645,-86.562824&spn=0.020757,0.025578&z=13&iwloc=A&output=embed"></iframe><br /><small><a href="http://maps.google.com/maps?f=q&source=embed&hl=en&geocode=&q=old+hickory+lake&sll=37.0625,-95.677068&sspn=38.775203,79.013672&ie=UTF8&hq=&hnear=Old+Hickory+Lake&t=h&ll=36.281645,-86.562824&spn=0.020757,0.025578&z=13&iwloc=A" style="color:#0000FF;text-align:left">View Larger Map</a></small>';
}

if ($lake == "pp") {
   $content .= '<iframe width="200" height="200" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q=percy+priest+lake&aq=&sll=36.281623,-86.562785&sspn=0.077216,0.154324&ie=UTF8&hq=&hnear=Percy+Priest+Lake&t=h&ll=36.156935,-86.617498&spn=0.01386,0.017252&z=14&iwloc=A&output=embed"></iframe><br /><small><a href="http://maps.google.com/maps?f=q&source=embed&hl=en&geocode=&q=percy+priest+lake&aq=&sll=36.281623,-86.562785&sspn=0.077216,0.154324&ie=UTF8&hq=&hnear=Percy+Priest+Lake&t=h&ll=36.156935,-86.617498&spn=0.01386,0.017252&z=14&iwloc=A" style="color:#0000FF;text-align:left">View Larger Map</a></small>';
}
if ($lake == "lb") {
   $content .= '<iframe width="200" height="200" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q=lake+barkley&aq=&sll=36.150524,-86.61252&sspn=0.019336,0.038581&ie=UTF8&hq=&hnear=Lake+Barkley&t=h&ll=36.999262,-88.192749&spn=0.109677,0.136642&z=11&iwloc=A&output=embed"></iframe><br /><small><a href="http://maps.google.com/maps?f=q&source=embed&hl=en&geocode=&q=lake+barkley&aq=&sll=36.150524,-86.61252&sspn=0.019336,0.038581&ie=UTF8&hq=&hnear=Lake+Barkley&t=h&ll=36.999262,-88.192749&spn=0.109677,0.136642&z=11&iwloc=A" style="color:#0000FF;text-align:left">View Larger Map</a></small>';
}
if ($lake == "kl") {
   $content .= '<iframe width="200" height="200" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q=kentucky+lake&aq=&sll=36.999487,-88.192541&sspn=0.305999,0.617294&ie=UTF8&hq=&hnear=Kentucky+Lake&t=h&ll=36.886211,-88.179016&spn=0.439359,0.54657&z=9&iwloc=A&output=embed"></iframe><br /><small><a href="http://maps.google.com/maps?f=q&source=embed&hl=en&geocode=&q=kentucky+lake&aq=&sll=36.999487,-88.192541&sspn=0.305999,0.617294&ie=UTF8&hq=&hnear=Kentucky+Lake&t=h&ll=36.886211,-88.179016&spn=0.439359,0.54657&z=9&iwloc=A" style="color:#0000FF;text-align:left">View Larger Map</a></small>';
}

$content .= "<table width=100% border=1>";
$content .= "<tr>";
//etc
file_put_contents($file, $content . file_get_contents($file));

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.