Advertisement
Guest User

Untitled

a guest
Nov 21st, 2014
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.06 KB | None | 0 0
  1. /*********************
  2. *** FUNCTIONS ***
  3. *********************/
  4. function is_user_id($id)
  5. {
  6. global $conn;
  7. $returnValue = TRUE;
  8.  
  9. $stmt = pg_prepare($conn, "login_query", "SELECT * FROM users
  10. WHERE user_id = $1");
  11.  
  12. // Prepared statement to check for the ID passed
  13. $loginCheck = pg_execute($conn, "id_check_query", array($id));
  14.  
  15. // This returns number of records found in results
  16. $recordsUserID = pg_num_rows($loginCheck);
  17.  
  18. // If there is a record found based on the users ID
  19. if($recordsUserID == 0)
  20. {
  21. $returnValue = FALSE;
  22. }
  23.  
  24. return $returnValue;
  25. }
  26.  
  27. function build_simple_dropdown($table_name, $prechecked = '')
  28. {
  29. global $conn; // Make $conn accessible in the function
  30. $isChecked = ''; // Variable to check if the value is the one that is checked
  31. $recordQuery = pg_query($conn, "SELECT * FROM $table_name;"); // Query the table for all of the records
  32. $columnArray = pg_fetch_all($recordQuery); // Make an array of the first row, which is the only row
  33. $output = '';
  34.  
  35. $output.= '<select name="'.$table_name.'">';
  36. // Make heading type field in drop down
  37. $output.= '<option value=""> </option>';
  38.  
  39. // Iterate through array and take each value as $value
  40. foreach($columnArray as $record)
  41. {
  42. $value = $record['value'];
  43. $isChecked = ($value == $prechecked) ? " selected='selected' " : '';
  44. $output.= '<option value="' . $value . '"' . $isChecked . '>'.ucwords($value).'</option>';
  45. }
  46. $output.= "</select>";
  47.  
  48. return $output;
  49. }
  50.  
  51. function build_dropdown($table_name, $checked = '')
  52. {
  53. global $conn; //make $conn accessible in the function
  54. $isChecked = ''; //variable to check if the value is the one that is checked
  55. $recordQuery = pg_query($conn,"SELECT*FROM $table_name;"); //query the record for all of its records
  56. $num_rows = pg_num_rows($recordQuery); //get the number of records
  57. $output = '';
  58.  
  59. $output.= '<select name="' . $table_name . '">';
  60. $output.= '<option value=""> </option>'; //add blank option to the dropdown
  61. for($counter = 0; $counter < $num_rows; $counter++)
  62. {
  63. //make each element in the drop down based on number of rows
  64. $currentValue = pg_fetch_result($recordQuery, $counter, "value");
  65.  
  66. $isChecked = ($currentValue == $checked) ? ' selected="selected" ' : '';
  67. $output.= '<option value="' . $currentValue . '"'.$isChecked.'>' . pg_fetch_result($recordQuery, $counter, "property") . '</option>';
  68. }
  69.  
  70. $output.='</select>';
  71. return $output;
  72. }
  73.  
  74. function build_radio($table_name, $checked = '')
  75. {
  76. global $conn; //make connection accessible from in the function
  77. $isChecked = ''; //variable to make an element in the form set to checked
  78. $recordQuery = pg_query($conn,"SELECT*FROM $table_name;"); //query the dbase for all of the info based on passed table
  79. $num_rows = pg_num_rows($recordQuery); //retrieve the number of rows based on the query
  80. $output = '';
  81.  
  82. for($counter = 0; $counter < $num_rows; $counter++)
  83. {
  84. $currentValue = pg_fetch_result($recordQuery, $counter, "value"); //pulls the current record from the database
  85.  
  86. //sets isChecked to essentially true if the $checked(passed argument) is the currentValue it is on, or if the counter is 0
  87. //this means if the the loop is going through with no previously selected, the first item is selected by default
  88. $isChecked = ($checked == $currentValue || $counter == 0) ? ' checked="checked" ' : '';
  89. $output.= '<input type="radio" name="' . $table_name . '" value="' . $currentValue . '"'.$isChecked.' />' . pg_fetch_result($recordQuery, $counter, "$table_name") . '<br/>';
  90. }
  91.  
  92. $output.= '<br/>';
  93. return $output;
  94. }
  95.  
  96. function build_checkbox($table_name, $checked = 0)
  97. {
  98. global $conn; //make connection accessible from in the function
  99. $isChecked = ''; //variable to make an element in the form set to checked
  100. $recordQuery = pg_query($conn,"SELECT * FROM $table_name");//query the dbase for all of the info based on passed table
  101. $num_rows = pg_num_rows($recordQuery); //retrieve the number of rows based on the query
  102. $output = "";
  103.  
  104. for($counter = 0; $counter < $num_rows; $counter++)
  105. {
  106. $value = pg_fetch_result($recordQuery, $counter, "value");
  107. $property = pg_fetch_result($recordQuery, $counter, "property");
  108.  
  109. //Check if the current value is set
  110. $isChecked = (isBitSet($counter, $checked) == 1) ? ' checked="checked" ' : '';
  111.  
  112. $output .= '<input type="checkbox" name="'.$table_name.'[]" value="'.$value.'"'.$isChecked.'/>';
  113. $output .= '<strong>'.$property.'</strong>&nbsp;&nbsp;&nbsp;';
  114. }
  115. return $output;
  116. }
  117.  
  118. function build_vertical_checkbox($table_name, $checked = 0)
  119. {
  120. global $conn; //make connection accessible from in the function
  121. $isChecked = ''; //variable to make an element in the form set to checked
  122. $recordQuery = pg_query($conn,"SELECT * FROM $table_name");//query the dbase for all of the info based on passed table
  123. $num_rows = pg_num_rows($recordQuery); //retrieve the number of rows based on the query
  124. $output = "";
  125.  
  126. for($counter = 0; $counter < $num_rows; $counter++)
  127. {
  128. $value = pg_fetch_result($recordQuery, $counter, "value");
  129. $property = pg_fetch_result($recordQuery, $counter, "property");
  130. $isChecked = (isBitSet($counter, $checked) == 1) ? ' checked="checked" ' : '';
  131.  
  132. $output .= '<input type="checkbox" name="'.$table_name.'[]" value="'.$value.'"'.$isChecked.'/>';
  133. $output .= '<strong>'.$property.'</strong>&nbsp;<br/>';
  134.  
  135. }
  136. return $output;
  137. }
  138.  
  139. function build_horizontal_radio($table_name, $checked = '')
  140. {
  141. global $conn; //make connection accessible from in the function
  142. $isChecked = ''; //variable to make an element in the form set to checked
  143. $recordQuery = pg_query($conn,"SELECT*FROM $table_name;"); //query the dbase for all of the info based on passed table
  144. $num_rows = pg_num_rows($recordQuery); //retrieve the number of rows based on the query
  145. $output = "";
  146.  
  147. for($counter = 0; $counter < $num_rows; $counter++)
  148. {
  149. $currentValue = pg_fetch_result($recordQuery, $counter, "value"); //pulls the current record from the database
  150.  
  151. //sets isChecked to essentially true if the $checked(passed argument) is the currentValue it is on, or if the counter is 0
  152. //this means if the the loop is going through with no previously selected, the first item is selected by default
  153. $isChecked = ($checked == $currentValue || $counter == 0) ? ' checked="checked" ' : '';
  154. $output.= '<input type="radio" name="' . $table_name . '" value="' . $currentValue . '"'.$isChecked.' />' . pg_fetch_result($recordQuery, $counter, "property");
  155. }
  156.  
  157. $output.= '<br/>';
  158. return $output;
  159. }
  160.  
  161. function get_property($value, $table_name)
  162. {
  163. global $conn;
  164. $recordQuery = pg_query($conn, "SELECT property FROM $table_name WHERE value= $value;");
  165. $result = pg_fetch_row($recordQuery);
  166. return $result[0];
  167. }
  168.  
  169. function listing_preview($listing_id)
  170. {
  171. global $conn;
  172. $listingInfo = pg_query($conn, "SELECT * FROM listings WHERE listing_id = $listing_id;");
  173. $result = pg_fetch_assoc($listingInfo);
  174. $preview = '';
  175. //dump($result);
  176.  
  177. $preview.='<table style="width:100%;cellpadding:5;text-align:center;">';
  178. $preview.=' <tr>
  179. <th colspan="3" style="text-align:left";">
  180. <a href="./listing-display.php?listing_id='.$listing_id.'">'.$result['headline'].'</a>
  181. </th>
  182. </tr>
  183. <tr>
  184. <td rowspan="3" style="height:150px;">
  185. <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>"
  186. </td>
  187. <td rowspan="3" style="border:1px solid;width:120px">
  188. <img src="./images/favicon.png" alt="House Icon" />' . get_property($result["property_type"], PROPERTY_TYPE_TABLE) . ' <hr/><br/>
  189. ' . get_property($result["number_of_bedrooms"], BEDROOMS_TABLE) . '<br/>
  190. Bedrooms <br/>
  191. ' . get_property($result["number_of_bathrooms"], BATHROOMS_TABLE) . ' <br/>
  192. Bathrooms <br/>
  193. </td>
  194. <td class="left">
  195. <strong>$'.$result["price"].'</strong> <br/>
  196. </td>
  197. </tr>
  198. <tr>
  199. <td class="left">
  200. '. $result["address"].'<br/>
  201. '.get_property($result["city"], CITY_TABLE).', '. $result["province"].', ' .$result["postal_code"].'
  202. </td>
  203. </tr>
  204. <tr>
  205. <td class="left" style="width:50%;">
  206. ' . substr($result["description"], 0, 100) . '...
  207. </td>
  208. </tr>
  209. </table>';
  210.  
  211. return $preview;
  212. }
  213.  
  214. function build_sql_from_checkbox($checkbox, $field_name) //passed the checkbox array from post, and name of the field its passing
  215. {
  216. $sqlToAdd = '';
  217. $sqlToAdd = " AND (";
  218.  
  219. for($counter = 0; $counter < sizeof($checkbox); $counter++)
  220. {
  221. $sqlToAdd .= "listings." . $field_name . " = " . $checkbox[$counter];
  222.  
  223. if($counter != sizeof($checkbox)-1)
  224. {
  225. $sqlToAdd.= " OR ";
  226. }
  227. }
  228. $sqlToAdd .= ") ";
  229.  
  230. return $sqlToAdd;
  231. }
  232. function build_sql_from_value($value, $field_name) //passed the checkbox array from post, and name of the field its passing
  233. {
  234. $sqlToAdd = '';
  235. if($value > 0)
  236. {
  237. $sqlToAdd = " AND (";
  238. $i = 0;
  239. while(pow(2, $i) <= $value)
  240. {
  241. $sqlToAdd .= isBitSet($i, $value)?"listings." . $field_name . " = " . pow(2,$i) . " OR ":"";
  242.  
  243. $i++;
  244. }
  245. $sqlToAdd = substr($sqlToAdd, 0, (strlen($sqlToAdd) - 4));
  246. $sqlToAdd .= ") ";
  247. }
  248. return $sqlToAdd;
  249. }
  250. function getPropertyList($table, $elements_as_sum = 0, $delimit = ", ", $terminator = " &amp; ")
  251. {
  252. global $conn;
  253. $results = pg_query($conn, "SELECT property FROM " . $table);
  254. $elements = pg_fetch_all($results);
  255. $list = "";
  256. for ($counter = 0; $counter < sizeof($elements); $counter++)
  257. {
  258. $list .= (isBitSet($counter, $elements_as_sum ))? $elements[$counter]['property'] . $delimit: '';
  259.  
  260. /*if (pow(2, $counter) > $elements_as_sum )
  261. {
  262. $list = substr($list, 0, (strlen($list)-strlen($delimit)));
  263. $list .= $terminator;
  264. }*/
  265. }
  266.  
  267. $list = substr($list, 0, (strlen($list)-strlen($delimit))) . '.';
  268. return $list;
  269. }
  270. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement