Jump to content

[SOLVED] Please help with this!!


rn14

Recommended Posts

Im trying to pass the values returned to the variable $to so that a message is sent to each number individualy but it only sends it to one number not them all

 

$query = "SELECT `mobile` FROM `customer`";

    $result = mysql_query($query) or die(mysql_error());

    $number = array();

    while(list($mobile) = mysql_fetch_row($result)) { $number[] = $mobile; }

 

    // Test it

    foreach($number as $mobile) { echo $mobile . "

"; }

 

 

 

 

 

$user = "xxxx";

$password = "";

$api_id = "";

$baseurl ="http://api.clickatell.com";

$text = $_POST['text'];

$to = $mobile;

 

 

Link to comment
https://forums.phpfreaks.com/topic/39503-solved-please-help-with-this/
Share on other sites

I need to loop through a database, take the phone number value, send an individual message to each number. This is my attempt. It only takes one number from the database insteading of looping through and sending a message to all the numbers.

 

$query = "SELECT `mobile` FROM `customer`";
    $result = mysql_query($query) or die(mysql_error());
    $number = array();
    while(list($mobile) = mysql_fetch_row($result)) { $number[] = $mobile; }

    // Test it
    foreach($number as $mobile) { echo $mobile . "
"; }

// These are the details for my clickatell account. the $to variable needs to be passed the phone numbers from database
$user = "xxxx";
$password = "";
$api_id = "";
$baseurl ="http://api.clickatell.com";
$text = $_POST['text'];
$to = $mobile; 




Basically like this

 

<?php
// queries to retrieve the numbers, let's assume $row is holding the fetch array
// have your phone formatting here.  I don't know what it takes to send a sms (phone message), through
// php, so I can't help on that part
while ($row = mysql_fetch_array($query)) {
   // mail for each phone number here.
}
?>

This is at least the third time you've asked this question today. The usual practice on this forum is to ask the question once and all the answers/replies will be in one place.

 

This section of code:

<?php
// These are the details for my clickatell account. the $to variable needs to be passed the phone numbers from database
$user = "xxxx";
$password = "";
$api_id = "";
$baseurl ="http://api.clickatell.com";
$text = $_POST['text'];
$to = $mobile;
?>

needs to be within the for loop.

 

Ken

I meant to say "while" loop, not "for" loop.

<?php
$query = "SELECT mobile FROM customer";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_assoc($result)) {

// These are the details for my clickatell account. the $to variable needs to be passed the phone numbers from database
       $user = "xxxx";
       $password = "";
       $api_id = "";
       $baseurl ="http://api.clickatell.com";
       $text = $_POST['text'];
       $to = $row['mobile'];
}
?>

 

Ken

Iv included the entire script probarly too much code. It will send a message but only too one number in the database not all the numbers in the database. I have included the form at the bottom where the person enters the message. Also in this you will see a text box where a number can be entered. This was because this script was initially meant to send to just an individual number.

 

<cfoutput>
<cfhttp url="http://api.clickatell.com/http/sendmsg" method="POST" resolveurl="false">
<cfhttpparam type="FORMFIELD" name="api_id" value="xxxxxxx">
<cfhttpparam type="FORMFIELD" name="user" value="xxxxxxx">
<cfhttpparam type="FORMFIELD" name="password" value="xxxxxxxxx">
<cfhttpparam type="FORMFIELD" name="text" value="<?$_POST['text'];?>">
<cfhttpparam type="FORMFIELD" name="to" value="">
</cfhttp>
</cfoutput>
PHP
<?
//dbconnection here

$query = "SELECT mobile FROM customer";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_assoc($result)) {

// These are the details for my clickatell account. the $to variable needs to be passed the phone numbers from database
       $user = "xxxx";
       $password = "xxxxx";
       $api_id = "xxxxx";
       $baseurl ="http://api.clickatell.com";
       $text = $_POST['text'];
       $to = $row['mobile'];
}
// auth call
$url = "$baseurl/http/auth?user=$user&password=$password&api_id=$api_id";
// do auth call
$ret = file($url);
// split our response. return string is on first line of the data returned
$sess = split(":",$ret[0]);
if ($sess[0] == "OK") {
$sess_id = trim($sess[1]); // remove any whitespace
$url = "$baseurl/http/sendmsg?session_id=$sess_id&to=$to&text=$text";
// do sendmsg call
$ret = file($url);
$send = split(":",$ret[0]);
if ($send[0] == "ID")
echo "success<br>message ID: ". $send[1];
else
echo "send message failed";
} else {
echo "Authentication failure: ". $ret[0];
exit();
}
?>

 

<form action="page2.php" method="post">
To:<input type="Text" name="to"><br><br>
Message:<input type="text" name="text">
<br><br><input type="submit" value="send">
</form>

You need to put all the code that deals with sending the message within the "while" loop or you will only send to the last number retrieved.

<?php
//dbconnection here

$query = "SELECT mobile FROM customer";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_assoc($result)) {

// These are the details for my clickatell account. the $to variable needs to be passed the phone numbers from database
       $user = "xxxx";
       $password = "xxxxx";
       $api_id = "xxxxx";
       $baseurl ="http://api.clickatell.com";
       $text = $_POST['text'];
       $to = $row['mobile'];
// auth call
       $url = "$baseurl/http/auth?user=$user&password=$password&api_id=$api_id";
// do auth call
       $ret = file($url);
// split our response. return string is on first line of the data returned
       $sess = split(":",$ret[0]);
       if ($sess[0] == "OK") {
           $sess_id = trim($sess[1]); // remove any whitespace
           $url = "$baseurl/http/sendmsg?session_id=$sess_id&to=$to&text=$text";
// do sendmsg call
           $ret = file($url);
           $send = split(":",$ret[0]);
           if ($send[0] == "ID")
               echo "success<br>message ID: ". $send[1];
           else
               echo "send message failed for $to";
       } else
          exit("Authentication failure: ". $ret[0]);
}
?>

 

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.