Ever wanted to make your gallery a little more dynamic?
Now you can sort your photos by year & category. Sort settings stick between page loads. Uses the Query Tag.
-based on:
http://ellislab.com/forums/viewthread/38371/
&
http://ellislab.com/forums/viewthread/43918/
<?php
# Start the session:
session_start();
//////// class SortResults:
class SortResults {
# Create your own array… Just change the name in the call.
var $year_arr = array(
'' => 'All',
'2006' => '2006',
'2007' => '2007',
);
var $category_arr = array(
'' => 'All',
'2' => 'Art / Interactive',
'3' => 'People',
'4' => 'Camps',
'6' => 'Dust Storms',
'5' => 'Dusk & Dawn',
'7' => 'Exodus',
'8' => 'The Man',
'9' => 'The Temple',
);
var $sort_arr = array(
'ASC' => 'Ascending',
'DESC' => 'Descending'
);
var $limit_arr = array(
'5' => '5',
'10' => '10',
'20' => '20',
'30' => '30',
'40' => '40',
'50' => '50',
'100' => '100'
);
function create_options($s, $a) {
$the_return = '';
foreach($this->$a as $key => $value) {
if($key == $s) {
$the_return .= sprintf('<option value="%s" selected="selected">%s</option>'."\n", $key, $value);
} else {
$the_return .= sprintf('<option value="%s">%s</option>'."\n", $key, $value);
}
}
return $the_return;
}
}
function processForms($array){ print_r($array); }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Script for Sticky sorting of Gallery entries</title>
</head>
<body>
<?php
if((!isset($_POST['submit'])) && (isset($_SESSION['sort']))) {
$sticky = unserialize($_SESSION['sort']);
//processForms($sticky);
$posted['category_id'] = $sticky['category_id'];
$posted['sort'] = $sticky['sort'];
$posted['limit'] = $sticky['limit'];
$posted['year'] = $sticky['year'];
} else {
# Determine $_POST vars:
$posted['category_id'] = (isset($_POST['category_id'])) ? $_POST['category_id'] : '';
$posted['sort'] = (isset($_POST['sort'])) ? $_POST['sort'] : '';
$posted['limit'] = (isset($_POST['limit'])) ? $_POST['limit'] : '';
$posted['year'] = (isset($_POST['year'])) ? $_POST['year'] : '';
//processForms($posted);
$_SESSION['sort'] = serialize($posted);
}
# Create the object:
$form_items = new SortResults();
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>">
<select name="category_id">
<option value="">View by Category:</option>
<?php print($form_items->create_options($posted['category_id'], 'category_arr')); ?>
</select>
<select name="year">
<option value="">Year:</option>
<?php print($form_items->create_options($posted['year'], 'year_arr')); ?>
</select>
<select name="sort">
<option value="">Order In:</option>
<?php print($form_items->create_options($posted['sort'], 'sort_arr')); ?>
</select>
<select name="limit">
<option value="">Result Limit:</option>
<?php print($form_items->create_options($posted['limit'], 'limit_arr')); ?>
</select>
<input name="submit" type="submit" value="Go!" />
</form>
<hr >
<!-- {category} is my custom field. -->
<!-- If custom field not selected, remove option. -->
<!-- sort="" and limit="" get default values of "asc" and "5"... See below. -->
{exp:query
limit="<?php echo ($posted['limit']) ? $posted['limit'] : '5'; ?>" paginate="top" sql="
SELECT * FROM exp_gallery_entries, exp_gallery_categories WHERE exp_gallery_entries.cat_id = exp_gallery_categories.cat_id
<?php if($posted["category_id"]) echo "AND exp_gallery_entries.cat_id = '".$posted['category_id']."'"; ?>
<?php if($posted["year"]) echo "AND year( from_unixtime( exp_gallery_entries.entry_date ) ) = '".$posted['year']."'"; ?>
ORDER BY entry_date <?php echo ($posted['sort']) ? $posted['sort'] : 'DESC'; ?>
"}
{if no_results}Sorry, no results.{/if}
<span>
<a href="/photos/view/{entry_id}" class="gallery_img_select">_/files/photo_gallery/{cat_folder}/{filename}{extension}_</a><br>
{title}<br>
{entry_date format="%m-%d-%Y"} </span>
{paginate}
Page {current_page} of {total_pages} pages {pagination_links}
{/paginate}
{/exp:query}
</body>
</html>