Send SMS multiple times

240 Views Asked by At

I have a problem on sending SMS using Ozeki Message Server. I want to send an sms multiple times, but it will only send when I reload the page manually.

Here's my code:

        <?php

    $balance = mysqli_query($connection, "SELECT * FROM balance WHERE status = 'Installment' OR status = 'No Downpayment' AND schoolyear = '$schoolyear'");
    $totalstud = mysqli_num_rows($balance);

    for($i=0; $i<$totalstud; $i++)
    {
      while($row_bal = mysqli_fetch_array($balance))
      {
        $student_id = $row_bal['student_id'];
        $outstanding_balance = $row_bal['outstanding_balance'];

        $student = mysqli_query($connection, "SELECT * FROM student WHERE student_id = '$student_id'");

        while($row_stud = mysqli_fetch_array($student))
        {
          $firstname = $row_stud['firstname'];
          $lastname = $row_stud['lastname'];
          $cp_num = $row_stud['cp_num'];

          $schedule = mysqli_query($connection, "SELECT * FROM schedule WHERE schoolyear = '$schoolyear'");

          while($row_sched = mysqli_fetch_array($schedule))
          {
            $date = $row_sched['date'];
            $time = $row_sched['time'];
            $message = $row_sched['message'];

            $message = $firstname . " " . $lastname . " " . $message . "" . $outstanding_balance . ".";

            echo "MESSAGE: " . $message . "<br><br>";
            $cp[$student_id] = $cp_num;
            $msg[$student_id] = $message;
          }
        }
      }
    }

    for($i=1; $i<=$totalstud; $i++)
    {
      $gatewayURL  =   'http://localhost:9333/ozeki?';
      $request = 'login=admin';
      $request .= '&password=abc123';
      $request .= '&action=sendMessage';
      $request .= '&messageType=SMS:TEXT';
      $request .= '&recepient='.urlencode($cp[$i]);
      $request .= '&messageData='.urlencode($msg[$i]);
      $request .= '&sender='.urlencode("OZEKI");

      $url =  $gatewayURL . $request;  

      //Open the URL to send the message
       file($url);

      if($i < $totalstud)
      {

      //This reloads page
      $page = $_SERVER['PHP_SELF'];
      $sec = "2";
    ?>
      <html>
          <head>
            <meta http-equiv="refresh" content="<?php echo $sec?>;URL='<?php echo $page?>'">
          </head>
          <body>
          <?php
              //echo "Watch the page reload itself in 10 second!";
          ?>
          </body>
      </html>
    <?php
      }
    }
    ?>

In my code I fetch data from my database. The $totalstud which counts the total number of students with Installment or No Downpayment. This will be the basis of how many times the SMS will be sent.

But what happen is, the message has been sent more than the $totalstud. Anyone knows how I can send the sms base from the $totalstud?

Assume that:

$totalstud = 3;
0

There are 0 best solutions below