Jump to content

Unable to remove the comma at the last value


genzedu777

Recommended Posts

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'].', ';
         }
      }

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 = ', ';
   }
}

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.