Created
August 23, 2013 14:05
-
-
Save sta1r/6319675 to your computer and use it in GitHub Desktop.
.. with array and initialization.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!-- A-Z Class --> | |
| <?php | |
| // check whether class exists | |
| if(class_exists('AZ') != true) | |
| { | |
| class AZ { | |
| public $array; // profiles content array | |
| /** | |
| * [__construct description] | |
| * | |
| * @param array $array [array of content] | |
| */ | |
| public function __construct( array $array = array() ) { | |
| $this->array = $array; | |
| } | |
| /** | |
| * [__destruct destroy array after completion] | |
| */ | |
| public function __destruct() { | |
| unset( $this->array); | |
| } | |
| /** | |
| * [split_name - extract the Surname from title field] | |
| */ | |
| public function split_name($name, $bit) { | |
| $pos = strrpos($name, ' '); | |
| if ($pos === false) { | |
| $surname = $name; | |
| } | |
| $firstname = substr($name, 0, $pos + 1); | |
| $surname = substr($name, $pos); | |
| if ($bit == 'first') { | |
| return trim($firstname); | |
| } else { | |
| return trim($surname); | |
| } | |
| } | |
| /** | |
| * [alpha_sort_array - sort our array by a custom orderby parameter] | |
| */ | |
| public function alpha_sort_array($array, $orderby) { | |
| $sortArray = array(); | |
| foreach($array as $item){ | |
| foreach($item as $key=>$value){ | |
| if(!isset($sortArray[$key])){ | |
| $sortArray[$key] = array(); | |
| } | |
| $sortArray[$key][] = $value; | |
| } | |
| } | |
| array_multisort($sortArray[$orderby],SORT_ASC,$array); | |
| return $array; | |
| } | |
| /** | |
| * [enhance_arrays - push additional values into our existing $this->array] | |
| */ | |
| public function enhance_arrays() { | |
| $enhancedArray = array(); | |
| $enhancedItemArray = array(); | |
| foreach($this->array as $item){ | |
| $surname = $this->split_name($item['title'], 'first'); | |
| foreach($item as $key=>$value){ | |
| $enhancedItemArray[$key] = $value; | |
| $enhancedItemArray['surname'] = $surname; | |
| $enhancedItemArray['letter'] = $surname[0]; | |
| } | |
| $enhancedArray[] = $enhancedItemArray; | |
| } | |
| return $enhancedArray; | |
| } | |
| public function do_item_loop($item) { | |
| ?> | |
| <li class="row"> | |
| <figure> | |
| <a href="<?php echo $item['section_link']; ?>" title=""><img src="http://placehold.it/300x300&text=THUMBNAIL" alt="Image Alt"></a> | |
| </figure> | |
| <div class="text"> | |
| <h3 class="size-h5"><a href="<?php echo $item['section_link']; ?>" title=""><?php echo $item['title']; ?></a></h3> | |
| <?php if ($item['teaser'] != '') { echo "<p>" . $item['teaser'] . "</p>"; } ?> | |
| </div> | |
| </li> | |
| <?php | |
| } | |
| /** | |
| * [do_output output the content for the blocks] | |
| */ | |
| public function do_output() { | |
| $enhanced = $this->enhance_arrays(); | |
| $sorted = $this->alpha_sort_array($enhanced, 'surname'); | |
| ?> | |
| <div class="row"> | |
| <?php | |
| $temp_letter = ''; | |
| $counter = 1; | |
| $item_count = sizeof($sorted); | |
| foreach ($sorted as $item) { | |
| if ( $item['letter'] != $temp_letter ) { | |
| if ($counter > 1) { ?> | |
| </ul> | |
| </div><!-- .l-content --> | |
| </div><!-- .row --> | |
| <?php } ?> | |
| <div class="row az-group" data-group="<?php echo $item['letter']; ?>"> | |
| <h2 class="az-letter"><?php echo $item['letter']; ?></h2> | |
| <div class="image-list-with-text-content l-content"> | |
| <ul> | |
| <?php } | |
| $this->do_item_loop($item); | |
| if ($counter == $item_count) { ?> | |
| </ul> | |
| </div><!-- .l-content --> | |
| </div><!-- .row --> | |
| <?php | |
| } | |
| $temp_letter = $item['letter']; | |
| $counter++; | |
| } // end foreach ?> | |
| </div><!-- .row --> | |
| <?php | |
| } | |
| } // AZ Class declaration | |
| } // end check if class_exists | |
| ?> | |
| <?php | |
| $profile_array[] = array( | |
| "title" => "Title Jones", | |
| "section_link" => "http://www.google.com", | |
| "teaser" => "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce nulla turpis, ullamcorper congue pharetra sit amet, varius sit amet diam. Sed bibendum porttitor mattis. Phasellus nisl mi, gravida in porta nec, hendrerit quis arcu. (max XX words)" | |
| "side" => "right" | |
| ); | |
| $profile_array[] = array( | |
| "title" => "Title 2 Jameson", | |
| "section_link" => "http://www.google.com", | |
| "teaser" => "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce nulla turpis, ullamcorper congue pharetra sit amet, varius sit amet diam. Sed bibendum porttitor mattis. Phasellus nisl mi, gravida in porta nec, hendrerit quis arcu. (max XX words)" | |
| ); | |
| $profile_array[] = array( | |
| "title" => "Another Title Surname", | |
| "section_link" => "http://www.google.com", | |
| "teaser" => "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce nulla turpis, ullamcorper congue pharetra sit amet, varius sit amet diam. Sed bibendum porttitor mattis. Phasellus nisl mi, gravida in porta nec, hendrerit quis arcu. (max XX words)" | |
| ); | |
| $profile_array[] = array( | |
| "title" => "Another Title Long", | |
| "section_link" => "http://www.google.com", | |
| "teaser" => "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce nulla turpis, ullamcorper congue pharetra sit amet, varius sit amet diam. Sed bibendum porttitor mattis. Phasellus nisl mi, gravida in porta nec, hendrerit quis arcu. (max XX words)" | |
| ); | |
| $profile_array[] = array( | |
| "title" => "Another Title Loose", | |
| "section_link" => "http://www.google.com", | |
| "teaser" => "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce nulla turpis, ullamcorper congue pharetra sit amet, varius sit amet diam. Sed bibendum porttitor mattis. Phasellus nisl mi, gravida in porta nec, hendrerit quis arcu. (max XX words)" | |
| ); | |
| // Create new object for blocks | |
| $profiles = new AZ($profile_array); | |
| $profiles->do_output(); | |
| ?> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment