Welcome Guest, Not a member yet? Register   Sign In
foreach loop in a foreach loop
#5

Sorry for the late reply. It looks like the forum stopped sending email notifications, again! Sad

I would do something like that:

PHP Code:
$data['categories'] = $this->books_model->get_categories();

foreach (
$data['categories'] as &$cat) {
    $cat['books'] = $this->books_model->get_books($cat['id']);


In the view you can loop like that:
PHP Code:
foreach ($categories as $cat) {
    echo $cat['name'] . "<br>";

    foreach ($cat['books'] as $book) {
        echo $book['title']. "<br>";
    }

CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply


Messages In This Thread
foreach loop in a foreach loop - by DirkTeur - 03-26-2020, 04:46 AM
RE: foreach loop in a foreach loop - by DirkTeur - 03-27-2020, 03:35 PM
RE: foreach loop in a foreach loop - by DirkTeur - 03-28-2020, 11:37 AM
RE: foreach loop in a foreach loop - by includebeer - 03-30-2020, 01:58 PM
RE: foreach loop in a foreach loop - by DirkTeur - 04-15-2020, 10:41 AM



Theme © iAndrew 2016 - Forum software by © MyBB