Drawings Photos Recordings Train Info.
crossbuck

Valid HTML!

Valid CSS!

Page Code

This page explains the code used to present photo results, based on the query from the Photo Search Enter page. For entering the query, see the code for the Photo Search Enter page.

Photo Search Results Page

The "Photo Search Results" page gathers the query sent by the "Photo Search Enter" page in order to present photos based on the query string. This page shows the entire code used to display the photos.

To avoid presenting variables that are normally hidden, some variable names have been changed.

Database connect

As with the Photo Search Enter page, the first operation is to connect to the server and select the database. This code is exactly the same as the code at the beginning of the Photo Search Enter page.

<?php
$connect = mysql_connect("host_address","user","password");
if (!$connect)
	die('Could not connect');
mysql_select_db("database_name", $connect);

Variables

Before accessing the database, all main variables extracted from the query string.

$photo_locorstock = $_GET['result_locors'];
$photo_mark = $_GET['selectmark'];
$photo_eqtype = $_GET['selecttype'];
$photo_date = $_GET['selectdate'];
$photo_phototype = $_GET['selectphototype'];
$photo_rowspage = $_GET['selectrowsperpage'];

Filtering the Results

The following code creates the correct filter for the results, in order to account for all possible combinations of filtering by mark, equipment type and by date.

//Type of Filter - Part 1
if ($photo_eqtype != "All Types" & $photo_mark != "All Marks")
	$filter1 = "WHERE Type = '$photo_eqtype' AND Mark = '$photo_mark'";
elseif ($photo_eqtype != "All Types")
	$filter1 = "WHERE Type = '$photo_eqtype'";
elseif ($photo_mark != "All Marks")
	$filter1 = "WHERE Mark = '$photo_mark'";
else
	$filter1 = "";

//Type of Filter - Part 2
if ($photo_phototype != "All Photos" & $photo_eqtype == "All Types" & $photo_mark == "All Marks")
	$filter2 = "WHERE PhotoType = '$photo_phototype'";
elseif ($photo_phototype != "All Photos")
	$filter2 = "AND PhotoType = '$photo_phototype'";
else
	$filter2 = "";

Part 3 of the filter interprets the value of the select list for the date.

//Type of Filter - Part 3
list($photo_yyyy, $photo_mm, $photo_dd) = explode(",", $photo_date);

if ($photo_date != "All Dates" & $filter1 == "" & $filter2 == "")
	{
	$filter3 = "WHERE Year = '$photo_yyyy' 
		AND Month = '$photo_mm' 
		AND Day = '$photo_dd'";
	}
		
elseif ($photo_date != "All Dates")
	{
	$filter3 = "AND Year = '$photo_yyyy' 
		AND Month = '$photo_mm' 
		AND Day = '$photo_dd'";
	}
		
else
	$filter3 = "";

Database Access for Count

The database is accessed twice. The first database query serves to count the results.

$loco_result2 = mysql_query("SELECT * 
	FROM PhotosB $filter1 $filter2 $filter3") 
	or die(mysql_error());
	
$rstock_result2 = mysql_query("SELECT * 
	FROM PhotosA $filter1 $filter2 $filter3") 
	or die(mysql_error());
	
$count1 = mysql_num_rows($loco_result2); 
$count2 = mysql_num_rows($rstock_result2);

Page Number Calculating

The purpose of counting the results is not only to list the number of results, but also to split the results into multiple pages if necessary.

$photo_rowspage = $_GET['selectrowsperpage'];

if (empty($photo_rowspage)||!is_numeric($photo_rowspage))
	$photo_rowspage = 10;
	
if ($photo_locorstock == "Locomotives")
	$page_last = ceil($count1/$photo_rowspage);
elseif ($photo_locorstock == "Rolling Stock")
	$page_last = ceil($count2/$photo_rowspage);
$page_number=$_GET['page'];

if(empty($page_number)||$page_number<1||!is_numeric($page_number))
	$page_number=1;
if($page_number>$page_last)
	$page_number=$page_last;

A selected number of database results can be displayed using the syntax LIMIT X, Y where X is the starting point and Y is the number of rows to be selected. For instance, LIMIT 30,20 will display 20 results starting at the 31st row (the first row is row 0)

if($page_last>0)
	$display_select = 'LIMIT ' . ($page_number-1) * $photo_rowspage . ', ' . $photo_rowspage;

Database Access for Results

A second database query is made in order to obtain the photo results. It is the same as the first query, except that the results are placed in order and selected based on the page number.

$loco_result1 = mysql_query("SELECT * 
	FROM PhotosB $filter1 $filter2 $filter3 
	ORDER BY Mark, Number, Link 
	$display_select") or die(mysql_error());
	
$rstock_result1 = mysql_query("SELECT * 
	FROM PhotosA $filter1 $filter2 $filter3 
	ORDER BY Mark, Number, Ord, Link 
	$display_select") or die(mysql_error());

Headers for Results

Headers at the top of the page indicate the reporting mark, equipment type, date, photo type and the total number of results. Links to page numbers are then listed below the headers.

print '<a href="http://trainiax.net/mephotosearchenter.php">
	New Search</a><br><br>';
	
print '<b>Reporting Mark: </b>' . $photo_mark . 
	'<br><b>Equipment Type: </b>' . $photo_locorstock . ', ' . $photo_eqtype . 
	'<br><b>Date: </b>';
	
if ($photo_date != "All Dates")
	print $photo_yyyy . '-' . $photo_mm . '-' . $photo_dd;
else
	print 'All Dates';
print '<br><b>Photo Type: </b>';

if ($photo_phototype == "All Photos")
	print 'All Photos';
elseif ($photo_phototype == "D")
	print 'Detail Photos Only';
elseif ($photo_phototype == "R")
	print 'Roster Photos Only';
elseif ($photo_phototype == "S")
	print 'Scenic Photos Only';
print '<br><b>Results: </b>';
if($photo_locorstock == "Locomotives")
	print $count1;
if($photo_locorstock == "Rolling Stock")
	print $count2;

Page Number Listing - Before Results

The page number listing is the most complicated part of the code. First, the current page is indicated out of the total (eg. "Page 1 of 3"). Then, a listing of all pages is created, with links to all the pages except for the current page, which is listed in bold and unlinked.

if ($page_last>0)
	print '<br><br>Page ' . $page_number . ' of ' . $page_last;
if($page_last>1)
	{
	print '<br>';
	$i=1;
	if($page_number==1)
		{
		print '<b>' . $page_number . ' </b>'; $i++;
		}
	while($i<$page_number)
		{
		print '<a href="mephotosearchresults.php?
		locorstock_result1=' . $_GET['locorstock_result1'] . 
		'&amp;selectmark=' . $_GET['selectmark'] . 
		'&amp;selecttype=' . $_GET['selecttype'] . 
		'&amp;selectdate=' . $_GET['selectdate'] . 
		'&amp;selectphototype=' .$_GET['selectphototype'] . 
		'&amp;selectrowsperpage=' . $_GET['selectrowsperpage'] . 
		'&amp;page=' . $i .'">' . $i . '</a> ';
		$i++;
		}
	if($page_number!=1)
		{
		print '<b>' . $page_number . ' </b>'; $i++;
		}
	while($i<($page_last+1))
		{
		print '<a href="mephotosearchresults.php
		?locorstock_result1=' . $_GET['locorstock_result1'] . 
		'&amp;selectmark=' . $_GET['selectmark'] .
		'&amp;selecttype=' . $_GET['selecttype'] . 
		'&amp;selectdate=' . $_GET['selectdate'] . 
		'&amp;selectphototype=' . $_GET['selectphototype'] . 
		'&amp;selectrowsperpage=' . $_GET['selectrowsperpage'] . 
		'&amp;page=' . $i .'">' . $i . '</a> ';
		$i++;
		}
	}

Results - Table Display

The results are presented in an html table. In this case, the table uses some styles applied to the cells (defined in class "drawings") but uses no size specifications.

if($count1>0 & $photo_locorstock == "Locomotives")
	{
	print '<br><br><table border=1><tr>
	<th>Mark</th>
	<th>Number</th>
	<th>Thumbnail</th>
	<th>Size</th>
	<th>Information</th></tr>';
	while($loco_row=mysql_fetch_array($loco_result1))
		{
		print "<tr>";
		print "<td>" . $loco_row['Mark'] . "</td>";
		print "<td>" . $loco_row['Number'] . "</td>";
		
		print '<td><img src="http://trainiax.net/photos/' . 
			$loco_row['Link'] . '-t.JPG" alt=""></td>';
		
		print '<td><a href="http://trainiax.net/photos/' . 
			$loco_row['Link'] . '.JPG">Large</a><br>
			<a href="http://trainiax.net/photos/' . 
			$loco_row['Link'] . '-s.JPG">Small</a></td>';
		
		print '<td>Type: ' . $loco_row['Type'] . '<br>
			Builder: ' . $loco_row['Builder'] . '<br>
			Date: ' . $loco_row['Year'] . '-' . $loco_row['Month'] . '-' . 
				$loco_row['Day'] . '<br>
			Location: ' . $loco_row['Location'] . '<br>
			Railroad: ' . $loco_row['Railroad'] . '</td>';
		print "</tr>";
		}
	print "</table>";
	}
if($count2>0 & $photo_locorstock == "Rolling Stock")
	{print '<br><br><table border=1><tr>
	<th>Mark</th>
	<th>Number</th>
	<th>Thumbnail</th>
	<th>Size</th>
	<th>Information</th></tr>';
	while($rstock_row=mysql_fetch_array($rstock_result1))
		{
		print "<tr>";
		print "<td>" . $rstock_row['Mark'] . "</td>";
		print "<td>" . $rstock_row['Number'] . " " . 
			$rstock_row['Unit'] . "</td>";
			
		print '<td><img src="http://trainiax.net/photos/' .
			$rstock_row['Link'] . '-t.JPG" alt=""></td>';
			
		print '<td><a href="http://trainiax.net/photos/' .
			$rstock_row['Link'] . '.JPG">Large</a><br>
			<a href="http://trainiax.net/photos/' . 
			$rstock_row['Link'] . '-s.JPG">Small</a></td>';
			
		print '<td>
			Type: ' . $rstock_row['Type'] . ', ' . 
				$rstock_row['TypeSpecific'] . '<br>
			Builder: ' . $rstock_row['Builder'] . '<br>
			Date: ' . $rstock_row['Year'] . '-' . $rstock_row['Month'] . '-' .
				$rstock_row['Day'] . '<br>
			Location: ' . $rstock_row['Location'] . '<br>
			Railroad: ' . $rstock_row['Railroad'] . '</td>';
		print "</tr>";
		}
	print "</table>";
	}

Page Number Listing - After Results

After the results are printed, the page number list is displayed in the same manner as before, with two changes:

The database connection and php tags are closed.

if($page_last>0)
	print '<br>Page ' . $page_number . ' of ' . $page_last;
if($page_last>1)
	{
	print '<br>'; $j=1;
	if($page_number==1)
		{
		print '<b>' . $page_number . ' </b>'; 
		$j++;
		}
	while($j<$page_number)
		{
		print '<a href="mephotosearchresults.php?
		locorstock_result1=' . $_GET['locorstock_result1'] . 
		'&amp;selectmark=' . $_GET['selectmark'] .
		'&amp;selecttype=' . $_GET['selecttype'] . 
		'&amp;selectdate=' . $_GET['selectdate'] . 
		'&amp;selectphototype=' . $_GET['selectphototype'] . 
		'&amp;selectrowsperpage=' . $_GET['selectrowsperpage'] . 
		'&amp;page=' . $j .'">' . $j . '</a> ';
		$j++;
		}
	if($page_number!=1)
		{
		print '<b>' . $page_number . ' </b>'; 
		$j++;
		}
	while($j<($page_last+1))
		{
		print '<a href="mephotosearchresults.php?
		locorstock_result1=' . $_GET['locorstock_result1'] . 
		'&amp;selectmark=' . $_GET['selectmark'] .
		'&amp;selecttype=' . $_GET['selecttype'] . 
		'&amp;selectdate=' . $_GET['selectdate'] . 
		'&amp;selectphototype=' . $_GET['selectphototype'] . 
		'&amp;selectrowsperpage=' . $_GET['selectrowsperpage'] . 
		'&amp;page=' . $j .'">' . $j . '</a> ';
		$j++;
		}
	print '<br>';
	}
	
if($page_last>0)
	{
	print'<br><a href="
		http://trainiax.net/mephotosearchenter.php">
		New Search</a><br><br>';
	}
mysql_close($connect);
?>
Copyright © Michael Eby - Page code last updated 2010-07-18