在for循环中使用代码点火器生成一系列数字并保存在数据库中
问题描述:
Iam尝试使用for循环在codeigniter中生成随机数。生成的数字应保存在数据库中。在我的代码中,只有一行被添加到数据库中。下面是我的代码。在for循环中使用代码点火器生成一系列数字并保存在数据库中
查看
<form action="<?php echo site_url('/Gen/gen_num'); ?>" method="post">
<select name="country_code_id" class="form-control show-tick" required>
<option value="">263</option>
</select>
<select name="mobile_operator_prefix_id" class="form-control show-tick" required>
<option value="0">772</option>
<option value="1">774</option>
<option value="2"> 774</option>
</select>
<input type="hidden" name="mobile_number" placeholder="Mobile Number">
<input type="text" class="form-control" name="quantity" placeholder="How many numbers ?" required>
<button class="btn btn-block bg-pink waves-effect" type="submit">SUBMIT</button>
</form>
位指示
$quantity = $this->input->post('quantity');
for ($x=0; $x<$quantity; $x++); {
$mobile_number = rand(10000, 99999);
$data = array(
'mobile_operator_prefix_id' => $this->input->post('mobile_operator_prefix_id'),
'country_code_id' => $this->input->post('country_code_id'),
'mobile_number' => $mobile_number,
);
$this->Gen_model->save_number($data);
}
型号
function save_number($data){
$this->db->insert('mobile_numbers', $data);
}
答
你的代码看起来很好 - 你只是个人有T Ø删除您分号后的for循环 - 试试这个,而不是
$quantity = $this->input->post('quantity');
for ($x=0; $x<$quantity; $x++)
{
$mobile_number = rand(10000, 99999);
$data = array(
'mobile_operator_prefix_id' => $this->input->post('mobile_operator_prefix_id'),
'country_code_id' => $this->input->post('country_code_id'),
'mobile_number' => $mobile_number,
);
$this->Gen_model->save_number($data);
}
为了更好地理解,你可以在this PHP Sandbox example
看看Owwwhh..that是问题。我删除了分号,它的功能就像一个魅力。非常感谢你 –