We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Creating a loop to load an array

Development and Programming

rchampagne's avatar
rchampagne
212 posts
15 years ago
rchampagne's avatar rchampagne

OK, I am totally stumped. I’ve created 2 arrays with entry information. Now I am trying to create some type of loop that is an array, which has nested array information. I really don’t know what to do.

Maybe one of you more experienced PHP programmers can lend me some insight?

Here is what I have so far…

<?php $ms_title_array = array(); ?>
<?php $ms_start_date = array(); ?>
<?php $ms_id = array(); ?>
<?php $ms_url_array = array(); ?>

    {exp:weblog:entries  weblog="projects" entry_id="1210" }
        {reverse_related_entries weblog="milestones" status="open|pending|closed|complete|Complete|Pending"}
            <?php $ms_title_array[] = "{title}"; ?>
            <?php $ms_start_date[] = "{ms_start_date format="%Y-%m-%d"}"; ?>
            <?php $ms_id[] = "{entry_id}"; ?>
        {/reverse_related_entries}
        <?php $ms_url_array[] = "{path=projects/milestones}{entry_id}"; ?>
    {/exp:weblog:entries}

<?php
    echo json_encode(array(
            array(
                
                'id' => "$ms_id[0]",
                'title' => "$ms_title_array[0]",
                'start' => "$ms_start_date[0]",
                'url' => "$ms_url_array[0]",
            )
    ));
?>

I can get this to spit out a single array value no problem, but I’m stumped as to how I can automate populating that second array.

Should I separate the json_encode function and do that at a later point?

Many thanks,

       
rchampagne's avatar
rchampagne
212 posts
15 years ago
rchampagne's avatar rchampagne

OK, I’m digging into this hardcore and I got to this,

<?php $ms_title_array = array(); ?>
<?php $ms_start_date = array(); ?>
<?php $ms_id = array(); ?>

    {exp:weblog:entries  weblog="projects" entry_id="1210" }
        {reverse_related_entries weblog="milestones" status="open|pending|closed|complete|Complete|Pending"}
            <?php $ms_title_array[] = "{title}"; ?>
            <?php $ms_start_date[] = "{ms_start_date format="%Y-%m-%d"}"; ?>
            <?php $ms_id[] = "{entry_id}"; ?>
        {/reverse_related_entries}
        <?php $ms_url = "{path=projects/milestones}{entry_id}"; ?>
    {/exp:weblog:entries}

<?php
    $array_size =  sizeof($ms_id);
    $i = 0;
    while ($i < $array_size) {
        $event = array(array(
            'id' => "$ms_id[$i]",
            'title' => "$ms_title_array[$i]",
            'start' => "$ms_start_date[$i]",
            'url' => "$ms_url",
        ),);        
        $i++;        
        echo json_encode($event);    
    }    
?>

Which outputs below

[{"id":"1231","title":"Milestone 2","start":"2010-09-27","url":"http://example.com/index.php/projects\/milestones/1210"}]
[{"id":"1232","title":"past due milestone","start":"2010-09-13","url":"http://example.com/index.php/projects\/milestones/1210"}]
[{"id":"1230","title":"test milestone","start":"2010-09-20","url":"http://example.com/index.php/projects\/milestones/1210"}]

BUT, what I need, is to have each one of the events in the curly braces, be in a single echoed JSON format, separated by commas.

so What I am trying to accomplish is this

[{"id":"1231","title":"Milestone 2","start":"2010-09-27","url":"http://example.com/index.php/projects\/milestones/1210"},
{"id":"1232","title":"past due milestone","start":"2010-09-13","url":"http://example.com/index.php/projects\/milestones/1210"},
{"id":"1230","title":"test milestone","start":"2010-09-20","url":"http://example.com/index.php/projects\/milestones/1210"}]

Does anyone know if this is possible? I think I am very close, but I’m not sure where to go… 😕

       
rchampagne's avatar
rchampagne
212 posts
15 years ago
rchampagne's avatar rchampagne

Got it! Guess I just needed to message myself 😛

Here is the code in case someone is trying to do something similar to me. Everyone else on here is always so helpful. The least I can do is share the little bit of experience I have 😊

<?php $ms_title_array = array(); ?>
<?php $ms_start_date = array(); ?>
<?php $ms_id = array(); ?>

    {exp:weblog:entries  weblog="projects" entry_id="1210" }
        {reverse_related_entries weblog="milestones" status="open|pending|closed|complete|Complete|Pending"}
            <?php $ms_title_array[] = "{title}"; ?>
            <?php $ms_start_date[] = "{ms_start_date format="%Y-%m-%d"}"; ?>
            <?php $ms_id[] = "{entry_id}"; ?>
        {/reverse_related_entries}
        <?php $ms_url = "{path=projects/milestones}{entry_id}"; ?>
    {/exp:weblog:entries}

<?php
    $array_size =  sizeof($ms_id);
    $i = 0;
    $event = array();
    while ($i < $array_size) {
        $event[] = 
            array(
                'id' => "$ms_id[$i]",
                'title' => "$ms_title_array[$i]",
                'start' => "$ms_start_date[$i]",
                'url' => "$ms_url",
            );        
        $i++;
    }
    echo json_encode($event);
    
?>
       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.