-1

I have write some code for send sms to multiple users with database values i will get the mobile numbers to that code but i will send sms to that only single mobile number msg can passed remain numbers not accepting but i have send the no of mobile numbers are there all members msg will send i will write some code in here once check how to send and also i will try this code working only one mobile number

in the below code i will get records 2.

mobile numbers are also display 2 values.

sendSMS() function i will placed in while loop and outer while loop but last record will be selected.

$get_donor = "SELECT * FROM donor_register WHERE `dnr_bloodgroup` ='$blood_group' and `dnr_city` = '$city' ";
          $get_result = $conn->query($get_donor);
            $count = $get_result->num_rows;

            if($count > 0)
            {
            while($row = $get_result->fetch_assoc())
            {
                  $phone ='';
                  $phone .=$row['dnr_phone']; 
                  echo $phone;
           } 
           sendSMS("Pname",$phone,$message);
         }

My sendSMS function like this

function sendSMS($sender, $mobile_number, $message)
{
//some code 
}

I Expected out put is Pname 3214569870,7412589630,4561237890... this will i will ecpected

But i have get it pname 3214569870 only.

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 1
    You have `$phone ='';` inside the while loop. –  Jul 31 '19 at 11:22
  • but my output look like this 78945612303214569870 but i expected out put is 3214569870,7412589630,4561237890 this way – Nagendra Kumar Jul 31 '19 at 11:24
  • 2
    If you want commas in between, use `$phone[] = $row['dnr_phone'];`, then `echo join(", ", $phone);` –  Jul 31 '19 at 11:26
  • @ChrisG seems, ^ it should be the answer :) – splash58 Jul 31 '19 at 11:27
  • @ChrisG not working the out put look like this error will came $phone[] = $row['dnr_phone']; echo join(", ", $phone); Uncaught Error: [] operator not supported for strings – Nagendra Kumar Jul 31 '19 at 11:30
  • Before the loop, put `$phone = [];` –  Jul 31 '19 at 11:33
  • Change your SQL to be `SELECT GROUP_CONCAT(dnr_phone) AS phone_list FROM donor_register WHERE...`, then it is 1 record with `$row['phone_list']` as the list of numbers. – Nigel Ren Jul 31 '19 at 11:44
  • Possible duplicate of [How do I create a comma-separated list from an array in PHP?](https://stackoverflow.com/questions/2435216/how-do-i-create-a-comma-separated-list-from-an-array-in-php) – Dharman Aug 01 '19 at 17:40
  • this is another concept and that one is another concept – Nagendra Kumar Aug 08 '19 at 12:04

4 Answers4

2

declare $phone variable out side of while loop

$phone ='';
while($row = $get_result->fetch_assoc())
            {
                  if($phone) {
                       $phone .=','.$row['dnr_phone']; 
                  } else {
                       $phone .=$row['dnr_phone']; 
                  }

                  echo $phone;
           }
Devsi Odedra
  • 5,244
  • 1
  • 23
  • 37
2

Try this

    $phone ='';
    while($row = $get_result->fetch_assoc())
    {

      if($phone && $row['dnr_phone']) {
          $phone .= ',';
        } 
      $phone .=$row['dnr_phone'];
    }
    echo $phone;
1
    if($count > 0)
    {   
        $phone ='';
        while($row = $get_result->fetch_assoc())
        {
            if($phone==''){
                $phone .=$row['dnr_phone']; 
            }else{
                $phone .=','.$row['dnr_phone']; 
            }
            echo $phone;
        } 
        sendSMS("Pname",$phone,$message);
    }
0

Due to above code, $phone takes only last mobile number,because sendSMS function called after while loop.

Solution:: sendSMS("Pname",$phone,$message); This function should be in while loop.