drn1008
drn1008
2016-03-26 11:30

如何在从mysql中取出的记录之间放一个逗号而不是结束或开始php

已采纳

I want to put a comma in between the numbers I pull out of the database , but when I do it , it sometimes put the comma at the end and beginning which I don’t like

I want the numbers to be = 44123123123,44188123123,44123123123

Not = , 44123123123,44188123123,44123123123 Or = 44123123123,44188123123,44123123123,

I have tried everything but no luck . please help see also Bellow is what I have tried

$comma =",";
while($record = $targe1->fetch(PDO::FETCH_ASSOC)){
$cell = $record['cell'] ;
$company = $record['company'];
$surname = $record['surname'];

    $final_cell1 = explode(",",$record['cell']);

$final_cell = implode(",",$final_cell1).$comma;


echo $final_cell;

 }

I am writing a script which sends sms it pulls out numbers and names then it sends . the bulk sms plug in for sending many sms at once , it requires the numbers to be separated by a comma and if there is a comma at the begging or end it ignores and no sms will be sent . any help will be appreciated .

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

2条回答

  • dsfs23434 dsfs23434 5年前

    To remove the last comma you just need substr()

    Try this:

    while ($record = $targe1->fetch(PDO::FETCH_ASSOC)) {
        /* Other code */
        $cell .= $record['cell'].',';
     }
    
    $cell= substr($cell,0,-1);
    

    Hope this helps.

    点赞 评论 复制链接分享
  • dongtun1209 dongtun1209 5年前

    Gather your numbers in a single array and use implode once to it.

    $records_array=array();
    while($record = $targe1->fetch(PDO::FETCH_ASSOC)){
    $records_array = $record['cell'] ;
    }
    $final_cell=implode(',',records_array );
    
    echo  $final_cell;
    
    点赞 评论 复制链接分享