2

I have a and Array which looks like this:

$sms = array(
  'from' => 'DummyFrom', 
  'to' => '+46709751949',
  'message' => 'Hello hello!'
);
echo sendSMS ($sms) . "\n";

what i'm trying to do is to put this array within a foreach loop so it can be executed multiple time (based on the given times from mysql database). to explain it better, I did something like this:

if (is_array($g_numbers)) {
    foreach ($g_numbers as $number) {
        $sms = array(
            'from' => 'DummyFrom',
            'to' => "" . $number . "",
            'message' => 'Hello hello!'
        );
        echo sendSMS($sms) . "\n";
    }
}

but that is wrong and its stops the PHP page to execute properly without any errors!

Could someone please advise on this issue?

My full code :

<?php
$people       = array();
$sql          = "SELECT id, g_name, numbers FROM groups WHERE g_name='$groups'";
$query        = mysqli_query($db_conx, $sql);
$productCount = mysqli_num_rows($query);
while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {

    $g_id               = $row['id'];
    $g_name             = $row['g_name'];
    $g_numbers          = $row['numbers'];
    $people[$g_numbers] = $g_numbers;

}
?>
<?
// Example to send SMS using the 46elks service
// Change $username, $password and the mobile number to send to

function sendSMS($sms)
{

    // Set your 46elks API username and API password here
    // You can find them at https://dashboard.46elks.com/
    $username = 'xxxxxxxxxxxxxxxxxxxxxxxx';
    $password = 'xxxxxxxxxxxxxxxxxxxxxxxx';

    $context = stream_context_create(array(
        'http' => array(
            'method' => 'POST',
            'header' => "Authorization: Basic " . base64_encode($username . ':' . $password) . "\r\n" . "Content-type: application/x-www-form-urlencoded\r\n",
            'content' => http_build_query($sms),
            'timeout' => 10
        )
    ));

    $response = file_get_contents('https://api.46elks.com/a1/SMS', false, $context);

    if (!strstr($http_response_header[0], "200 OK"))
        return $http_response_header[0];

    return $response;
}
if (is_array($g_numbers)) {
    foreach ($g_numbers as $number) {
        $sms = array(
            'from' => 'DummyFrom',
            /* Can be up to 11 alphanumeric characters */
            'to' => "" . $number . "",
            /* The mobile number you want to send to */
            'message' => 'Hello hello!'
        );
        echo sendSMS($sms) . "\n";
    }
}
?>
3
  • You are foreaching $g_numbers and not providing us that code. is $g_numbers an array when you print_r it? Is it even populated? Is it an empty array? What is the output to print_r($g_numbers); Commented Oct 25, 2015 at 14:33
  • @Jesse, i've added my entire code now. sorry about that. Commented Oct 25, 2015 at 14:35
  • $g_numbers = $row['numbers']; are you sure you don't want to do $g_numbers[] = $row['numbers']; Commented Oct 25, 2015 at 14:38

1 Answer 1

1

This will work because tables values are stored in $people:

if (is_array($people)) {
  foreach ($people as $number) {
    $sms = array(
        'from' => 'DummyFrom',
        /* Can be up to 11 alphanumeric characters */
        'to' => "" . $number . "",
        /* The mobile number you want to send to */
        'message' => 'Hello hello!'
    );
    echo sendSMS($sms) . "\n";
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.