Skip to content

Instantly share code, notes, and snippets.

@tmbritton
Created October 18, 2012 19:25
Show Gist options
  • Save tmbritton/3914241 to your computer and use it in GitHub Desktop.
Save tmbritton/3914241 to your computer and use it in GitHub Desktop.
Parse response from Food52
function food52_parse_question_array($payload, $header) {
$results = drupal_json_decode($payload); // api returns double-encoded json
$out = '<h2>' . $header . '</h2>';
// We then iterate through the array to build questions
foreach ($results as $i) {
$question = drupal_json_decode($i);
// Title
$linkurl = $_SERVER['SERVER_NAME'] . '/hotline/';
$linkoptions = array(
'attributes' => array(
'title' => ''
),
'query' => array(
'view' => 'question',
'id' => $question['pickle_question']['id']
),
);
// Last Answer Posted
if ($question['pickle_question']['answers'][0]['nice_created_at']) {
$linkoptions['attributes']['title'] = t("Last answered " . $question['pickle_question']['answers'][0]['nice_created_at'] . " ago");
}
$out .= '<div class="body">';
$out .= '<h3>' . l(t($question['pickle_question']['title']), 'hotline/', $linkoptions) . '</h3>';
// Author and Post Date
$upage = "http://www.food52.com/users/" . $question['pickle_question']['user']['id'] . "_" . $question['pickle_question']['user']['login'];
$out .= "<p class=\"asked\">asked by " . l($question['pickle_question']['user']['login'], $upage) . " " . $question['pickle_question']['nice_created_at'] . " ago</p>";
// Last Answer Posted
//if ($question['pickle_question']['answers'][0]['nice_created_at']) {
// $out .= "<p>Last answered " . $question['pickle_question']['answers'][0]['nice_created_at'] . " ago</p>";
//}
// Tags -- are we linkifying??
if (!empty($question['pickle_question']['tags'])) {
$tnum = 0;
$out .= "<p class=\"tagged\">Tagged: ";
foreach ($question['pickle_question']['tags'] as $i) {
$linkoptions = array(
'attributes' => array(
'title' => ''
),
'query' => array(
'view' => 'tag',
'id' => urlencode($question['pickle_question']['tags'][$tnum]['name'])
),
);
$tags[$tnum] = l($question['pickle_question']['tags'][$tnum]['name'], 'hotline/', $linkoptions);
$tnum++;
}
$out .= implode(", ", $tags) . "</p>";
unset($tags);
}
// Answer Count
$out .= '</div>'; //div.body
$out .= '<div class="counts">';
$out .= "<p>" . $question['pickle_question']['answers_count'] . "<br>Answers</p>";
// Views Count
$out .= "<p>" . $question['pickle_question']['views'] . "<br>Views</p>";
$out .= '</div>';
$out .= "<hr class=\"clear\" />";
}
return $out;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment