Code Sample: PHP

This snippet contains the core of the code that generates the portfolio overview on this site. The code queries the portfolio data from the database, generates the HTML, and prints the HTML to the page. These are broken up into separate files on the website, but are consolidated here to make it easier to follow.

1<?php

2

3class Portfolio extends MGPage {

4    

5    /**

6     * Returns general info (title, link, year launched, etc)

7     * about each portfolio entry

8     *

9     * @author Matthew Gove

10     */

11    private function get_data() {

12        require CONFIG . '/db_connect.php';

13        require SCRIPTS . '/php/user_error_messages.php';

14        $lang = $this->language;

15        /*---------------------------------------------

16         | Define empty arrays to put MySQL data into |

17         ---------------------------------------------*/

18        $title = array();

19        $year_launched = array();

20        $category = array();

21        $overview_link = array();

22        $live_site = array();

23        $code_sample = array();

24        $description = array();

25        

26        /*---------------------------------------------------------

27         | If a $_GET['item'] is defined, add a 'WHERE' clause to |

28         | filter by that criterion.                              |

29         ---------------------------------------------------------*/

30        if (isset($_GET['item'])) $detail_criteria = "AND overview_link = '" . $_GET['item'] . "'";

31        

32        /*----------------------

33         | Run the MySQL Query |

34         ----------------------*/

35        if (!$mysqli) throw new Error ($db_connection_fail[$lang], $e->getMessage(), $e->getCode());

36        $entries = $mysqli->prepare("

37            SELECT portfolio_translations.title, year_launched, portfolio_category.category, overview_link, live_site, code_sample, portfolio_translations.description 

38            FROM portfolio

39            JOIN portfolio_category ON portfolio_category.id = portfolio.category_id

40            JOIN portfolio_translations ON portfolio_translations.portfolio_id = portfolio.id

41            WHERE portfolio_category.language_code = ? AND portfolio_translations.language_code = ? $detail_criteria

42            ORDER BY order_to_display DESC;

43        ");

44        if (!$entries) throw new Error($db_connection_fail[$lang], $mysqli->error, $mysqli->errno);

45        $entries->bind_param('ss', $this->language, $this->language);

46        if (!$entries->execute()) throw new Error('entries error', $entries->error, $entries->errno);

47        $entries->store_result();

48        $entries->bind_result($title_index, $yr_launched, $cat, $link, $site, $code_smpl, $desc);

49        while($entries->fetch()) {

50            $title[] = $title_index;

51            $year_launched[] = $yr_launched;

52            $category[] = $cat;

53            $overview_link[] = $link;

54            $live_site[] = $site;

55            $code_sample[] = $code_smpl;

56            $description[] = $desc;

57        }

58        

59        /*-------------------------------------------------------

60         | If no data returned from database, throw a 404 error |

61         -------------------------------------------------------*/

62        if ($entries->num_rows == 0) @(new HttpError(404))->throw_error();

63        

64        $entries->free_result();

65        $mysqli->close();

66        

67        return array(

68            'title' => $title,

69            'year_launched' => $year_launched,

70            'category' => $category,

71            'overview_link' => $overview_link,

72            'live_site' => $live_site,

73            'code_sample' => $code_sample,

74            'description' => $description

75        );

76    }

77    

78    

79    

80    /**

81     * Prints the overview page for the portfolio. It

82     * includes title, website category, year launched,

83     * and links to a detailed overview and the live site.

84     * @author Matthew Gove

85     */

86    public function print_overview() {

87        $data = $this->get_data();

88        $lang = $this->get_language();

89        

90        $launched_in = array(

91            'fr' => 'Formée en',

92            'en' => 'Launched in'

93        );

94        

95        $lv_site = array(

96            'fr' => 'Voyez le Site',

97            'en' => 'Live Site'

98        );

99        

100        $portfolio = array('fr' => 'Portefeuille', 'en' => 'Portfolio');

101        

102        $proudly_built_with = array(

103            'fr' => 'Fièrement Construis Avec',

104            'en' => 'Proudly Built With'

105        );

106        

107        $view_details = array(

108            'fr' => 'Étude de Cas',

109            'en' => 'Case Study'

110        );

111        

112        $website = array(

113            'fr' => '',

114            'en' => 'Website'

115        );

116        

117        /*-------------------------

118         | Define the HTML output |

119         -------------------------*/

120        $html .= '<h1>' . $portfolio[$this->language] . '</h1>';

121        for ($i = 0; $i < count($data['title']); $i++) {

122            $proglangs = $this->get_proglangs($data['overview_link'][$i]);

123            $programming_languages = $this->make_list($proglangs['proglangs']);

124            $view_more_link = $this->format_link($data['overview_link'][$i], $view_details[$this->language]);

125            $live_site_link = $this->format_link($data['live_site'][$i], $lv_site[$this->language]);

126            

127            $html .= <<<HTML

128                <div class="portfolio-item">

129                    <h2>{$data['title'][$i]}</h2>

130                    <p>{$data['category'][$i]} {$website[$lang]}</p>

131                    <img src="/images/portfolio/{$data['overview_link'][$i]}.png" alt="{$data['title'][$i]}" />

132                    <div class="portfolio-item-info">

133                        <h3>{$proudly_built_with[$this->language]}</h3>

134                        $programming_languages

135                    </div>

136                    <p>{$launched_in[$lang]} {$data['year_launched'][$i]}<p>

137                    <p>$view_more_link <span class="portfolio-spacer"></span> $live_site_link</p>

138                </div>

139HTML;

140        }

141        

142        echo $html;

143    }   # end function print_overview

144    

145}   # end class Portfolio

146

147/*-------------------------------------

148 | The next block of code prints all  |

149 | of the necessary HTML to the page. |

150 -------------------------------------*/

151require_once $_SERVER['DOCUMENT_ROOT'] . '/../config/app_config.php';

152require_once CONFIG . '/db_connect.php';

153require_once CLASSES . '/portfolio.php';

154

155$javascript = '<script src="/scripts/js/portfolio.image.overlay.js"></script>';

156$meta_description = "A professional portfolio showcasing my work in website development.";

157$meta_keywords = "Professional, Portfolio, Web Development, Web Developer, Matt Gove";

158

159$title = array(

160    'fr' => 'Portefeuille',

161    'en' => 'Portfolio'

162);

163

164$body = new Portfolio();

165$nav = new NavMenu($title[$body->language], $javascript);

166

167/*--------------------

168 | BEGIN HTML OUTPUT |

169 --------------------*/

170$nav->page_start();

171

172try {

173    if (isset($_GET['item'])) $body->print_details();

174    else $body->print_overview();

175} catch (Error $e) {    # The Error class modifies the Exception class

176    display_error($were_sorry[$body->language], array($e->user_message, $system_admins_notified[$body->language], $recover_message[$body->language]));

177    $e->log();

178}

179

180$nav->insert_footer();

181

182if (DEBUG_MODE) {

183    $debugger = new Debugger(get_defined_vars(), $mysqli, $e);

184    $debugger->print_debug();

185}

186

187?>