Codeigniter using foreach in view
Asked Answered
H

8

5

I'm using Codeigniter on an Apache xampp set up and attempting to use foreach in a view for the first time and I just can't get it to work.

My Controller code:

class Test extends CI_Controller {

        public function index($page = 'testv')
    {
            if ( ! file_exists(APPPATH.'/views/'.$page.'.php'))
            {
                show_404();
            }
                $this->load->model('testm');
                $data['news'] = $this->testm->get_news();

                $this->load->view('headder');
                $this->load->view($page, $data);
                $this->load->view('footer');
    }
}

My Model:

class Testm extends CI_Model {

    public function __construct()
    {
        parent::__construct();
    }

    Public function get_news()
    {
        $query = $this->db->query('SELECT id, date, text FROM news');

        foreach ($query->result_array() as $row)
        {
            echo $row['id'];
            echo $row['date'];
            echo $row['text'];
        }
    }
}

My View:

<main id='central_section'>
    <section id='logo_section'>
    <img src='<?php echo base_url(); ?>img/logo.png' id='small_logo' alt="Logo">
    </section>
    <section id='content'>
        <p class="large_headding">News</p>

            <?php foreach($news as $news) { ?>
                <article class="article_text">
                    <p class="segment_headding"><?php echo $news->date; ?></p>
                        <?php echo $news->text; ?>
                </article>
            <?php } ?>
        <div id="spacer"></div>
    </section>
</main>

At present I get the following warning message:

A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: views/testv.php

Line Number: 18

Backtrace:

File: C:\xampp\htdocs\tsh\CI\application\views\testv.php
Line: 18
Function: _error_handler

File: C:\xampp\htdocs\tsh\CI\application\controllers\test.php
Line: 16
Function: view

File: C:\xampp\htdocs\tsh\public_html\index.php
Line: 292
Function: require_once

But the text from my database does appear at the top of the page outside of the headder so I know the database is connecting and the data from it is being collected. Can anyone tell me where I'm going wrong?

Halverson answered 16/4, 2015 at 15:14 Comment(0)
D
1

you just have issue in your model function foreach loop just replace that with:

$query = $this->db->query('SELECT id, date, text FROM news');
$result = $query->result_array();
return $result;
Dentilabial answered 16/4, 2015 at 17:17 Comment(1)
Thank you, with a little tweak to the view file to change the call from <?php echo $news->date; ?> to <?php echo $news['date']; ?> this worked perfectly!Halverson
L
3

In your controller you're correctly assigning the result of the model to a variable:

$data['news'] = $this->testm->get_news();

but in your model you're ECHOING the result, not RETURNING them. SO $data['news'] is null (since the model's method doesn't return anything), and you can't iterate over null of course -> and that's the error in your view. Change your model to return an array, for example:

Public function get_news()
{
    $query = $this->db->query('SELECT id, date, text FROM news');
    return $query->result_array();
}

the text from my database does appear at the top of the page

because, indeed, you're echoing it (as pointed out above) before the views are outputted, that's why you see them before the rendered html

Leopoldeen answered 16/4, 2015 at 15:25 Comment(1)
Not me, I up voted you as I found you explanation very useful. The only reason I didn't mark yours as the correct answer is that Imran answered first and his answer while not including as much detail, did answer my question. No offense meant.Halverson
C
3

please try this:

in your view page:

<?php foreach($news->result() as $new) { ?>
        <article class="article_text">
            <p class="segment_headding"><?php echo $new->date; ?></p>
                <?php echo $new->text; ?>
        </article>
    <?php } ?>
Corpus answered 17/4, 2015 at 9:2 Comment(0)
B
1
        <?php foreach($news as $new) { ?>
            <article class="article_text">
                <p class="segment_headding"><?php echo $new->date; ?></p>
                    <?php echo $new->text; ?>
            </article>
        <?php } ?>

Try foreach($news as $new) instead of foreach($news as $news).

Blasphemous answered 16/4, 2015 at 16:4 Comment(0)
D
1

you just have issue in your model function foreach loop just replace that with:

$query = $this->db->query('SELECT id, date, text FROM news');
$result = $query->result_array();
return $result;
Dentilabial answered 16/4, 2015 at 17:17 Comment(1)
Thank you, with a little tweak to the view file to change the call from <?php echo $news->date; ?> to <?php echo $news['date']; ?> this worked perfectly!Halverson
D
1

use

foreach($thing as $something): //it is important if not compulsory to use a different name for variables!!
some HTML
endforeach;

instead

Diabolo answered 17/4, 2015 at 4:32 Comment(0)
D
1

Try this for your answer:

your controller:

public function index($page = 'testv')
{
        if ( ! file_exists(APPPATH.'/views/'.$page.'.php'))
        {
            show_404();
        }
            $this->load->model('testm');
            $data['news'] = $this->testm->get_news();

            $this->load->view('headder');
            $this->load->view($page, $data);
            $this->load->view('footer');
}

model function :

Public function get_news()
{
    $query = $this->db->query('SELECT id, date, text FROM news');
    return $query;
}

in view file :

<?php foreach($news->result() as $new) { ?>
    <article class="article_text">
        <p class="segment_headding"><?php echo $new->date; ?></p>
            <?php echo $new->text; ?>
    </article>
<?php } ?>
Dandiprat answered 17/4, 2015 at 9:59 Comment(0)
B
0

you can use HTML compatible PHP version. that will help customize HTML code with PHP

<?php echo $var; ?> is equal to <?= $var ?>

<?php foreach($allNews as $news): ?>
    <article class="article_text">
        <p class="segment_headding"><?= $news->date ?></p>
        <?= $news->text ?>
    </article>
<?php endforeach; ?>
Buenrostro answered 19/3, 2020 at 7:31 Comment(0)
T
0
<select class="form-control" id="role" name="role">
                            <?php foreach ($roles as $role) : ?>
                                <option value="<?= $role->id ?>"><?= $role->name ?></option>
                            <?php endforeach; ?>
                        </select>
Testis answered 22/2 at 10:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.