Codeigniter/PHP如何正确使用for循环?
我的型号如下Codeigniter/PHP如何正确使用for循环?,php,codeigniter,Php,Codeigniter,我的型号如下 Array ( [cFirstName] => Array ( [0] => Tom [1] => Alexa ) [cLastName] => Array ( [0] => Jones [1] => Planter ) ) //Get all the customers currently pending //install for the user making the request. function getAllCustomers
Array ( [cFirstName] => Array ( [0] => Tom [1] => Alexa ) [cLastName] => Array ( [0] => Jones [1] => Planter ) )
//Get all the customers currently pending
//install for the user making the request.
function getAllCustomersPendingInstall()
{
$data=array();
//Need to use sessions to display proper
//records for each user. Temp set id to user #7
$id = 7;
//query the db and return all record where SalesRepId == $id
$query = $this->db->get_where('customers', array('SalesRepId' => $id));
//check logic, if rows exist RETURN all rows, else
//return message that no pending installs is available.
if($query->num_rows != 0) {
foreach($query->result() as $row) {
$data['cFirstName'][] = $row->customerFirstName;
$data['cLastName'] [] = $row->customerLastName;
}
} else {
$data = "No pending installs available!";
return $data;
}
//the following var_dump is only showing the last record.
//need to show all rows (which should be 2)
//var_dump($data); exit;
return $data;
}
我的控制器如下所示
Array ( [cFirstName] => Array ( [0] => Tom [1] => Alexa ) [cLastName] => Array ( [0] => Jones [1] => Planter ) )
//Get all the customers currently pending
//install for the user making the request.
function getAllCustomersPendingInstall()
{
$data=array();
//Need to use sessions to display proper
//records for each user. Temp set id to user #7
$id = 7;
//query the db and return all record where SalesRepId == $id
$query = $this->db->get_where('customers', array('SalesRepId' => $id));
//check logic, if rows exist RETURN all rows, else
//return message that no pending installs is available.
if($query->num_rows != 0) {
foreach($query->result() as $row) {
$data['cFirstName'][] = $row->customerFirstName;
$data['cLastName'] [] = $row->customerLastName;
}
} else {
$data = "No pending installs available!";
return $data;
}
//the following var_dump is only showing the last record.
//need to show all rows (which should be 2)
//var_dump($data); exit;
return $data;
}
我的问题是如何在视图中正确地使用FOR循环,以便循环所有返回的行。正如您所看到的,
打印\u r
正确地返回了正确的行-但是我无法循环它们。谢谢你的帮助!非常感谢 我认为您要做的是为数据库返回的每一行获取一个关联数组。如果我错了,请纠正我
应该解决你的问题
{
$this->load->library('table');
$this->load->model('GetData');
$data['optimum'] = $this->GetData->getAllCustomersPendingInstall();
$this->load->view('welcome_message', $data);
}
然后在您的视图中(其中$customer
是$data
数组)
我认为您要做的是为数据库返回的每一行获取一个关联数组。如果我错了,请纠正我 应该解决你的问题
{
$this->load->library('table');
$this->load->model('GetData');
$data['optimum'] = $this->GetData->getAllCustomersPendingInstall();
$this->load->view('welcome_message', $data);
}
然后在您的视图中(其中$customer
是$data
数组)
在您的视图中尝试以下操作:
<?php foreach($customer as $c):?>
<?php echo $c['cfirst'];?>
<?php endforeach;?>
在您的视图中尝试以下操作:
<?php foreach($customer as $c):?>
<?php echo $c['cfirst'];?>
<?php endforeach;?>
这不回显任何内容这不回显任何工作内容,但什么是数据索引?为什么需要允许代码运行?您需要创建一个数组数组。您以前的做法是创建一个带有2个键的关联数组,然后在模型中覆盖foreach循环每次迭代的值。感谢您的帮助,虽然您的答案工作正常,但我选择了上面的答案,因为我觉得它与我的原始代码融合得更好。非常感谢。这是可行的,但什么是数据索引?为什么需要允许代码运行?您需要创建一个数组数组。您以前的做法是创建一个带有2个键的关联数组,然后在模型中覆盖foreach循环每次迭代的值。感谢您的帮助,虽然您的答案工作正常,但我选择了上面的答案,因为我觉得它与我的原始代码融合得更好。非常感谢。