Jeffro Posted August 4, 2011 Share Posted August 4, 2011 I've ran into this before.. I'm sure it's easy for you guys, but I can't figure it out. Here's my code.. echo '{'; while($row = mysql_fetch_array( $result )) { $mytitle = $row['title']; echo $mytitle; echo '|'; } echo '}'; After the very last echo in the array, it finishes with: example|} How can I remove the | after the very last word only (keeping all other instances) making it finish with: example} Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/243765-how-do-i-remove-this-last-character-in-an-array/ Share on other sites More sharing options...
Alex Posted August 4, 2011 Share Posted August 4, 2011 One way: $titles = array(); while($row = mysql_fetch_array( $result )) { $titles[] = $row['title']; } echo '{' . implode('|', $titles) . '}'; implode Quote Link to comment https://forums.phpfreaks.com/topic/243765-how-do-i-remove-this-last-character-in-an-array/#findComment-1251625 Share on other sites More sharing options...
phpSensei Posted August 4, 2011 Share Posted August 4, 2011 <?PHPSensei To The Rescue Option 1 (possibly): What Alex Mentioned, however Alex's option is for an Array, while op is displaying 1 element from the SQL Option 2: Just count the number of rows in the sql, if its the last loop then prevent it from display the "|", see example below So What Can I Do Sensei? $i = 0; $last_element = mysql_num_rows($sql); while($row = mysql_fetch_array($sql)){ if($i < $last_element){ print "|"; } $i++; } Quote Link to comment https://forums.phpfreaks.com/topic/243765-how-do-i-remove-this-last-character-in-an-array/#findComment-1251626 Share on other sites More sharing options...
Jeffro Posted August 4, 2011 Author Share Posted August 4, 2011 Thanks so much guys. I used option 2 from sensei - though I had to change the last line to: if($i < $last_element -1){ print "|"; } $i++; Looks perfect. Much appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/243765-how-do-i-remove-this-last-character-in-an-array/#findComment-1251630 Share on other sites More sharing options...
phpSensei Posted August 4, 2011 Share Posted August 4, 2011 Yes, Alex's method works if your using it inside an array loaded with elements, so keep that noted somewhere in the future, it will help you out alot. Quote Link to comment https://forums.phpfreaks.com/topic/243765-how-do-i-remove-this-last-character-in-an-array/#findComment-1251631 Share on other sites More sharing options...
Alex Posted August 4, 2011 Share Posted August 4, 2011 Option 1 (possibly): What Alex Mentioned, however Alex's option is for an Array, while op is displaying 1 element from the SQL Um.. what? My solution would work if there is only a single result. Plus, the whole premise of the question is there will be multiple rows returned, otherwise the whole question is pretty moot. You wouldn't need the while loop, and could just do: echo '{' . $the_single_result . '}'; Quote Link to comment https://forums.phpfreaks.com/topic/243765-how-do-i-remove-this-last-character-in-an-array/#findComment-1251632 Share on other sites More sharing options...
phpSensei Posted August 4, 2011 Share Posted August 4, 2011 Alex he is printing the the titles, not storing them inside the while loop, then reprinting them again. Why do that? The OP just wanted to print them inside the while loop, and leave out a "|" at the end of the last row. $the_single_result Thats not a flexible solution, and if he wanted to edit some row data individually he would after to explode the single result again. Quote Link to comment https://forums.phpfreaks.com/topic/243765-how-do-i-remove-this-last-character-in-an-array/#findComment-1251633 Share on other sites More sharing options...
Alex Posted August 4, 2011 Share Posted August 4, 2011 I understand that, but whether you output them one at a time, or all at once afterwards is irrelevant. The output is exactly the same. Quote Link to comment https://forums.phpfreaks.com/topic/243765-how-do-i-remove-this-last-character-in-an-array/#findComment-1251634 Share on other sites More sharing options...
phpSensei Posted August 4, 2011 Share Posted August 4, 2011 See my post above i edited. Quote Link to comment https://forums.phpfreaks.com/topic/243765-how-do-i-remove-this-last-character-in-an-array/#findComment-1251635 Share on other sites More sharing options...
Alex Posted August 4, 2011 Share Posted August 4, 2011 I think it's flexible. I was only arguing that my solution would produce the same output, and would work for even a single result, but I would also argue that my solution is just as flexible, and maybe a bit more clean. If you wanted to edit the data, you would do it beforehand, that's kind of the point. You separate your pre-processing from the outputting, it makes it cleaner and gives your output a single point instead of all over the place. $titles = array(); while($row = mysql_fetch_array( $result )) { $titles[] = $row['title']; // edit title here before inserting it into $titles. } echo '{' . implode('|', $titles) . '}'; The logic behind your code and the output are two completely different things, and it's much cleaner and easier to read if you keep them separated. Instead of having tons of echos everywhere and having to look through 50 lines of code to piece together what happens when the echos get combined, you can simply look at a single line and see it right there. Quote Link to comment https://forums.phpfreaks.com/topic/243765-how-do-i-remove-this-last-character-in-an-array/#findComment-1251640 Share on other sites More sharing options...
phpSensei Posted August 4, 2011 Share Posted August 4, 2011 Alex, please show me an example of your code printing out without using 50 lines of explodes and for loops and counting the elements for that loop. Your doing repetitive coding for such a simple problem. Your code will result in a messier situation. Quote Link to comment https://forums.phpfreaks.com/topic/243765-how-do-i-remove-this-last-character-in-an-array/#findComment-1251643 Share on other sites More sharing options...
Alex Posted August 4, 2011 Share Posted August 4, 2011 I'm not sure what you're asking for. Printing out what exactly? You're making claims without any justification... Quote Link to comment https://forums.phpfreaks.com/topic/243765-how-do-i-remove-this-last-character-in-an-array/#findComment-1251650 Share on other sites More sharing options...
phpSensei Posted August 4, 2011 Share Posted August 4, 2011 check my pm. Quote Link to comment https://forums.phpfreaks.com/topic/243765-how-do-i-remove-this-last-character-in-an-array/#findComment-1251656 Share on other sites More sharing options...
Alex Posted August 4, 2011 Share Posted August 4, 2011 Yeah, I got the pm, but I'd prefer to keep the discussion in the thread. Quote Link to comment https://forums.phpfreaks.com/topic/243765-how-do-i-remove-this-last-character-in-an-array/#findComment-1251657 Share on other sites More sharing options...
phpSensei Posted August 4, 2011 Share Posted August 4, 2011 Sure, well I gave my reply in my pm. so let me know what you think, quote my reply btw, I forgot how to check my "sent" folder, cant find it. Quote Link to comment https://forums.phpfreaks.com/topic/243765-how-do-i-remove-this-last-character-in-an-array/#findComment-1251661 Share on other sites More sharing options...
Alex Posted August 4, 2011 Share Posted August 4, 2011 In regards to our arguments, nothing personal but I guess you think of it more in the long run whereas I was proposing a quicker example. My simple question that the Data can be edited on the spot and printed out, but whereas I was mentioning that if the user needs to edit $single_items, he would need to count the number of elements, do a for loop, and such. I'm not really sure what you mean. Maybe I didn't make my point clear enough. The whole concept behind my idea is that you keep the data separate from the output. I wouldn't suggest that you format the data in the string, then have to edit it like that later on, that's unnecessary. But if you keep the data separate from the formatting, then you can change the formatting however you want in the future without having to deal with the logic. This is pretty much the whole idea behind template engines. Quote Link to comment https://forums.phpfreaks.com/topic/243765-how-do-i-remove-this-last-character-in-an-array/#findComment-1251665 Share on other sites More sharing options...
phpSensei Posted August 4, 2011 Share Posted August 4, 2011 Which is why I told OP the exact same thing Alex, however in his case, he just wanted to simply print out the titles to the screen. Your method of data handling would require same amount of work as mine, but since the OP has pm'ed me several times about what he is trying to do, I just posted a more suitable solution to his problem. I doubt he is doing anything OOP related either, he said he is a beginner in his pm's. Your right though, keeping the content seperate from the output would be better, but based on his level of training, he has trouble passing with much simpler things. Quote Link to comment https://forums.phpfreaks.com/topic/243765-how-do-i-remove-this-last-character-in-an-array/#findComment-1251668 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.