genzedu777 Posted January 24, 2011 Share Posted January 24, 2011 Hi guys, Currently my result displayed a 'comma' at the end Lower Primary English, Math, Science, Chinese, How do I remove the comma which is bold in red? Thank you so much My code while($row3 = mysqli_fetch_array($data3)) { if ($row3['level_id'] == 2) { echo ''.$row3['subject_name'].', '; } } Quote Link to comment https://forums.phpfreaks.com/topic/225499-unable-to-remove-the-comma-at-the-last-value/ Share on other sites More sharing options...
PFMaBiSmAd Posted January 24, 2011 Share Posted January 24, 2011 There are several different ways you could do this - 1) Use GROUP_CONCAT() in your query to return the list of values the way you want, 2) Echo the ', ' before each value, except on the first pass through the loop, 3) Put the values into an array and use implode() to form the list of values, 4) Concatenate the values into a string and then trim the extra ', ' off of the end of the string. Quote Link to comment https://forums.phpfreaks.com/topic/225499-unable-to-remove-the-comma-at-the-last-value/#findComment-1164402 Share on other sites More sharing options...
PFMaBiSmAd Posted January 24, 2011 Share Posted January 24, 2011 5) Concatenate the values into a string, putting the ', ' before each value, except on the first pass through the loop. Quote Link to comment https://forums.phpfreaks.com/topic/225499-unable-to-remove-the-comma-at-the-last-value/#findComment-1164407 Share on other sites More sharing options...
cyberRobot Posted January 24, 2011 Share Posted January 24, 2011 And yet another option: $separator = ''; while($row3 = mysqli_fetch_array($data3)) { if($row3['level_id'] == 2) { echo $separator.$row3['subject_name']; $separator = ', '; } } Quote Link to comment https://forums.phpfreaks.com/topic/225499-unable-to-remove-the-comma-at-the-last-value/#findComment-1164409 Share on other sites More sharing options...
genzedu777 Posted January 24, 2011 Author Share Posted January 24, 2011 Thanks PFMaBiSmAd. Hey CyberRobot! It works! How did you do it Can you explain the code briefly? I dont quite understand how it works. How did you take away the ',' at the end? $separator = ''; while($row3 = mysqli_fetch_array($data3)) { if($row3['level_id'] == 2) { echo $separator.$row3['subject_name']; $separator = ', '; } } Quote Link to comment https://forums.phpfreaks.com/topic/225499-unable-to-remove-the-comma-at-the-last-value/#findComment-1164429 Share on other sites More sharing options...
PFMaBiSmAd Posted January 24, 2011 Share Posted January 24, 2011 That code is an implementation of choice #2 on the list of possible ways to do this. Can you not look at that code and step through what it is doing and see that it is setting $separator to an empty string before the first pass through the loop so that nothing will be echoed before the first value. It then sets $separator to the actual separator value after the first value has been echoed so that the remainder of the values will have the ', ' separator. Quote Link to comment https://forums.phpfreaks.com/topic/225499-unable-to-remove-the-comma-at-the-last-value/#findComment-1164432 Share on other sites More sharing options...
cyberRobot Posted January 24, 2011 Share Posted January 24, 2011 First we created a variable called "$separator" and made sure it was blank. $separator = ''; Then in the loop, we display whatever "$separator" holds along with the "subject_name". Note that the first time through the loop "$separator" will be blank. Every other time it will contain ', ' which is assigned after the echo statement. ... echo $separator . $row3['subject_name']; $separator = ', '; ... Hopefully that makes sense, let me know if it doesn't. Quote Link to comment https://forums.phpfreaks.com/topic/225499-unable-to-remove-the-comma-at-the-last-value/#findComment-1164440 Share on other sites More sharing options...
litebearer Posted January 24, 2011 Share Posted January 24, 2011 AND by displaying it BEFORE the value rather than AFTER, the result is no comma on the end Quote Link to comment https://forums.phpfreaks.com/topic/225499-unable-to-remove-the-comma-at-the-last-value/#findComment-1164446 Share on other sites More sharing options...
genzedu777 Posted January 24, 2011 Author Share Posted January 24, 2011 Thanks! Got it now! Quote Link to comment https://forums.phpfreaks.com/topic/225499-unable-to-remove-the-comma-at-the-last-value/#findComment-1164488 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.