Ok - here’s the snippet. This is meant to be called multiple times on a page.
<?php
// load necessary modules
$modx->runSnippet('load_mootools');
$modx->runSnippet('load_squeezebox');
$output = '';
// generate paths
$assets = $modx->getOption('base_url').'assets/';
$pl_url = $assets.'photolab/';
$pl_path = $modx->getOption('base_path').'assets/photolab/';
// include image code
// (moved to external file for easier editing)
require($pl_path.'processors/image.inc.php');
return $output;
?>
And here is the code from the file (image.inc.php)...
<?php
// include necessary functions & libraries
require_once($pl_path.'config.inc.php');
require_once($pl_path.'functions/filereader.func.php');
require_once($pl_path.'functions/photopath.func.php');
//#############################################################################################
// DATEBASE QUERIES
//#############################################################################################
if ( isset($serial) ) { //<--- make sure there's something to look up
// connect to the database
try {
$dbh = new PDO("mysql:host=$SQL_HOST;dbname=$SQL_DB", $SQL_USER, $SQL_PASS, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8', PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => false));
} catch (PDOException $e) {}
// CREATE QUERY TO PULL IMAGE INFO
// --------------------------------------------------------------
// selects
$sql = 'SELECT img.image_serial, eve.event_name ';
// search main table
$sql.= 'FROM pl_images AS img ';
// join event table
$sql.= 'LEFT JOIN pl_events AS eve ON img.image_event = eve.event_id ';
// where clauses
$sql.= 'WHERE img.image_serial=? ';
$sql.= 'AND img.image_active=1 ';
// limit (just in case!)
$sql.= 'LIMIT 1 ';
// RUN QUERY
// --------------------------------------------------------------
$stmt = $dbh->prepare($sql);
$stmt->bindParam(1, $serial, PDO::PARAM_STR);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
// close database connection;
$dbh = NULL;
}
//#############################################################################################
// TEST AND SET OPTIONAL PARAMETERS
//#############################################################################################
// test for width setting - default is 250
$myWidth = (isset($width)) ? (int)$width : 250;
// minimum width is 75
$myWidth = ($myWidth < 75) ? 75 : $myWidth;
// maximum width is 736
$myWidth = ($myWidth > 736) ? 736 : $myWidth;
// select image type based on image size
if ($myWidth == 75) {
$type = 'thumb';
} elseif ($myWidth <= 300) {
$type = 'web';
} else {
$type = 'small';
}
// test for float setting - default is left
if (isset($float)) {
$myFloat = ($float=='left' || $float=='') ? 'float_left' : ($float=='right') ? 'float_right' : '';
} else {
$myFloat = 'float_left';
}
// generate width string
$widthString = 'width: '.$myWidth.'px;';
//#############################################################################################
// GENERATE DETAIL PAGE
//#############################################################################################
if ($row['image_serial'] != NULL) { //<--- for non-empty result
// generate image URL
$dispUrl = photoPath($row['image_serial'], $type, $pl_url);
// generate link to detail popup
$vars = 'serial='.$serial;
$detailUrl = $modx->makeUrl($DETAIL_PAGE, '', $vars);
// read in the image link template
$image_tpl = get_file_contents($pl_path.'templates/image.tpl.html');
// inject content
$placeholders = array('[[+pli_img]]', '[[+pli_link]]', '[[+pli_title]]', '[[+pli_class]]', '[[+pli_style]]', '[[+pli_caption]]');
$values = array($dispUrl, $detailUrl, $row['event_name'], $myFloat, $widthString, $caption);
$output = str_replace($placeholders, $values, $image_tpl);
} else { //<-- for empty result
$output = '';
}
if ($debug) {
$output .= '<div style="background: #a00; color: #fff; padding: 20px;">';
$output .= "Serial: <strong>$serial</strong><br/>";
$output .= "SQL: <strong>$sql</strong><br/>";
$output .= "Result: <strong>".serialize($row)."</strong><br />";
$output .= "Float In: <strong>$float</strong><br/>";
$output .= "Width In: <strong>$width</strong><br/>";
$output .= "Caption In: <strong>$caption</strong><br/>";
$output .= "Type: <strong>$type</strong><br/>";
$output .= "URL: <strong>$dispUrl</strong><br/>";
$output .= "Detail: <strong>$detailUrl</strong><br/>";
$output .= "Float Out: <strong>$myFloat</strong><br/>";
$output .= "Width Out: <strong>$myWidth</strong><br/>";
$output .= "Width String: <strong>$widthString</strong>";
$output .= '</div>';
}
?>
As stated above. The first image works fine. Here’s an example of the debug dump from subsequent images:
Serial: c6696-36a
SQL: SELECT img.image_serial, eve.event_name FROM pl_images AS img LEFT JOIN pl_events AS eve ON img.image_event = eve.event_id WHERE img.image_serial=? AND img.image_active=1 LIMIT 1
Result: b:0;
Float In: left
Width In: 100
Caption In: TESTING 3
Type: web
URL:
Detail:
Float Out: float_right
Width Out: 100
Width String: width: 100px;
From the above input, it seems that the call to the database is what’s breaking...
Thanks for taking a look at it!