
Page Code
This page explains the form used to enter the photo search. For the page displaying the results, see the code for the Photo Search Results page.
Photo Search Enter Page
The "Photo Search Enter" page uses a form to send a query to the "Photo Search Results" page, which then presents photos based on what was filled in the form. The following shows the code used for the form on the "Photo Search Enter" page. All parts of the code are included, so when attached together in a string they create the form's entire code.
To avoid presenting variables that are normally hidden, some variable names have been changed.
Database connect
Before the form is presented, a connection needs to be made to the database. The following code connects to the database server.
- All code is based on php, so a php opening tag is required at the beginning.
- If the connection to the server has failed, the process is killed and a message states "Could not connect".
- Replace "host_address", "user" and "password" with their actual values.
<?php
$connect = mysql_connect("host_address","user","password");
if (!$connect)
die('Could not connect');
Once a connection has been made to the server, the database needs to be selected.
- Replace "database_name" with its actual value.
mysql_select_db("database_name", $connect);
Form Fields
There are two sets of form fields: one for locomotives and one for rolling stock.
- The set of fields to be presented is determined by a variable that is added to the query string of the page, called 'locorsselect'.
- The variable 'locorsselect' has two possible values: "Locomotives" or "Rolling Stock".
- If the value of the variable is anything besides the two possible values, it is automatically given the value "Locomotives".
if ($_GET['locorsselect'] != "Locomotives" || $_GET['locorsselect'] != "Rolling Stock") $_GET['locorsselect']="Locomotives";
Switching between "Locomotives" and "Rolling Stock"
To navigate between locomotive and rolling stock photos, the terms "Locomotives" and "Rolling Stock" are displayed in a list.
- The current selection is printed in bold.
- The alternative selection has a link that reloads the page, with a query string giving a new value to 'locorsselect'.
if ($_GET['locorsselect'] == "Locomotives") print '<ul> <li><b>Locomotives</b></li> <li><a href="mephotosearchenter.php?locorsselect=Rolling+Stock"> Rolling Stock</a></li> </ul>'; if ($_GET['locorsselect'] == "Rolling Stock") print '<ul> <li><a href="mephotosearchenter.php?locorsselect=Locomotives"> Locomotives</a></li> <li><b>Rolling Stock</b></li> </ul>';
Form Start
The form action is the "Photo Search Results" page, called 'mephotosearchresults.php'. The value of 'locorsselect' is needed for that page, so it is included in the form as a hidden value.
print '<form action="http://trainiax.net/mephotosearchresults.php"> <input type="hidden" name="result_locors" value="' . $_GET['locorsselect'] . '">';
Reporting Marks
Locomotives and rolling stock are stored in two different tables. Table_1 stores locomotives, and Table_2 stores rolling stock. Once the value of locorsselect is determined, the form presents a select list of all the reporting marks found in either table. The following code is for this select list.
- Depending on the value of 'locorsselect', the heading indicates either "Locomotives - Reporting Marks" or "Rolling Stock - Reporting Marks".
- The first option is "All Marks", followed by an empty option with the same value. This is simply to create a space before the marks themselves are listed.
- One option is created for each mark and is given the value of the mark.
- An additional query is made to the database to indicate the number of photos for each mark.
print '<h3>' . $_GET['locorsselect'] . ' - Reporting Marks</h3>';
print '<select name="selectmark">
<option>All Marks</option>
<option value="All Marks"></option>';
//Selection of Locomotive Mark
if ($_GET['locorsselect'] == "Locomotives")
{
$loco_marklist = mysql_query("SELECT DISTINCT Mark
FROM Table_1
ORDER BY Mark");
while($loco_markrow=mysql_fetch_array($loco_marklist))
{
//the following three lines are for number count
$count_locomark=$loco_markrow['Mark'];
$count_locoquery=mysql_query("SELECT *
FROM Table_1
WHERE Mark='$count_locomark'");
$count_lococount=mysql_num_rows($count_locoquery);
print '<option value="' . $loco_markrow['Mark'] .
'">' . $loco_markrow['Mark'] .
' (' . $count_lococount . ')</option>';
}
}
//Selection of Rolling Stock Mark
if ($_GET['locorsselect'] == "Rolling Stock")
{
$rstock_marklist = mysql_query("SELECT DISTINCT Mark
FROM Table_2
ORDER BY Mark");
while($rstock_markrow=mysql_fetch_array($rstock_marklist))
{
$count_rstockmark=$rstock_markrow['Mark'];
$count_rstockquery=mysql_query("SELECT *
FROM Table_2
WHERE Mark='$count_rstockmark'");
$count_rstockcount=mysql_num_rows($count_rstockquery);
print '<option value="' . $rstock_markrow['Mark'] .
'">' . $rstock_markrow['Mark'] .
' (' . $count_rstockcount . ')</option>';
}
}
print '</select><br>';
Types of Locomotives or Rolling Stock
Following a similar method as with the marks, a select list is created of all types of locomotives or rolling stock found in either Table_1 or Table_2.
- Depending on the value of 'locorsselect', the heading indicates either "Types of Locomotives" or "Types of Rolling Stock".
- The first option is "All Types", followed by an empty option with the same value.
- Locomotive types include the builder in the list; rolling stock types do not.
- One option is created for each type (rolling stock) or each builder-type combination (locomotives)
print '<h3>Types of ' . $_GET['locorsselect'] . '</h3>';
print '<select name="selecttype">
<option selected>All Types</option>
<option value="All Types"></option>';
//Selection of Locomotive Type
if ($_GET['locorsselect'] == "Locomotives")
{
$loco_typelist = mysql_query("SELECT DISTINCT Builder,Type
FROM Table_1
ORDER BY Builder, Type");
while($loco_typerow=mysql_fetch_array($loco_typelist))
{
print '<option value="' . $loco_typerow['Type'] . '">' .
$loco_typerow['Builder'] . ' ' .
$loco_typerow['Type'] . '</option>';
}
}
//Selection of Rolling Stock Type
if ($_GET['locorsselect'] == "Rolling Stock")
{
$rstock_typelist = mysql_query("SELECT DISTINCT Type
FROM Table_2
ORDER BY Type");
while($rstock_typerow=mysql_fetch_array($rstock_typelist))
{
print '<option>' . $rstock_typerow['Type'] . '</option>';
}
}
print '</select><br>';
Date
The database has the year, month and day listed in separate columns. The date selection list presents all combinations of year, month and day for either locomotives or rolling stock.
- The database connection is closed at the end of this select list.
- The php tag is also closed since no more php code is required for the rest of the page.
print '<h3>Dates</h3>
<select name="selectdate">
<option selected>All Dates</option>
<option value="All Dates"></option>';
if ($_GET['locorsselect'] == "Locomotives")
{
$list_date = mysql_query("SELECT DISTINCT Year, Month, Day
FROM Table_1
ORDER BY Year, Month, Day");
}
if ($_GET['locorsselect'] == "Rolling Stock")
{
$list_date = mysql_query("SELECT DISTINCT Year, Month, Day
FROM Table_2
ORDER BY Year, Month, Day");
}
while($row_date = mysql_fetch_array($list_date))
{
print '<option value="' . $row_date['Year'] . ',' .
$row_date['Month'] . ',' . $row_date['Day'] . '">' .
$row_date['Year'] . '-' . $row_date['Month'] . '-' .
$row_date['Day'] . '</option>';
}
print '</select>';
?>
Other Form Inputs
The remaining form inputs are simple html and serve to select photo type and result pages.
- A set of radio buttons with the name "selectphototype" presents options of All Photos, or Roster, Detail or Scenic photos only.
- A set of radio buttons with the name "selectrowsperpage" selects the number of results to be presented per page (10, 20, 50 or 100).
- The form ends with Submit and Reset buttons.
<h3>Type of photos</h3> <input type="radio" name="selectphototype" value="All Photos" checked id="id_All"><label for="id_All">All Photos</label><br> <input type="radio" name="selectphototype" value="R" id="id_R"><label for="id_R">Roster Photos Only</label><br> <input type="radio" name="selectphototype" value="D" id="id_D"><label for="id_D">Detail Photos Only</label><br> <input type="radio" name="selectphototype" value="S" id="id_S"><label for="id_S">Scenic Photos Only</label><br> <h3>Results per page</h3> <input type="radio" name="selectrowsperpage" value="10" id="rows10"><label for="rows10">10</label><br> <input type="radio" name="selectrowsperpage" value="20" checked id="rows20"><label for="rows20">20</label><br> <input type="radio" name="selectrowsperpage" value="50" id="rows50"><label for="rows50">50</label><br> <input type="radio" name="selectrowsperpage" value="100" id="rows100"><label for="rows100">100</label><br> <br> <input value="Search Photos" type="submit"> <input value="Reset" type="reset"> </form>