Friday, October 18, 2013

Remove last character of a string, for example comma and a space



In the following example, I combined all reviewer full name using ',  ' (comma and  a pace), but I need to remove the last comma and space when they are displayed.  PHP function rtrim can be used for this purpose, example PHP code:
  $sql_1 = "SELECT
                  M.prefix,M.first_name,M.last_name
                  FROM cesei_module_user_reviews MUR
                  LEFT JOIN cesei_members M ON MUR.member_id=M.member_id WHERE MUR.module_id=".$module_id ;
  $result_1 = mysql_query($sql_1, $db);
   $reviewers = ' ';
 while($row_1 = mysql_fetch_assoc($result_1)){
             $reviewers .= trim(htmlspecialchars($row_1['prefix'].' '.$row_1['first_name'].' '.$row_1['last_name'])).',  ';
        
    }
    $reviewers = rtrim($reviewers, ", ");

No comments:

Post a Comment