Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

php - codeigniter: pass array from controller to view

I have CodeIgniter question. How can I pass an array from controller to view? Here is my code that doesn't work:

controller:

$data_part13['header3_item'][] = array('title' => 'first image 1' , 'img' => 'https://encrypted-tbn0.google.com/images?q=tbn:ANd9GcQoshslL3aMNzG50708domqPSA4ouPjk_wA7jCpVRUH3k8zVdn9' );

$this->load->view('part_1_3', $data_part13);

and view:

<div id="header3">
    <div id="header3-inner">
        <?php
        if (isset($header3_item)){
            foreach ($header3_item as $key) {
        ?>
                <div class="header3-item">
                    <img alt="<?php echo($key->title); ?>" src="<?php echo($key->img); ?>"/>
                </div>
        <?php
            }
        }
        ?>
    </div>
</div>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You did it correctly (kinda). You passed an array to the view, but your problem was that you were using an object in the view. You should have instead done something like this:

$data_part13['header3_item'][] = (object) array('title' => 'first image 1' , 'img' => 'https://encrypted-tbn0.google.com/images?q=tbn:ANd9GcQoshslL3aMNzG50708domqPSA4ouPjk_wA7jCpVRUH3k8zVdn9' );

$this->load->view('part_1_3', $data_part13);

The view part can stay the same.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...