简体   繁体   English

从Codeigniter中的数据库中选择电话

[英]selecting phone from database in codeigniter

I am trying to send the messages to customers who has birthday today. 我正尝试将消息发送给今天有生日的客户。 if 2 customers have birthday but it is sending SMS twice to first customer only. 如果2个客户有生日,但仅向第一个客户发送两次SMS。

my controller is like 我的控制器就像

$sms_count = $this->db->query("select * from tbl_customers where date_of_birth='". $today."' and concat('',phone * 1) = phone")->result();
$sender = $this->input->post($this->security->xss_clean('outlet_name'));
$message = $this->input->post($this->security->xss_clean('message'));
$numbers = array($this->db->query("select phone from tbl_customers where date_of_birth='". $today."'")->row('phone')); 

 foreach ($sms_count as $value) {  
                    try {
                        $result = $textlocal->sendSms($numbers, $message, $sender); 
                        $this->session->set_flashdata('exception', 'SMS has been sent successfully!');
                    } 
                }

First of all get all data, then loop through that data and send SMS as below 首先获取所有数据,然后遍历该数据并发送SMS,如下所示

 $this->db->select('phone');
 $this->db->where('date_of_birth', $today);
 $numbers = $this->db->get('tbl_customers')->result_array();
 if($numbers){
     foreach ($numbers as $key => $value) {
                $result = $textlocal->sendSms($value['phone'], $message, $sender); 
                 $this->session->set_flashdata('exception', 'SMS has been sent successfully!');

       }
   }

In your initial code you used row(), which meant you were only getting one number, rather than an array of numbers. 在您的初始代码中,您使用了row(),这意味着您仅获得一个数字,而不是数字数组。 Using result_array() gives you an associative array. 使用result_array()会为您提供一个关联数组。

 $numbers = $this->db->select('phone')->get_where('date_of_birth', $today)->result_array();
 if(!empty($numbers)){
     foreach ($numbers as $key => $value) {
                 $result = $textlocal->sendSms($value['phone'], $message, $sender); 
                 $this->session->set_flashdata('exception', 'SMS has been sent successfully!');

       }
   } else {
                 var_dump($numbers);
                 $this->session->set_flashdata('exception', 'No Numbers found.');
}

This should be enough to help you debug your code if the SMS isn't sending. 如果没有发送SMS,这应该足以帮助您调试代码。

As you are using a foreach loop regardless if the data is 1 or more it should still work. 当您使用foreach循环时,无论数据是1还是更大,它仍然应该工作。 If you are running this code within your application you will need to apply time logic and save the result of the SMS sending into your DB. 如果您在应用程序中运行此代码,则需要应用时间逻辑并将SMS发送的结果保存到数据库中。 So that it won't send again to the person by accident, If you are running this as a cron job then you have no problem providing its set to once a day. 这样它就不会再意外发送给该人了,如果您将其作为cron作业运行,那么将其设置为每天一次就没有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM