1
    <?php 
    print_r($optimum);
    $dataNumRows = count($optimum); 
    ?>

    <?php for ($i = 0; $i < $dataNumRows; $i++) : ?>
        <?php echo $cFirstName; ?>
        <?php echo $cLastName; ?>
    <?php endfor; ?>

print_r在我的视图中插入显示以下内容:

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;           
}

我的控制器如下

{
    $this->load->library('table');
    $this->load->model('GetData');
    $data['optimum'] = $this->GetData->getAllCustomersPendingInstall();
    $this->load->view('welcome_message', $data);
}

我的问题是如何在 VIEW 中正确使用 FOR 循环,以便循环遍历所有返回的行。如您所见,print_r它正确返回了正确的行-但是我无法遍历它们。谢谢您的帮助!非常感激!

4

2 回答 2

1

我认为您要做的是为从数据库返回的每一行获取一个关联数组。如果我错了,请纠正我。

应该解决你的问题

$data = array();
$data_index = 0;
if($query->num_rows != 0) {
            foreach($query->result() as $row) {
                $data[$data_index]['cfirst'] = $row->customerFirstName;
                $data[$data_index]['clast'] = $row->customerLastName;
                $data_index++;
            }
        } else {
            $data = "No pending installs available!";
            return $data;
        } 

然后在你看来(数组在$customer哪里)$data

<?php foreach($customer as $c):?>
<?php echo $c['cfirst'];?>
<?php endforeach;?>
于 2013-04-06T21:42:43.503 回答
1

在你看来试试这个:

<?php for ($i = 0; $i < $dataNumRows; $i++) : ?>
    <?php echo $optimum['cFirstName'][$i]; ?>
    <?php echo $optimum['cLastName'][$i]; ?>
<?php endfor; ?>
于 2013-04-06T21:47:29.043 回答