0

I would want to capture $status and insert it in a database 'smsdb'. However, I'm unable to do so and wish someone would guide me. This starting code is part of wall function and thats where the status is got from after calling the function. In the grabdetails function where I get the other details into the db, $status is inaccessible. Would someone lead me please... // code

 $name = $resultarr['name'];
 $amount = $resultarr['amount'];
 $transaction_id = $resultarr['trans_id'];
 $date = $resultarr['time_paid'];

//message template
$message = "Dear $name we have received $amount from you. MPESA transaction Id $transaction_id on $date.";

$mobilenumber = $resultarr['msisdn']; // get mobile number from array
$message_sent = $message;

$serviceArguments = array(
        "mobilenumber" => $mobilenumber,
        "message" => $message_sent
);

$client = new SoapClient("http://59.38.606.10:8080/smsengine/smsws?WSDL");

$result = $client->process($serviceArguments);

grabdetails($message_sent, $mobilenumber);

return $result;

} 
 //I call the function wall() to send sms         

 wall();
$perm = wall();
$status = $perm->return; //outputing the status
  // Here I want to capture the $status variable and put it in a db below
echo "$status";


function grabdetails($messagee, $mobno)
{

$message_sent = $messagee;
$mobilenumber = $mobno;


$servername = "localhost";
$username = "root";
$password = "";
$dbname = "smsdb";

// Create connection


// Check connection

 $sql = "INSERT INTO smsdb (sms_text, receiver_number, time_sent, status)
   VALUES
     ('$message_sent', '$mobilenumber', NOW(), '$status' )";
 $conn->query($sql);

Any one?

8
  • please give proper opening and closing braces in your code Commented Jun 13, 2016 at 11:28
  • where exactly? Kindly Commented Jun 13, 2016 at 11:32
  • where does this function grabdetails end? it has no closing braces and also where is function wall? Commented Jun 13, 2016 at 11:34
  • the starting part of the code are part of the wall function that ends just by the bracket when it is called. The grabdetails function ends just after the last line of code here. Thought it would be too long if I included everything. Commented Jun 13, 2016 at 11:36
  • you can pass status are parameter to function grabdetails. did you try that? Commented Jun 13, 2016 at 11:43

1 Answer 1

1

The $status variable is out of scope.

Add the line:

global $status;

To your function.

http://php.net/manual/en/language.variables.scope.php

EDIT:

When you call grabdetails within the wall() function, the $status variable is not yet set. Maybe pass the status as a parameter in the grabdetails function from within the wall() function.

e.g. grabdetails($message_sent, $mobilenumber, $result);

Also change the function declaration to grabdetails($messagee, $mobno, $status);

Sign up to request clarification or add additional context in comments.

7 Comments

Should I include global here global $status = $perm->return;? And when inserting in the database?
function grabdetails($messagee, $mobno, $status) { like this? What of the calling in the wall function where is expects two parameters? I mean here grabdetails($message_sent, $mobilenumber);
Add the result variable as a parameter when you call the method. grabdetails($message_sent, $mobilenumber, $result);. Isn't $result->return == $status?
grabdetails($message_sent, $mobilenumber, $result); And later this function grabdetails($messagee, $mobno, $perm->return) { $message_sent = $messagee; $mobilenumber = $mobno; $status = $perm->return; But could not work
Try grabdetails($message_sent, $mobilenumber, $result->return); and grabdetails($messagee, $mobno, $status);
|

Your Answer

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