Location: PHPKode > scripts > Particle Gallery > includes/functions_fetch.php
<?php
function fetchalbum($topid, $href = 0){
	global $db, $dbprefix, $config;
	
	$loop = 1;
	$cur  = intval($topid);
	$row  = 0;
	$etc1 = "";
	$etc2 = "";
	
	while($loop == 1){
	
		$sql = "SELECT * FROM " . $dbprefix . "albums WHERE albumid = " . dbSecure($cur);
		$top = $db->execute($sql);
		if ($top->rows < 1){ return false; }
		
		if ($top->fields["parent"] == 0){ $loop = 0; }
		
		// set new variables
		$cur  = $top->fields["parent"];
		$etc2 = d2($top->fields["title"]) . "/" . $etc2;
		
		// add extra code
		if ($row == 0){
			$etc1 = $top->fields["title"];
		} else {
			$etc1 = $top->fields["title"] . ' ' . $config["breadcrumb"] . ' ' . $etc1;
		}
		
		$row++;
	
	}
	
	// post processing
	if ($href == 0){
		$etc = '<a href="' . $config["topicpage"] . $etc2 . '">' . $etc1 . '</a>';
	} else {
		$etc = $etc1;
	}
	
	return $etc;
}

function fetchoptions($parent, $exclude){
	global $db, $dbprefix;
	
	// new style, all topic things
	$loop = 1;
	$cur  = 0;
	$code  = "";
	
	// sub-function for getting lower beings
	function fetchchildalbums($cur, $parent, $exclude){
		global $db, $dbprefix, $config, $code;
		$sql = "SELECT * FROM " . $dbprefix . "albums WHERE parent = " . $cur . " ORDER BY title ASC";
		$erk = $db->execute($sql);
		
		if ($erk->rows > 0){ do {
			
			if ($erk->fields["albumid"] <> $exclude){
			
				// work out textual path
				$nexttitle = fetchalbum($erk->fields["albumid"], 1);
				
				// work out extra code
				if ($erk->fields["albumid"] == $parent){
					$xc = ' selected="selected"';
				} else {
					$xc = '';
				}
				
				// add code to selector box
				$code .= '<option value="' . $erk->fields["albumid"] . '"' . $xc . '>' . $nexttitle . '</option>';
				
				// get children of this one
				$code = fetchchildalbums($erk->fields["albumid"], $parent, $exclude);
			
			}
		
		} while ($erk->loop()); }
		
		// and return
		return $code;
	}
	
	// call the top function
	$code = fetchchildalbums(0, $parent, $exclude);
	
	// and return code
	return $code;
}

// for fetching all album IDs for current and below
function fetchstack($albumid){
	global $db, $dbprefix;
	
	// set up the variables
	$albumid = intval($albumid);
	$row  = 0;
	$cur  = 1;
	$scan = array();
	array_push($scan, $albumid);
	
	// begin the loop thing
	while ($row < $cur){
		$sql = "SELECT * FROM " . $dbprefix . "albums WHERE parent = " . $scan[$row];
		$rec = $db->execute($sql);
		if ($rec->rows > 0){ do {
			
			array_push($scan, $rec->fields["albumid"]);
			
		} while ($rec->loop()); }
		
		$row++;
		$cur = count($scan);
	}
	
	return $scan;
}

// turns a stack array into SQL usable code
function process_stack($stack){
	if (!(is_array($stack))){ return $stack; }
	
	foreach($stack as $e){
		$x .= "|" . $e;
	} $x = substr($x, 1);
	
	return $x;
}

function fetchalbumnum($scan, $albumid = 0){
	global $db, $dbprefix, $usr;
	
	$sql = "SELECT albumid FROM " . $dbprefix . "albums WHERE albumid REGEXP '^(" . $scan . ")$' AND albumid <> " . dbSecure(intval($albumid));
	
	if ($usr->Access < 2){
		$sql .= " AND visible = 1";
	}
	
	$rec = $db->execute($sql);
	$result = $rec->rows;
	$rec->clear();
	
	// and return rows
	return $result;
}

function fetchimagenum($scan){
	global $db, $dbprefix, $usr;
	
	$sql = "SELECT imageid FROM " . $dbprefix . "images WHERE visible = 1 AND albumid REGEXP '^(" . $scan . ")$'";
	
	$rec = $db->execute($sql);
	$result = $rec->rows;
	$rec->clear();
	
	// and return rows
	return $result;
}

function fetchlatestaddition($scan){
	global $db, $dbprefix, $usr;
	
	// exclude hidden albums
	if ($usr->Access > 1){
		$hid = "0";
	} else {
		$hid = fetchhiddenstack();
		$hid = process_stack($hid);
	}
	
	// get recordset
	$sql = "SELECT * FROM " . $dbprefix . "images WHERE albumid REGEXP '^(" . $scan . ")$' AND albumid NOT REGEXP '^(" . $hid . ")$' AND visible = 1 ORDER BY postdate DESC LIMIT 0, 1";
	$rec = $db->execute($sql);
	
	// create the result
	if ($rec->rows < 1){
		$result = "Never";
	} else {
		if ($rec->fields["title"] <> ""){
			$result = '<a href="viewimage.php?imageid=' . $rec->fields["imageid"] . '">' . $rec->fields["title"] . '</a>';
		} else {
			$result = '<a href="viewimage.php?imageid=' . $rec->fields["imageid"] . '">Untitled</a>';
		}
		
		$result .= " in ";
		
		$sql = "SELECT * FROM " . $dbprefix . "albums WHERE albumid = " . $rec->fields["albumid"];
		$alb = $db->execute($sql);
		
		if ($alb->rows < 1){
			$result .= "unknown album";
		} else {
			$result .= '<a href="viewalbum.php?albumid=' . $alb->fields["albumid"] . '">' . $alb->fields["title"] . '</a>';
		}
		
		// free up album memory
		$alb->clear();
		
		// now do the date
		$result .= "<br />" . date("j F Y H:i", $rec->fields["postdate"]);
	}
	
	// free up the memory
	$rec->clear();
	
	// and return rows
	return $result;
}

// for fetching all album IDs that are hidden
function fetchhiddenstack(){
	global $db, $dbprefix;
	
	// set up the variables
	$scan = array();
	array_push($scan, 0);
	
	// get the stuff
	$sql = "SELECT * FROM " . $dbprefix . "albums WHERE visible <> 1";
	$rec = $db->execute($sql);
	if ($rec->rows > 0){ do {
		
		array_push($scan, $rec->fields["albumid"]);
		
	} while ($rec->loop()); }
	
	return $scan;
}

// for working out who uploaded an image
function fetchusername($userid){
	global $db, $dbprefix;
	
	if ($userid == 0){ return "Anonymous"; }
	
	$sql = "SELECT * FROM " . $dbprefix . "users WHERE userid = " . dbSecure(intval($userid));
	$rec = $db->execute($sql);
	
	if ($rec->rows < 1){
		return "Unknown";
	} else {
		return $rec->fields["username"];
	}
}

function fetchsender($userid){
	global $db, $dbprefix;
	
	if ($userid == 0){ return "System"; }
	
	$sql = "SELECT * FROM " . $dbprefix . "users WHERE userid = " . dbSecure(intval($userid));
	$use = $db->execute($sql);
	if ($use->rows < 1){
		return "Unknown";
	} else {
		return $use->fields["username"];
	}
}
?>
Return current item: Particle Gallery