Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*********************
- *** FUNCTIONS ***
- *********************/
- function is_user_id($id)
- {
- global $conn;
- $returnValue = TRUE;
- $stmt = pg_prepare($conn, "login_query", "SELECT * FROM users
- WHERE user_id = $1");
- // Prepared statement to check for the ID passed
- $loginCheck = pg_execute($conn, "id_check_query", array($id));
- // This returns number of records found in results
- $recordsUserID = pg_num_rows($loginCheck);
- // If there is a record found based on the users ID
- if($recordsUserID == 0)
- {
- $returnValue = FALSE;
- }
- return $returnValue;
- }
- function build_simple_dropdown($table_name, $prechecked = '')
- {
- global $conn; // Make $conn accessible in the function
- $isChecked = ''; // Variable to check if the value is the one that is checked
- $recordQuery = pg_query($conn, "SELECT * FROM $table_name;"); // Query the table for all of the records
- $columnArray = pg_fetch_all($recordQuery); // Make an array of the first row, which is the only row
- $output = '';
- $output.= '<select name="'.$table_name.'">';
- // Make heading type field in drop down
- $output.= '<option value=""> </option>';
- // Iterate through array and take each value as $value
- foreach($columnArray as $record)
- {
- $value = $record['value'];
- $isChecked = ($value == $prechecked) ? " selected='selected' " : '';
- $output.= '<option value="' . $value . '"' . $isChecked . '>'.ucwords($value).'</option>';
- }
- $output.= "</select>";
- return $output;
- }
- function build_dropdown($table_name, $checked = '')
- {
- global $conn; //make $conn accessible in the function
- $isChecked = ''; //variable to check if the value is the one that is checked
- $recordQuery = pg_query($conn,"SELECT*FROM $table_name;"); //query the record for all of its records
- $num_rows = pg_num_rows($recordQuery); //get the number of records
- $output = '';
- $output.= '<select name="' . $table_name . '">';
- $output.= '<option value=""> </option>'; //add blank option to the dropdown
- for($counter = 0; $counter < $num_rows; $counter++)
- {
- //make each element in the drop down based on number of rows
- $currentValue = pg_fetch_result($recordQuery, $counter, "value");
- $isChecked = ($currentValue == $checked) ? ' selected="selected" ' : '';
- $output.= '<option value="' . $currentValue . '"'.$isChecked.'>' . pg_fetch_result($recordQuery, $counter, "property") . '</option>';
- }
- $output.='</select>';
- return $output;
- }
- function build_radio($table_name, $checked = '')
- {
- global $conn; //make connection accessible from in the function
- $isChecked = ''; //variable to make an element in the form set to checked
- $recordQuery = pg_query($conn,"SELECT*FROM $table_name;"); //query the dbase for all of the info based on passed table
- $num_rows = pg_num_rows($recordQuery); //retrieve the number of rows based on the query
- $output = '';
- for($counter = 0; $counter < $num_rows; $counter++)
- {
- $currentValue = pg_fetch_result($recordQuery, $counter, "value"); //pulls the current record from the database
- //sets isChecked to essentially true if the $checked(passed argument) is the currentValue it is on, or if the counter is 0
- //this means if the the loop is going through with no previously selected, the first item is selected by default
- $isChecked = ($checked == $currentValue || $counter == 0) ? ' checked="checked" ' : '';
- $output.= '<input type="radio" name="' . $table_name . '" value="' . $currentValue . '"'.$isChecked.' />' . pg_fetch_result($recordQuery, $counter, "$table_name") . '<br/>';
- }
- $output.= '<br/>';
- return $output;
- }
- function build_checkbox($table_name, $checked = 0)
- {
- global $conn; //make connection accessible from in the function
- $isChecked = ''; //variable to make an element in the form set to checked
- $recordQuery = pg_query($conn,"SELECT * FROM $table_name");//query the dbase for all of the info based on passed table
- $num_rows = pg_num_rows($recordQuery); //retrieve the number of rows based on the query
- $output = "";
- for($counter = 0; $counter < $num_rows; $counter++)
- {
- $value = pg_fetch_result($recordQuery, $counter, "value");
- $property = pg_fetch_result($recordQuery, $counter, "property");
- //Check if the current value is set
- $isChecked = (isBitSet($counter, $checked) == 1) ? ' checked="checked" ' : '';
- $output .= '<input type="checkbox" name="'.$table_name.'[]" value="'.$value.'"'.$isChecked.'/>';
- $output .= '<strong>'.$property.'</strong> ';
- }
- return $output;
- }
- function build_vertical_checkbox($table_name, $checked = 0)
- {
- global $conn; //make connection accessible from in the function
- $isChecked = ''; //variable to make an element in the form set to checked
- $recordQuery = pg_query($conn,"SELECT * FROM $table_name");//query the dbase for all of the info based on passed table
- $num_rows = pg_num_rows($recordQuery); //retrieve the number of rows based on the query
- $output = "";
- for($counter = 0; $counter < $num_rows; $counter++)
- {
- $value = pg_fetch_result($recordQuery, $counter, "value");
- $property = pg_fetch_result($recordQuery, $counter, "property");
- $isChecked = (isBitSet($counter, $checked) == 1) ? ' checked="checked" ' : '';
- $output .= '<input type="checkbox" name="'.$table_name.'[]" value="'.$value.'"'.$isChecked.'/>';
- $output .= '<strong>'.$property.'</strong> <br/>';
- }
- return $output;
- }
- function build_horizontal_radio($table_name, $checked = '')
- {
- global $conn; //make connection accessible from in the function
- $isChecked = ''; //variable to make an element in the form set to checked
- $recordQuery = pg_query($conn,"SELECT*FROM $table_name;"); //query the dbase for all of the info based on passed table
- $num_rows = pg_num_rows($recordQuery); //retrieve the number of rows based on the query
- $output = "";
- for($counter = 0; $counter < $num_rows; $counter++)
- {
- $currentValue = pg_fetch_result($recordQuery, $counter, "value"); //pulls the current record from the database
- //sets isChecked to essentially true if the $checked(passed argument) is the currentValue it is on, or if the counter is 0
- //this means if the the loop is going through with no previously selected, the first item is selected by default
- $isChecked = ($checked == $currentValue || $counter == 0) ? ' checked="checked" ' : '';
- $output.= '<input type="radio" name="' . $table_name . '" value="' . $currentValue . '"'.$isChecked.' />' . pg_fetch_result($recordQuery, $counter, "property");
- }
- $output.= '<br/>';
- return $output;
- }
- function get_property($value, $table_name)
- {
- global $conn;
- $recordQuery = pg_query($conn, "SELECT property FROM $table_name WHERE value= $value;");
- $result = pg_fetch_row($recordQuery);
- return $result[0];
- }
- function listing_preview($listing_id)
- {
- global $conn;
- $listingInfo = pg_query($conn, "SELECT * FROM listings WHERE listing_id = $listing_id;");
- $result = pg_fetch_assoc($listingInfo);
- $preview = '';
- //dump($result);
- $preview.='<table style="width:100%;cellpadding:5;text-align:center;">';
- $preview.=' <tr>
- <th colspan="3" style="text-align:left";">
- <a href="./listing-display.php?listing_id='.$listing_id.'">'.$result['headline'].'</a>
- </th>
- </tr>
- <tr>
- <td rowspan="3" style="height:150px;">
- <a href="./listing-display.php?listing_id='.$listing_id.'"><img src="./images/no_image.png" alt="No images for this listing" style="width:225px;height:152px; /> </a>"
- </td>
- <td rowspan="3" style="border:1px solid;width:120px">
- <img src="./images/favicon.png" alt="House Icon" />' . get_property($result["property_type"], PROPERTY_TYPE_TABLE) . ' <hr/><br/>
- ' . get_property($result["number_of_bedrooms"], BEDROOMS_TABLE) . '<br/>
- Bedrooms <br/>
- ' . get_property($result["number_of_bathrooms"], BATHROOMS_TABLE) . ' <br/>
- Bathrooms <br/>
- </td>
- <td class="left">
- <strong>$'.$result["price"].'</strong> <br/>
- </td>
- </tr>
- <tr>
- <td class="left">
- '. $result["address"].'<br/>
- '.get_property($result["city"], CITY_TABLE).', '. $result["province"].', ' .$result["postal_code"].'
- </td>
- </tr>
- <tr>
- <td class="left" style="width:50%;">
- ' . substr($result["description"], 0, 100) . '...
- </td>
- </tr>
- </table>';
- return $preview;
- }
- function build_sql_from_checkbox($checkbox, $field_name) //passed the checkbox array from post, and name of the field its passing
- {
- $sqlToAdd = '';
- $sqlToAdd = " AND (";
- for($counter = 0; $counter < sizeof($checkbox); $counter++)
- {
- $sqlToAdd .= "listings." . $field_name . " = " . $checkbox[$counter];
- if($counter != sizeof($checkbox)-1)
- {
- $sqlToAdd.= " OR ";
- }
- }
- $sqlToAdd .= ") ";
- return $sqlToAdd;
- }
- function build_sql_from_value($value, $field_name) //passed the checkbox array from post, and name of the field its passing
- {
- $sqlToAdd = '';
- if($value > 0)
- {
- $sqlToAdd = " AND (";
- $i = 0;
- while(pow(2, $i) <= $value)
- {
- $sqlToAdd .= isBitSet($i, $value)?"listings." . $field_name . " = " . pow(2,$i) . " OR ":"";
- $i++;
- }
- $sqlToAdd = substr($sqlToAdd, 0, (strlen($sqlToAdd) - 4));
- $sqlToAdd .= ") ";
- }
- return $sqlToAdd;
- }
- function getPropertyList($table, $elements_as_sum = 0, $delimit = ", ", $terminator = " & ")
- {
- global $conn;
- $results = pg_query($conn, "SELECT property FROM " . $table);
- $elements = pg_fetch_all($results);
- $list = "";
- for ($counter = 0; $counter < sizeof($elements); $counter++)
- {
- $list .= (isBitSet($counter, $elements_as_sum ))? $elements[$counter]['property'] . $delimit: '';
- /*if (pow(2, $counter) > $elements_as_sum )
- {
- $list = substr($list, 0, (strlen($list)-strlen($delimit)));
- $list .= $terminator;
- }*/
- }
- $list = substr($list, 0, (strlen($list)-strlen($delimit))) . '.';
- return $list;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement