
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' is the value of the hidden input with the value of locorsselect (either "Locomotives" or "Rolling Stock").
- 'photo_mark', 'photo_eqtype' and 'photo_date' are the values of the reporting mark, equipment type and date select lists.
- 'photo_phototype' and 'photo_rowspage' are the values of the radio button groups listing the photo type and the number of results per page.
$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.
- The first item in the filter has to be preceeded by WHERE and all subsequent items have to be preceeded by AND.
- Part 1 of the filter code interprets the values of the select lists for reporting mark and equipment type.
- Part 2 interprets the value of the group of radio buttons for the photo type. Part 2 is preceded by either WHERE or AND, depending on whether the mark and/or equipment type are also selected in Part 1.
//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.
- The value given to the dates in the select list follows the format YYYY,MM,DD.
- The values for Year, Month and Day are extracted by splitting the YYYY,MM,DD value using the commas as a separator.
- Part 3 is preceded by either WHERE or AND, depending on whether Part 1 or Part 2 of the filter were set.
//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.
- The database searches for all results of locomotives and rolling stock that match the criteria of the filters.
- If access to the database has failed, the connection is killed.
- Once the results have been obtained, a count is made of the number of 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.
- The number of rows per page is obtained from the query string. If a value is either absent or non-numeric, a value of 10 is assigned.
- The total number of pages is obtained by dividing the number of results by the number of rows per page.
- The value of the total count depends on whether locomotives or rolling stock are selected.
- The function 'ceil' rounds the number of pages up to the next highest whole number.
$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);
- The page number is obtained by the variable 'page' listed in the query string, except when the page is first loaded.
- When the page is first loaded, a page number value is absent. If a page number value is absent, less than 1 or non-numeric, a page number of 1 is assigned.
- If the page number is greater than the total number of pages, a value equal to the total number of pages is assigned. This displays the last page.
$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)
- This will only be applied if the last page is greater than zero--ie, if there is at least one result.
- The value of X (starting point) is the product of (the page number minus one) times (the number of results per page).
- The value of Y (rows to be selected) is the number of results per page.
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.
- A link called "New Search" directs back to the Photo Search page.
- The reporting mark and equipment type are printed using values directly obtained from the query string.
- Because the values for the date follow the format YYYY,MM,DD, the date is printed using the year, month and day variables calculated previously for the database query filter (Part 3).
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';
- The photo type has abbreviated values of "D", "R" and "S" for detail, roster and scenic photos, respectively. An extra step is therefore required to indicate the photo type based on its abbreviated value.
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';
- The number of results listed depends on whether locomotives or rolling stock were selected.
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.
- The page number will only be indicated if there is at least one result.
- Links to other pages will only be printed if there is more than one page, ie. if the last page is greater than one.
if ($page_last>0)
print '<br><br>Page ' . $page_number . ' of ' . $page_last;
if($page_last>1)
{
print '<br>';
- An index called 'i' is given an initial value of 1.
- If the page number is 1, then '1' is printed in bold unlinked text, and the value of i is increased to 2.
- If the page number is greather than 1, then while i is less than the current page number:
- i is printed as linked text.
- The value of the link is an exact copy of the existing query string, except with a 'page' of i. Clicking the link will therefore create the same photo search, but with a different page number.
- After the link is printed, i is increased by 1 and the next link text is printed using the new value of i.
- After i reaches the last value before the current page number, the links stop printing.
- The current page number is then printed in bold unlinked text, and i is increased by 1.
- Subsequently, while i is less than or equal to the number of the last page, the links are printed in the same manner until i reaches the value of the last page.
$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'] .
'&selectmark=' . $_GET['selectmark'] .
'&selecttype=' . $_GET['selecttype'] .
'&selectdate=' . $_GET['selectdate'] .
'&selectphototype=' .$_GET['selectphototype'] .
'&selectrowsperpage=' . $_GET['selectrowsperpage'] .
'&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'] .
'&selectmark=' . $_GET['selectmark'] .
'&selecttype=' . $_GET['selecttype'] .
'&selectdate=' . $_GET['selectdate'] .
'&selectphototype=' . $_GET['selectphototype'] .
'&selectrowsperpage=' . $_GET['selectrowsperpage'] .
'&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.
- No table is printed if there are no results to display. This prevents the creation of table headers over an empty table.
- The table code is repeated twice: once for locomotives and once for rolling stock. Only one table will display depending on whether locomotives or rolling stock are selected.
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>';
- For the photo results, table cells are printed to match the table headers.
- All photo sizes share a nearly identical filename: '-s' at the end of small images, '-t' at the end of thumbnails and nothing at the end of full-size images. This means that each photo has only a single filename stored in the database, and the different sizes are created by simply adding '-s' or '-t' at the end of the filename from the database.
- Once all the values have been printed from the database, the table is closed.
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>";
}
- The same code is used again in almost exactly the same manner for rolling stock, except for some additional fields in the database table:
- Units, denoting A, B, C etc. such as with articulated well cars
- Specific type, which follows the general type (such as a general type of "Boxcar" with a specific type of "50' 6", 5277 cu-ft").
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 letter 'j' is used instead of 'i' to avoid conflict with the previous page listing.
- The option "New Search" is printed only if there are search results. Otherwise, a page with no results would print two "New Search" links in a row.
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'] .
'&selectmark=' . $_GET['selectmark'] .
'&selecttype=' . $_GET['selecttype'] .
'&selectdate=' . $_GET['selectdate'] .
'&selectphototype=' . $_GET['selectphototype'] .
'&selectrowsperpage=' . $_GET['selectrowsperpage'] .
'&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'] .
'&selectmark=' . $_GET['selectmark'] .
'&selecttype=' . $_GET['selecttype'] .
'&selectdate=' . $_GET['selectdate'] .
'&selectphototype=' . $_GET['selectphototype'] .
'&selectrowsperpage=' . $_GET['selectrowsperpage'] .
'&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);
?>