Hey everyone, I've seen a few questions in this forum regarding custom pages that contain lists (and links) of book subjects/genres etc, and not many solutions.
With some tinkering, I've finally got something working, so figured I'd share. The code below is based off the manufacturer list sidebox by Kuroi. It displays the list of genres that are used in your products. It also displays the number of those books etc that contain that genre/subject.
I'm currently using v1.3.9g of Zen Cart and v4 (jph) of Moku's Book mod and it appears to work fine.
To make a list of something else (like Authors..which I might try next) all you have to do (i think) is point the SQL in the code to the relevant database fields.
Hope this helps some people.
Code:
<?php
/**
* genre list page - displays a list of book genres/subjects so the customer can choose to filter on books with those genres only
* edited by Tim Ryan based on the manufacturer list sidebox by Kuroi
* @package templateSystem
* @copyright Portions Copyright 2007-2009 Kuroi Web Design
* @copyright Portions Copyright 2003-2006 Zen Cart Development Team
* @copyright Portions Copyright 2003 osCommerce
* @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
*/
// set to true to display a count of the number of products for each genre
define('DISPLAY_GENRES_COUNTS', false);
// debug switch
define('DEBUG_GENRES_SIDEBOX', false);
// test if genres should show
$show_genres = true;
if (DEBUG_GENRES_SIDEBOX) {
echo "Entering genres module ...<br/>\n";
echo '- Images: ' . (DISPLAY_GENRES_IMAGES ? 'TRUE' : 'FALSE') . "<br/>\n";
echo '- Man. Status: ' . PRODUCTS_GENRES_STATUS . "<br/>\n";
echo '- Counts: ' . (DISPLAY_GENRES_COUNTS ? 'TRUE' : 'FALSE') . "<br/>\n";
echo '- Show: ' . ($show_genres ? 'TRUE' : 'FALSE') . "<br/><br/>\n";
}
if ($show_genres) {
// pull counts from the database
$genre_counts_query = "SELECT book_genre_id, COUNT( products_id ) AS number FROM " . TABLE_BOOKS_TO_GENRES . " WHERE book_genre_id IS NOT NULL AND book_genre_id > 0 GROUP BY book_genre_id";
$genre_counts_result = $db->Execute($genre_counts_query);
// prepare counts for use later
$genre_counts_list = array();
while (!$genre_counts_result->EOF) {
$genre_counts_list[$genre_counts_result->fields['book_genre_id']] = $genre_counts_result->fields['number'];
$genre_counts_result->MoveNext();
}
if (DEBUG_GENRES_SIDEBOX) {
echo '<h4>List of Counts</h4>';
echo '<pre>' . "\n";
var_dump($genre_counts_list);
echo '</pre>' . "\n";
}
// now let's get the genres
$genre_sidebox_query = "SELECT g.book_genre_id, gd.book_genre_name
FROM " . TABLE_BOOK_GENRE . " g, " . TABLE_BOOK_GENRE_DESCRIPTION . " gd
WHERE g.book_genre_id = gd.book_genre_id
ORDER BY gd.book_genre_name";
$genre_sidebox_result = $db->Execute($genre_sidebox_query);
if ($genre_sidebox_result->RecordCount() > 0) {
$genre_sidebox_list = array();
while (!$genre_sidebox_result->EOF) {
// prepare data
$genre_id = $genre_sidebox_result->fields['book_genre_id'];
$genre_name = $genre_sidebox_result->fields['book_genre_name'];
$genre_count = isset($genre_counts_list[$genre_id]) ? (int)$genre_counts_list[$genre_id] : 0;
if (DEBUG_GENRES_SIDEBOX) {
echo 'Processing ' . $genre_name . "<br/>\n";
}
// add to list only if we are showing all genres, or this one has products
if ($genre_count > 0) {
$genre_sidebox_list[] = array(
'id' => $genre_id,
'text' => $genre_name,
'count' => $genre_count);
}
// advance to next result
$genre_sidebox_result->MoveNext();
}
if (DEBUG_GENRES_SIDEBOX) {
echo '<br/><h4>Final List</h4>';
echo '<pre>' . "\n";
var_dump($genre_sidebox_list);
echo '</pre>' . "\n";
}
if (sizeof($genre_sidebox_list) > 0 ) {
$content = '';
$content .= '<div id="' . str_replace('_', '-', $box_id . 'Content') . '" class="">' . "\n";
$content .= '<ul>' . "\n";
foreach ($genre_sidebox_list as $genre) {
if ($genre['text']!=''){
$content .= '<li><a class="manufacturerName" href="' . zen_href_link(FILENAME_DEFAULT, 'typefilter=book_genre&book_genre_id=' . $genre['id']) . '">';
$content .= $genre['text'];
$content .= '</a>';
$content .= ' (' . $genre['count'] . ')';
$content .= '</li>' . "\n";
}
}
$content .= "</ul>\n</div>\n";
echo $content;
}
}
}
Bookmarks