Joomla PHP module - mod_php
|
|
Page 2 of 2
mod_php copy and paste codes:
You can just copy and paste these codes into mod_php.
Example Snippet: Show a welcome Message to users:
<?php
/**
* Custom code for mod_php.
* copyright: www.fijiwebdesign.com
* Shows a welcome to the logged in user, or vistor.
*/
global $my;
if ($my->id) {
echo 'Hello, '.$my->username;
} else {
echo 'Hello, visitor';
}
?>
Example Snippet: Display Flickr Photos
<?php
/**
* Example of display flickr photos for a tags list
* Requires: allow_url_fopen enabled in php.ini
*/
/**
* Configurations
*/
// the tags to get photos for
$tags = 'fiji';
// number of pics to show
$total = 5;
// main template
$main_template = '
<!-- photos for: '.$tags.' -->
<div class="flickr_photos">
{photo_template}
</div>
';
// the html template for each photo item/row
$photo_template = '
<div class="flickr_photo">
<img src="{img_src}" width="{img_width}" height="{img_height}" alt="{img_alt}" />
<h4>
<a href="{url}" target="_blank">{title}</a>
</h4>
</div>
';
// css for our photos styling
$css = '
<style type="text/css">
.flickr_photo {
background: #cfcfcf;
text-align: center;
}
.flickr_photo img {
border: 1px solid #000;
}
.flickr_photo h3 {
font-size: 100%;
font-weight: bold;
margin: 0px;
padding: 5px;
}
</style>
';
/**
* End Configurations
* Start Flickr Feed Request and display
*/
$flickr_feed_url = 'http://api.flickr.com/services/feeds/photos_public.gne?tags='.rawurlencode($tags).'&format=php';
include($flickr_feed_url);
$photos_html = '';
if (isset($feed['items']) && ($count = count($feed['items']))) {
$n = $count < $total ? $count : $total;
for($i = 0; $i < $n; $i++) {
$item = $feed['items'][$i];
$row_html = $photo_template;
$repl = $item;
// flickr image is in description, so we have to parse it out
preg_match("/<img src=\"(.*?)\" width=\"(.*?)\" height=\"(.*?)\" alt=\"(.*?)\" (.*?)\/>/ui", $item['description'], $matches);
if ($matches) {
$repl['img_html'] = $matches[0];
$repl['img_src'] = $matches[1];
$repl['img_width'] = $matches[2];
$repl['img_height'] = $matches[3];
$repl['img_alt'] = $matches[4];
}
foreach($repl as $name=>$value) {
$row_html = str_replace('{'.$name.'}', $value, $row_html);
}
$photos_html .= $row_html;
}
} else {
echo 'No photos found for, '.htmlentities($tags);
}
$html = str_replace('{photo_template}', $photos_html, $main_template);
// display
echo $css;
echo $html;
// debug
function dump($obj) {
echo nl2br(str_replace(' ', ' ', htmlentities(print_r($obj, true))));
}
?>
|
|