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

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

My print_r inserted in my VIEW shows the following:

Array ( [cFirstName] => Array ( [0] => Tom [1] => Alexa ) [cLastName] => Array ( [0] => Jones [1] => Planter ) )

My MODEL is the following

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

My CONTROLLER is the following

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

And my question is how do I properly use the FOR loop in my VIEW so that I can loop through all the returned rows. As you can see the print_r is properly returning the proper rows- However I am unable to loop through them. Thanks for the help! Much appreciated!

2 Answers 2

1

Try this in your view:

<?php for ($i = 0; $i < $dataNumRows; $i++) : ?>
    <?php echo $optimum['cFirstName'][$i]; ?>
    <?php echo $optimum['cLastName'][$i]; ?>
<?php endfor; ?>
Sign up to request clarification or add additional context in comments.

1 Comment

This does not echo anything
1

I think what you're trying to do is get an associative array for each row returned from the database. Correct me if I'm wrong about that.

Should fix your problem

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

then in your view (where $customer is the $data array)

<?php foreach($customer as $c):?>
<?php echo $c['cfirst'];?>
<?php endforeach;?>

3 Comments

This worked but what is data_index and why is it necessary to allow the code to function?
You need to create an array of arrays. The way you were doing it before was creating an associative array with 2 keys, then overwriting the values on each iteration of the foreach loop in your model.
Thank you for your help and though your answer worked properly I choose the above answer because I felt it blended better with my original code. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.