Page 1 of 1

Loop through uknown number of array items with json_encod

Posted: Tue Jun 23, 2009 3:48 pm
by elicash33
I've got this code that works, but need to make a tweak so that instead of a set number of events (9), the number is determined by the number of events in the RSS feed. (See lines 37+)

Code: Select all

<?php
 
    $year = date('Y');
    $month = date('m');
    $day = date("j");
    $counter = 0;
 
$doc = new DOMDocument();
$doc->load('http://redacted.com/page/event/search_results?mime=text/xml&format=commons_rss&wrap=no&db_alias=cp_read&radius_unit=miles&date_start=' . $year . '-' . $month . '-01+15%3A57%3A01&sql_order=event.start_day+%2Cevent.start_time+&order[0]=day&event_count=210&event_attendee_count=1240&tableName=reference.ref_zip_details&tablePrimary=ref_zip_details_id&tableFields[0]=city&tableFields[1]=state_cd&tableFields[2]=state_name&tableFields[3]=zip&tableFields[4]=zpre&tableFields[5]=area_code&tableFields[6]=county_fips&tableFields[7]=county_name&tableFields[8]=is_preferred&tableFields[9]=time_zone&tableFields[10]=has_dst&tableFields[11]=latitude&tableFields[12]=longitude&tableFields[13]=msa&tableFields[14]=pmsa&tableFields[15]=city_abbreviation&tableFields[16]=market_area&tableFields[17]=zip_type&tableFields[18]=cong_dist&tableFields[19]=cong_dist_text&tableFields[20]=cd_split&tableFields[21]=cd_percent');
$arrFeeds = array();
 
foreach ($doc->getElementsByTagName('item') as $node) { 
 
$counter = $counter + 1;
 
$itemRSS[$counter] = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'dateTime' => $node->getElementsByTagName('dateTime')->item(0)->nodeValue,
'city' => $node->getElementsByTagName('city')->item(0)->nodeValue,
'state' => $node->getElementsByTagName('state')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue
);
 
$event[$counter] = array ( 
'year' => substr($itemRSS[$counter]['dateTime'], 0, 4),
'month' => substr($itemRSS[$counter]['dateTime'], 5, 2),
'day' => substr($itemRSS[$counter]['dateTime'], 8, 2)
);
 
$eventdate[$counter] = $event[$counter]['year'] . "-" . $event[$counter]['month'] . "-" . $event[$counter]['day'];
 
array_push($arrFeeds, $itemRSS);
 
}
 
    echo json_encode(array(
 
        array(
            'id' => 1,
            'title' => $itemRSS[1]['title'],
            'start' => $eventdate[1],
            'url' => $itemRSS[1]['link']
        ),
 
        array(
            'id' => 2,
            'title' => $itemRSS[2]['title'],
            'start' => $eventdate[2],
            'url' => $itemRSS[2]['link']
        ),
        
        array(
            'id' => 3,
            'title' => $itemRSS[3]['title'],
            'start' => $eventdate[3],
            'url' => $itemRSS[3]['link']
        ),
 
        array(
            'id' => 4,
            'title' => $itemRSS[4]['title'],
            'start' => $eventdate[4],
            'url' => $itemRSS[4]['link']
        ),
 
        array(
            'id' => 5,
            'title' => $itemRSS[5]['title'],
            'start' => $eventdate[5],
            'url' => $itemRSS[5]['link']
        ),
 
        array(
            'id' => 6,
            'title' => $itemRSS[6]['title'],
            'start' => $eventdate[6],
            'url' => $itemRSS[6]['link']
        ),
        
        array(
            'id' => 7,
            'title' => $itemRSS[7]['title'],
            'start' => $eventdate[7],
            'url' => $itemRSS[7]['link']
        ),
 
        array(
            'id' => 8,
            'title' => $itemRSS[8]['title'],
            'start' => $eventdate[8],
            'url' => $itemRSS[8]['link']
        ),
 
        array(
            'id' => 9,
            'title' => "Event9",
            'start' => "$year-$month-24",
            'end' => "$year-$month-26",
            'url' => "http://google.com/"
        )
    
    ));
?>
It kinda works. But it will only show 9 events. The number of events in the RSS feed varies, though. So I tried to run a loop, so that I wouldn't have to list each array item. Here's that code, which instead of working results only in the words "loading" (and never loads) (see lines 38+):

Code: Select all

<?php
 
    $year = date('Y');
    $month = date('m');
    $day = date("j");
    $counter = 0;
    $count = 0;
 
$doc = new DOMDocument();
$doc->load('http://redacted.com/page/event/search_results?mime=text/xml&format=commons_rss&wrap=no&db_alias=cp_read&radius_unit=miles&date_start=' . $year . '-' . $month . '-01+15%3A57%3A01&sql_order=event.start_day+%2Cevent.start_time+&order[0]=day&event_count=210&event_attendee_count=1240&tableName=reference.ref_zip_details&tablePrimary=ref_zip_details_id&tableFields[0]=city&tableFields[1]=state_cd&tableFields[2]=state_name&tableFields[3]=zip&tableFields[4]=zpre&tableFields[5]=area_code&tableFields[6]=county_fips&tableFields[7]=county_name&tableFields[8]=is_preferred&tableFields[9]=time_zone&tableFields[10]=has_dst&tableFields[11]=latitude&tableFields[12]=longitude&tableFields[13]=msa&tableFields[14]=pmsa&tableFields[15]=city_abbreviation&tableFields[16]=market_area&tableFields[17]=zip_type&tableFields[18]=cong_dist&tableFields[19]=cong_dist_text&tableFields[20]=cd_split&tableFields[21]=cd_percent');
$arrFeeds = array();
 
foreach ($doc->getElementsByTagName('item') as $node) { 
 
$counter = $counter + 1;
 
$itemRSS[$counter] = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'dateTime' => $node->getElementsByTagName('dateTime')->item(0)->nodeValue,
'city' => $node->getElementsByTagName('city')->item(0)->nodeValue,
'state' => $node->getElementsByTagName('state')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue
);
 
$event[$counter] = array ( 
'year' => substr($itemRSS[$counter]['dateTime'], 0, 4),
'month' => substr($itemRSS[$counter]['dateTime'], 5, 2),
'day' => substr($itemRSS[$counter]['dateTime'], 8, 2)
);
 
$eventdate[$counter] = $event[$counter]['year'] . "-" . $event[$counter]['month'] . "-" . $event[$counter]['day'];
 
array_push($arrFeeds, $itemRSS);
 
}
 
foreach ($doc->getElementsByTagName('item') as $node) { 
 
$count = $count + 1;
 
echo json_encode(array(
 
    array(
        'id' => $count,
        'title' => $itemRSS[$count]['title'],
        'start' => $eventdate[$count],
        'url' => $itemRSS[$count]['link']
    )
));
 
}
 
?>
How do I rewrite the above so that I can loop through all the events with the echo 'json_encode'? Thanks for your help. I've been stuck on this.

Re: Loop through uknown number of array items with json_encod

Posted: Wed Jun 24, 2009 9:52 am
by elicash33
Can anybody help with this?