Nodral Posted October 11, 2011 Share Posted October 11, 2011 Hi All I have a script which creates a csv file with random entries. It should pick a country from an array (this is defined in countries.php), pick 4 letters, create a random price and a random 8 digit number then write each one to a line in the file. This works apart from one exception. When the country is chosing $country[0] it appends it to the previous line of the file. any ideas? $handle = fopen($file, 'a') or die("can't open file"); for ($i=1; $i<=500; $i++){ //get country include ('countries.php'); $countries=$country[array_rand($country)]; // create ACCRISS code $first=array ("C", "E", "F", "G", "I", "L", "M", "O", "P", "S", "T", "U", "V"); $second = array ("C", "D", "E", "F", "G", "M", "P", "V", "W", "X"); $third = array ("A", "M"); $fourth = array("N", "R"); $accriss= $first[array_rand($first)].$second[array_rand($second)].$third[array_rand($third)].$fourth[array_rand($fourth)]; //create price $pounds= rand(1,99); $pence=rand(0,99); $price="$pounds.$pence"; $price="£".number_format($price,2); //create account numbers $number = rand(10000000,99999999); // add new row to output fwrite($handle,"$countries,$accriss,$price,$number"); $place[]=$countries; $car[]=$accriss; $cost[]=$price; $account[]=$number; } //echo $output; fclose($handle); <?php //countries.php $country= array("Albania"," Antigua"," Argentina"," Aruba"," Australia"," Austria"," Bahamas"," Bahrain"," Belgium"," Benin"," Bosnia"," Botswana"," Bulgaria"," Burkina Faso"," Cameroon"," Chile"," Colombia"," Croatia"," This file goes on for about 150 countries but only Albania gets added to the current line instead of starting a new line. Quote Link to comment https://forums.phpfreaks.com/topic/248891-odd-array-behaviour/ Share on other sites More sharing options...
WebStyles Posted October 11, 2011 Share Posted October 11, 2011 try forcing a new line character... instead of this: fwrite($handle,"$countries,$accriss,$price,$number"); try this: fwrite($handle,"\n$countries,$accriss,$price,$number"); Quote Link to comment https://forums.phpfreaks.com/topic/248891-odd-array-behaviour/#findComment-1278151 Share on other sites More sharing options...
Nodral Posted October 11, 2011 Author Share Posted October 11, 2011 Tried this, but I get a blank line before every line in the output file. Gonna have to just force a newline prior to countries[0] Quote Link to comment https://forums.phpfreaks.com/topic/248891-odd-array-behaviour/#findComment-1278173 Share on other sites More sharing options...
WebStyles Posted October 11, 2011 Share Posted October 11, 2011 interesting, I used the same code here, and the only blank line I get is the first line of the file. (but I'm on a Mac, so newlines are \r and not \n here) Quote Link to comment https://forums.phpfreaks.com/topic/248891-odd-array-behaviour/#findComment-1278178 Share on other sites More sharing options...
Nodral Posted October 11, 2011 Author Share Posted October 11, 2011 That's sorted that out, however my $price is being output to the csv file as £30.12 for example. How do I remove the 'Â' sign? I know it's something to do with the encoding but I'm not sure how to change this. Quote Link to comment https://forums.phpfreaks.com/topic/248891-odd-array-behaviour/#findComment-1278184 Share on other sites More sharing options...
WebStyles Posted October 11, 2011 Share Posted October 11, 2011 hmmm... check out mb_internal_encoding or similar. You'll probably also need to make sure you're .txt file was created with the same encoding. UTF-8 is a good way to go for such a thing. Quote Link to comment https://forums.phpfreaks.com/topic/248891-odd-array-behaviour/#findComment-1278192 Share on other sites More sharing options...
Nodral Posted October 11, 2011 Author Share Posted October 11, 2011 I'm using the php file above to create a csv file which then opens in Excel. Which text file are you referring to? Quote Link to comment https://forums.phpfreaks.com/topic/248891-odd-array-behaviour/#findComment-1278195 Share on other sites More sharing options...
WebStyles Posted October 11, 2011 Share Posted October 11, 2011 sorry, I meant .csv (.txt .csv, same thing to me, they are flat text files...) if you open your .cvs in notepad, does it also contain the weird  thing? or only in excel? Try opening it in an editor that will allow you to change the encoding, or create a new one with the same encoding you're using in php. Quote Link to comment https://forums.phpfreaks.com/topic/248891-odd-array-behaviour/#findComment-1278205 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.