doulan6150
doulan6150
2012-08-18 22:18

使用PHP或Javascript删除最后一个逗号

I have tried many different things to do this and all my ideas have failed me. How do i get rid of the last comma after running this while loop?

<body onload="
    <?php while($row = mysql_fetch_assoc($thing3)): ?>
        checkEdits_<?php echo $row['directory_name']; ?>(),
    <?php endwhile; ?>
">

It adds the last comma at the end which ruins the whole script.

The php while(loop) puts this out when ran through the dom:

<body onLoad="checkEdits_juk(), checkEdits_HTML(), checkEdits_JAVASCRIPT(), checkEdits_JQUERY(),">

except i don't want the last comma at the end. Is their a way to get rid of it?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

5条回答

  • dongqie4402 dongqie4402 9年前

    you can put your data in an array and uese the implode function

      $directory_names = array();
      while($row = mysql_fetch_assoc($thing3)) {
        $directory_names[] = $row['directory_name'];
      }
      echo implode(', ', $directory_names);
    
    点赞 评论 复制链接分享
  • doumei9589 doumei9589 9年前

    Use rtrim to take off the last comma. I'd rewrite like this:

    <?php 
    $onload = "";
    while($row = mysql_fetch_assoc($thing3)) {
        $onload .= "checkEdits_" . $row['directory_name'] . ", ";
    }
    $onload = rtrim($onload, ', ');
    ?>
    
    <body onload="<?php echo $onload; ?>">
    
    点赞 评论 复制链接分享
  • dougu4027 dougu4027 9年前

    Or you can use:

    <body onload="function() {
        <?php while($row = mysql_fetch_assoc($thing3)) { ?>
            checkEdits_<?php echo $row['directory_name']; ?>(); 
        <?php } ?>
    }">
    
    点赞 评论 复制链接分享
  • duanchi4544 duanchi4544 9年前

    With your array of MYSQL rows, use php's join function (implode) http://php.net/manual/en/function.join.php

    点赞 评论 复制链接分享
  • duanmei2459 duanmei2459 9年前
    substr($yourString, 0, strlen($yourString) - 1);
    

    This returns a substring of your string that contains everything from the 0th index to the last - 1 position.

    点赞 评论 复制链接分享

相关推荐