Hey all,
Sorry to be the noob with two posts asking a question, but I've been fighting with this for a couple days now, and as far as I can tell, this should be working, but PHP isn't my main language, so I'm sure there's something being missed.
I've made a small program that allows the user to first select a country, then a state/province, and finally add a city to that location. So, when a new city is submitted, I load the .TXT file containing all the other cities belonging to that region into an array, add the new city to the end of that array, sort() it, the write it to the text file by way of while loop.
What's happening though is that when the first city is added, it has one city added to the file, and a carriage return (because of the "\r\n"). When the second city is added, it has the first two cities, followed by two carriage returns (making four lines). When the third city is added, it starts with four carriage returns, three cities, then one carriage return (making eight lines). And it just keep going on like that, doubling the number of lines every time.
[PHP] $country = file ('../../country/country.txt'); //loads all the saved countries into an array
$cou = count ($country);
//First, determine the location of the list of states or provinces for the selected country
$x = $cou - 1;
//we have to knock off the blank space at the end of the variable if it is not the last place in the array
$flat_cnt = trim($country[$_COOKIE['country']]);
$state = file ('../../country/'.$flat_cnt.'/states.txt');
$sta = count ($state);
$flat_sta = trim($state[$_COOKIE['state']]);
$cities = file ('../../country/'.$flat_cnt.'/'.$flat_sta.'/cities.txt');
$cit = count ($cities);//sets a variable to the number of items in the array
$x = 0;
if ( isset ($_COOKIE['city']))
{
print ($cities[$_COOKIE['city']]);
print ('<form method="post" action="post.php"><input type="submit" name="newcity" value="Reselect State" /></form>');
}
else
{
//Print out selection box
print ('<br /><br /><b>Please Enter A New City:</b><br />');
print ('<form method="post" name="submitCity"><input type="text" name="city" size="100"><br /><input type="submit" name="submitCity" value="Add New City" /></form>');
}
if (isset ($_POST['submitCity'])) //handle form.
{
if ($fp = fopen ('../../country/'.$flat_cnt.'/'.$flat_sta.'/cities.txt', 'w+')) //try to open the file. 'w+' means a new file will be created if needed and old files will be overwritten with new contents
{
//Add entered city into cities array
$cities[$cit] = $_POST['city'];
//make the cities array alphabetical
sort($cities);
flock ($fp, LOCK_EX);//lock the file so multiple users can NOT edit file simultaneously by accident
//Write the $cities array to the file
$z = 0;
while ($z <= $cit)
{
fwrite ($fp, "$cities[$z]\r\n");
$z++;
}
flock ($fp, LOCK_UN);//unlocks the file so another user can add cities
fclose ($fp); //close the file
//print a message
print "<p><font color=red><b>The new city, </font><font color=black>".$_POST['city']." </font><font color=red>has been successfully added!</b></font></p>";
}
else //Could not open the file
{
print "<p>The city could not be stored due to a system error.</p>";
}
}[/PHP]
Any help you can offer will be greatly appreciated (o^_^o)
~Xean