rajmohan Posted March 19, 2007 Share Posted March 19, 2007 i am having as follows one two three etc.... i need the output as "one","two",three",etc.... how to do in php please help me Quote Link to comment https://forums.phpfreaks.com/topic/43335-help-please-loop/ Share on other sites More sharing options...
onlyican Posted March 19, 2007 Share Posted March 19, 2007 for($i = 1; $i <= 50; $i++){ echo "\"".$i."\","; } this will show "1", "2", "3", All the way to 50 Quote Link to comment https://forums.phpfreaks.com/topic/43335-help-please-loop/#findComment-210419 Share on other sites More sharing options...
wildteen88 Posted March 19, 2007 Share Posted March 19, 2007 This what you mean?: <?php $numbers = array("one", "two", "three", "..."); echo implode(", ", $numbers); // output: one, two, three, ... ?> Need more information on what you are trying to do. Post your code here too. Quote Link to comment https://forums.phpfreaks.com/topic/43335-help-please-loop/#findComment-210420 Share on other sites More sharing options...
rajmohan Posted March 19, 2007 Author Share Posted March 19, 2007 ya nice i am happy is there any another way. Quote Link to comment https://forums.phpfreaks.com/topic/43335-help-please-loop/#findComment-210431 Share on other sites More sharing options...
onlyican Posted March 19, 2007 Share Posted March 19, 2007 Using the same array <?php $numbers = array("one", "two", "three", "..."); foreach($numbers as $TheNumber){ echo $TheNumber."<br />\n"; } it all depends on why you want to do the loop, to what will be the best method Quote Link to comment https://forums.phpfreaks.com/topic/43335-help-please-loop/#findComment-210432 Share on other sites More sharing options...
rajmohan Posted March 19, 2007 Author Share Posted March 19, 2007 i cannot put like this i am having like this onle one two three four five six like this i am having thousand what can i do i cant put in array i should automatically take it as put it in array after one <br> then <br> like this it looks it is in txt file i have to do it as "one","two", etc....... Quote Link to comment https://forums.phpfreaks.com/topic/43335-help-please-loop/#findComment-210436 Share on other sites More sharing options...
wildteen88 Posted March 19, 2007 Share Posted March 19, 2007 Your not making much sense. But are you tying to add a string which containts "one","two", "three", etc....... in to a text file with no html formatting? Quote Link to comment https://forums.phpfreaks.com/topic/43335-help-please-loop/#findComment-210437 Share on other sites More sharing options...
rajmohan Posted March 19, 2007 Author Share Posted March 19, 2007 yes actually i am having lot of city in a text file i need it like that other wise i want to put comma double quateos Quote Link to comment https://forums.phpfreaks.com/topic/43335-help-please-loop/#findComment-210438 Share on other sites More sharing options...
wildteen88 Posted March 19, 2007 Share Posted March 19, 2007 How do you get the list of cities? Does it come from a textarea which the user submits and each city is on a new line? I need more info in order to provide working code for you. Quote Link to comment https://forums.phpfreaks.com/topic/43335-help-please-loop/#findComment-210442 Share on other sites More sharing options...
onlyican Posted March 19, 2007 Share Posted March 19, 2007 ok I understand I think You dont want one two three ect You want one two three four five .. .. .. If its a text sheet, not HTM, you want \n\r NOT <br /> Quote Link to comment https://forums.phpfreaks.com/topic/43335-help-please-loop/#findComment-210445 Share on other sites More sharing options...
rajmohan Posted March 19, 2007 Author Share Posted March 19, 2007 actually i collect lot of citys from the net and put it in the textfile i forgot to put double quots and comma so now it contain lot of things what can i do Quote Link to comment https://forums.phpfreaks.com/topic/43335-help-please-loop/#findComment-210446 Share on other sites More sharing options...
wildteen88 Posted March 19, 2007 Share Posted March 19, 2007 Something like this?: <?php if(isset($_POST['cities']) && !empty($_POST['cities'])) { $cities_list = $_POST['cities']; // put each city on its own in an array. $cities = explode("\n", $cities_list); // now we format how we want the cities to be added to the txt file $city_list = ''; foreach($cities as $city) { $city_list .= '"' . trim($city) . '", '; } //clean up the cities_listed sting $city_list = substr($city_list, 0, (strlen($city_list)-2)); echo 'Cities to be added:<br />' . $city_list . '<br /><hr />'; // open file read for writing $city_file = fopen('cities.txt', 'a'); // make sure file is writeable if(is_writable('cities.txt')) { // write cities list to file fwrite($city_file, $city_list . "\r\n"); // close the file fclose($city_file); } else { echo 'Unable to write to file!'; } } ?> <form action="" method="post"> Cities:<br /><textarea name="cities"></textarea><br /> <input type="submit" name="submit" value="Add Cities!" /> </form> Add each city on a new line in the textarea. Submit the form it will format the string to be entered into the text file for you, the format entered into the text file would be like this: "city1, "city2", "city3", "exct...." It will add each group of cities you submit on a new line in the text file too. Quote Link to comment https://forums.phpfreaks.com/topic/43335-help-please-loop/#findComment-210458 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.