Cody Krecicki
Cody Krecicki

Reputation: 77

PHP - Twilio SMS MySQL Multiple Numbers - Only sends to first row

First I run this code to get the phone number, first name and last name for the Twilio SMS.

$users_query = mysql_query("SELECT first_name, last_name, phone_no from users WHERE phone_no!=''");
    $users = array();

    while($row = mysql_fetch_array($users_query))
    {
        $users[]=$row;
    }

Then I use this to send the SMS, which should send to all the numbers on the column phone_no.

foreach($users as $user)
    {
        $people = array(
            //$phone_no => $full_name
            $user['phone_no'] => $user['first_name'].' '. $user['last_name']
        );
    }

    // Step 5: Loop over all our friends. $number is a phone number above, and 
    // $name is the name next to it
    foreach ($people as $number => $name) {
        try{
            $sms = $client->account->messages->create(

            // the number we are sending to - Any phone number
            $number,

            array(

Currently the SMS is only sent the first rows phone number. I'm stuck.

Upvotes: 0

Views: 1662

Answers (1)

Ulrich Dohou
Ulrich Dohou

Reputation: 1559

It is normal ... Do this

$people = [];
foreach($users as $user)
{
  $people[] = array(
    'full_name' => $user['first_name'].' '. $user['last_name'],
    'phone_no' => $user['phone_no'],
  );
}

foreach ($people as $one) {

    $name = $one['full_name'];
    $phone = $one['phone_no'];

    $sms = $client->account->messages->create(
        $phone,

        array(
            'from' => "+15017250604", 
            'body' => "Hey $name, Monkey Party at 6PM. Bring Bananas!"
        )
    );
    echo "Sent message to $name";
 }

You should declare $people as an array. Then in the loop you have to add to $people this []

Upvotes: 1

Related Questions