[英]selecting phone from database in codeigniter
我正尝试将消息发送给今天有生日的客户。 如果2个客户有生日,但仅向第一个客户发送两次SMS。
我的控制器就像
$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!');
}
}
首先获取所有数据,然后遍历该数据并发送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!');
}
}
在您的初始代码中,您使用了row(),这意味着您仅获得一个数字,而不是数字数组。 使用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.');
}
如果没有发送SMS,这应该足以帮助您调试代码。
当您使用foreach循环时,无论数据是1还是更大,它仍然应该工作。 如果您在应用程序中运行此代码,则需要应用时间逻辑并将SMS发送的结果保存到数据库中。 这样它就不会再意外发送给该人了,如果您将其作为cron作业运行,那么将其设置为每天一次就没有问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.