Skip to content

Instantly share code, notes, and snippets.

@brunokruse
Created January 27, 2014 16:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brunokruse/8651657 to your computer and use it in GitHub Desktop.
Save brunokruse/8651657 to your computer and use it in GitHub Desktop.
get_paragraphs_for_image_id()
// calls for getting a list of paragraphs for an image id from url
// api/images/get_paragraphs_for_image_id/?id=374
public function get_paragraphs_for_image_id() {
global $json_api;
$id = $json_api->query->id;
$paragraphs_list = $this->find_matching_paragraphs($id);
return $paragraphs_list;
}
public function get_chapters_for_image($id) {
$paragraphs_list = $this->find_matching_paragraphs($id);
return $paragraphs_list;
}
// finds the other paragraphs that a particular image id belongs to
public function find_matching_paragraphs($id) {
$chapters_array = array("Exhibition", "Magazine", "Fashion", "Portrait", "Reportage");
// for each paragraph loop through the images and see if there
// is a matching id. if so, add the paragraph name to the array
$master_list = array();
$images_in_paragraph = array();
foreach( $chapters_array as $key=>$value ) {
$images_in_paragraph = $this->return_image_ids_for_paragraph($value);
foreach ($images_in_paragraph as $key1=>$images) {
if(in_array($id, $images)) {
array_push($master_list, $key1);
}
}
}
return $master_list;
}
// returns a list of matches
public function return_image_ids_for_paragraph($name) {
$json_data = array();
$json_data['Info']['last_post_modified'] = get_lastpostmodified('gmt');
// // 5 Chapters in the app
$json_data['Chapters']['Portrait'] = $this->get_chapter_by_name('portrait_paragraph');
$json_data['Chapters']['Reportage'] = $this->get_chapter_by_name('reportage_paragraph');
$json_data['Chapters']['Fashion'] = $this->get_chapter_by_name('fashion_paragraphs');
$json_data['Chapters']['Exhibition'] = $this->get_chapter_by_name('exhibition');
$json_data['Chapters']['Magazine'] = $this->get_chapter_by_name('magazine');
$$paragraphs_list = array();
foreach( $json_data['Chapters'][$name]['Paragraphs'] as $key1=>$value1 ) {
$paragraphs_list[$key1] = $value1;
}
$paragraph_count = count($paragraphs_list);
// create an array of image id's per paragraph
$keys_list = array();
for ($p = 0; $p < $paragraph_count; $p++) {
$paragraph_name = $paragraphs_list[$p]['title'];
$images_list = $paragraphs_list[$p]['images'];
$img_count = count($images_list);
for ($i = 0; $i < $img_count; $i++) {
foreach ($images_list as $key => $value) {
$keys_list[$paragraph_name][$key] = $value['id'];
}
}
}
return $keys_list; // list of image id's in each paragraph
}
@jbobrow
Copy link

jbobrow commented Feb 4, 2014

Looking at this code, I am getting the sense that the timeout is happening because of an infinite loop. I am guessing that this happens at line 46 when the get_chapter_by_name is called, which will then try and return all of its paragraphs with images, not just image ids, and now the images call this function looking for its related paragraphs. I am going to take another look and see if I can straighten this out.

best,
jb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment