1

In this http://codeigniter.com/user_guide/libraries/parser.html tutorial we have this multidemensional array

$data = array(
          'blog_title'   => 'My Blog Title',
          'blog_heading' => 'My Blog Heading',
          'blog_entries' => array(
                                  array('title' => 'Title 1', 'body' => 'Body 1'),
                                  array('title' => 'Title 2', 'body' => 'Body 2'),
                                  array('title' => 'Title 3', 'body' => 'Body 3'),
                                  array('title' => 'Title 4', 'body' => 'Body 4'),
                                  array('title' => 'Title 5', 'body' => 'Body 5')
                                  )
        );

And we printing him easily like this

{blog_entries}
    <h5>{title}</h5>
    <p>{body}</p>
{/blog_entries}

But how to output array like this one https://i.sstatic.net/WniLT.png :?

 $data = array( 
array(
    'user' => array('id' => "1", 'name' => "Test1"
    ),'title' => 'Title 1', 'body' => 'Body 1'),
array(
    'user' => array('id' => "2", 'name' => "Test2"
    ),'title' => 'Title 2', 'body' => 'Body 2'),
array(
    'user' => array('id' => "3", 'name' => "Test3"
    ),'title' => 'Title 3', 'body' => 'Body 3')
);

If we trying to do something like this

{blog_entries}
    {user}
        {id}
    {/user}
    {title}
    {body}
    <br />
{/blog_entries}

We ll have error:

Message: Invalid argument supplied for foreach()

Ah, found similar problem here Extended Template Parser: CodeIgniter... Issue with Nested Arrays

2 Answers 2

1

This functionality is included in the built-in template class. (I don't know if it existed when you first posted the question, but it is in there now, I use it myself.) The trick is in creating the proper array structure to pass to the parser. I tested this example in a live page just now, works 100% in CI 2.1.3.

As a specific example, consider this code which displays (with minimal formatting) a series of rooms available at a hotel, and shows, per room, the rate(s) per day for each day of your travel. (2 days, in this example.)

{available_rooms}
    <p>{room_type} {total_rate}</p>
    {room_rates}
        <p>${room_rate} {room_date}</p>
    {/room_rates}
    <hr>
{/available_rooms}

With out put similar to:

1 King Bed 119.98
 59.99 2013-03-23
 59.99 2013-03-24

1 King Bed 119.98
 59.99 2013-03-23
 59.99 2013-03-24

2 Double Beds 139.98
 69.99 2013-03-23
 69.99 2013-03-24

2 Double Beds 139.98
 69.99 2013-03-23
 69.99 2013-03-24

This is the array I used successfully in this example.

> available_rooms > 0 > room_type = 1 King Bed 
> available_rooms > 0 > total_rate = 119.98 
> available_rooms > 0 > room_rates > 0 > room_rate = 59.99 
> available_rooms > 0 > room_rates > 0 > room_date = 2013-03-23 
> available_rooms > 0 > room_rates > 1 > room_rate = 59.99 
> available_rooms > 0 > room_rates > 1 > room_date = 2013-03-24 
> available_rooms > 1 > room_type = 1 King Bed 
> available_rooms > 1 > total_rate = 119.98 
> available_rooms > 1 > room_rates > 0 > room_rate = 59.99 
> available_rooms > 1 > room_rates > 0 > room_date = 2013-03-23 
> available_rooms > 1 > room_rates > 1 > room_rate = 59.99 
> available_rooms > 1 > room_rates > 1 > room_date = 2013-03-24 
> available_rooms > 2 > room_type = 2 Double Beds 
> available_rooms > 2 > total_rate = 139.98 
> available_rooms > 2 > room_rates > 0 > room_rate = 69.99 
> available_rooms > 2 > room_rates > 0 > room_date = 2013-03-23 
> available_rooms > 2 > room_rates > 1 > room_rate = 69.99 
> available_rooms > 2 > room_rates > 1 > room_date = 2013-03-24 
> available_rooms > 3 > room_type = 2 Double Beds 
> available_rooms > 3 > total_rate = 139.98 
> available_rooms > 3 > room_rates > 0 > room_rate = 69.99 
> available_rooms > 3 > room_rates > 0 > room_date = 2013-03-23 
> available_rooms > 3 > room_rates > 1 > room_rate = 69.99 
> available_rooms > 3 > room_rates > 1 > room_date = 2013-03-24
Sign up to request clarification or add additional context in comments.

Comments

0

Setup your array more like this

$data = array( 'user' => array('id' => "1", 'name' => "Test1", 'title' => 'Title 1', 'body' => 'Body 1'),
                         array('id' => "2", 'name' => "Test2", 'title' => 'Title 2', 'body' => 'Body 2'),
                         array('id' => "3", 'name' => "Test3", 'title' => 'Title 3', 'body' => 'Body 3')
);

And after that, it's kind of like nesting HTML tags, except with key->value pairs in a PHP (If that make sense).

{user}
    <h4>{name} : {id}</h4>
    <span>{title}</span>
    <p>
        {body}
    </p>
{/user}

Comments

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.