Posted By

geothen on 04/15/09


Tagged

php list object textmate


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

Bluewall


list functions


 / Published in: PHP
 

  1. function list_free_dataset() {
  2. $this->list['has_limit'] = 0;
  3. $this->list['order'] = '';
  4. $this->list['filter'] = array();
  5. $this->list['additional_tables'] = array();
  6. $this->list['join_conditions'] = array();
  7. $this->list['special_select_fields'] = '';
  8. }
  9.  
  10. function list_set_limit($start, $limit) {
  11. $this->list['has_limit'] = 1;
  12. $this->list['start_limit'] = $start;
  13. $this->list['stop_limit'] = $limit;
  14. }
  15.  
  16. function list_set_order($order) {
  17. $this->list['order'] = $order;
  18. }
  19.  
  20. function list_set_filter($filter) {
  21. array_push($this->list['filter'], $filter);
  22. }
  23.  
  24. function list_set_additional_tables($tables) {
  25. array_push($this->list['additional_tables'], $tables);
  26. }
  27.  
  28. function list_set_join_conditions($condition) {
  29. array_push($this->list['join_conditions'], $condition);
  30. }
  31.  
  32. function list_set_special_select_fields($fields) {
  33. $this->list['special_select_fields'] = $fields;
  34. }
  35.  
  36. function list() {
  37. $SQL = "SELECT ";
  38.  
  39. // SET SELECT FIELDS
  40.  
  41. if ($this->list['special_select_fields']) $SQL .= $this->list['special_select_fields']." ";
  42. else $SQL .= "* ";
  43.  
  44. if (!$this->tablename) return false;
  45. $SQL .= "FROM ". $this->tablename;
  46.  
  47. // SET DB NAMES
  48. if ($this->list['additional_tables']) {
  49. foreach ($this->list['additional_tables'] as $table) {
  50. $SQL .= ", $table ";
  51. }
  52. }
  53.  
  54. // SET JOIN CONDITIONS
  55. if ($this->list['join_conditions']) {
  56. foreach ($this->list['join_conditions'] as $join) {
  57. if (!$condition) {
  58. $separator = " WHERE";
  59. $condition++;
  60. } else {
  61. $separator = " AND";
  62. }
  63.  
  64. $SQL .= "$separator $join";
  65. }
  66. }
  67.  
  68. // SET FILTER CONDITION
  69. if ($this->list['filter']) {
  70. foreach ($this->list['filter'] as $filter) {
  71. if (!$condition) {
  72. $separator = " WHERE";
  73. $condition++;
  74. } else {
  75. $separator = " AND";
  76. }
  77.  
  78. $SQL .= "$separator $filter";
  79. }
  80. }
  81.  
  82. // SET ORDER CONDITION
  83. if ($this->list['order']) $SQL .= " ORDER BY ".$this->list['order'];
  84.  
  85. // SET LIMIT CONDITION
  86. if ($this->list['has_limit']) $SQL .= " LIMIT ".$this->list['start_limit'].", ".$this->list['stop_limit'];
  87.  
  88. //echo "SQL: $SQL<br />";
  89.  
  90. $res = mysql_query($SQL, $this->dbh);
  91. if (mysql_num_rows($res) > 0) {
  92. $list = array();
  93. while ($row = mysql_fetch_assoc($res)) {
  94. array_push($list, $row);
  95. }
  96. return $list;
  97. }
  98.  
  99.  
  100. return 0;
  101. }

Report this snippet  

You need to login to post a comment.