Posted By

lukemcr on 11/19/09


Tagged

php search user ExpressionEngine groups


Versions (?)

Search function for the User Groups module for ExpressionEngine


 / Published in: PHP
 

URL: http://lukemcreynolds.com/content/search_function_for_the_user_groups_module_for_expressionengine/

This is a simple search function for the User Groups module for ExpressionEngine. It searches groups (names and descriptions) as well as topics and topic replies. It respects group types as well - results from private groups will not show up in the topic or topic replies search results.

"groups/search.php" is below - it relies on the search form at the very top.

  1. <form id="group_search" method="post" action="{homepage}/groups/search/">
  2. <input type="text" id="group_search_box" name="group_search_box" />
  3. <input type="hidden" id="member_id" name="member_id" value="{member_id}" />
  4.  
  5. <input type="radio" name="search" value="search_discussions" id="search_discussions" checked>Discussions
  6. <input type="radio" name="search" value="search_groups" id="search_groups"> Groups<br>
  7. <input type="submit" value="Submit"/>
  8. </form>
  9.  
  10. <?php
  11.  
  12. if (isset($_POST['group_search_box']))
  13. {
  14. global $DB, $REGX;
  15.  
  16. $_POST = $REGX->xss_clean( $_POST );
  17. $_POST = $REGX->strip_quotes( $_POST );
  18.  
  19. if ($_POST['search'] == 'search_groups')
  20. {
  21. $query = $DB->query("SELECT user_group_name, user_group_description, user_group_status, user_group_type, user_group_id, user_group_url_title FROM exp_user_groups WHERE (user_group_name LIKE '%".$_POST['group_search_box']."%' OR user_group_description LIKE '%".$_POST['group_search_box']."%') AND user_group_status='open'");
  22. $num_rows=$query->num_rows;
  23.  
  24. if ( $query->num_rows == 0 )
  25. {
  26. echo "No groups could be found matching your search.";
  27. }
  28. else {
  29. echo "<p>You searched Groups for \"<strong>".$_POST['group_search_box']."</strong>\".<br />";
  30. echo "There are ".$query->num_rows." results.<br>";
  31.  
  32. foreach($query->result as $row)
  33. {
  34. ?>
  35. <a href="{homepage}/groups/<?php echo $row['user_group_url_title'];?>/<?php echo $row['user_group_id'];?>/">
  36. <?php echo $row['user_group_name'];?></a> - <?php echo $row['user_group_description'];?><br>
  37. <?php
  38. }
  39.  
  40.  
  41. }
  42. }
  43.  
  44. if ($_POST['search'] == "search_discussions")
  45. {
  46. /* Topic Search*/
  47. $query = $DB->query("SELECT exp_user_groups_topics.user_group_id, exp_user_groups_topics.topic_id, exp_user_groups_topics.subject, exp_user_groups_topics.entry_date
  48. FROM exp_user_groups, exp_user_groups_topics
  49. WHERE (
  50. exp_user_groups_topics.subject LIKE '%".$_POST['group_search_box']."%'
  51. OR exp_user_groups_topics.body LIKE '%".$_POST['group_search_box']."%'
  52. )
  53. AND exp_user_groups_topics.status = 'open'
  54. AND (
  55. exp_user_groups.user_group_type = 'public'
  56. OR exp_user_groups.user_group_type = 'moderated'
  57. )
  58. AND exp_user_groups.user_group_id = exp_user_groups_topics.user_group_id"
  59. );
  60. $num_rows=$query->num_rows;
  61.  
  62. if ( $query->num_rows == 0 )
  63. {
  64. echo "No discussion topics could be found matching your search.";
  65. }
  66. else {
  67. echo "<p>You searched Discussions topics for \"<strong>".$_POST['group_search_box']."</strong>\".<br />";
  68. echo "There are ".$query->num_rows." results.<br>";
  69.  
  70. foreach($query->result as $row)
  71. {
  72. ?>
  73. <a href="{homepage}/groups/talk/<?php echo $row['user_group_id'];?>/<?php echo $row['topic_id'];?>/">
  74. <?php echo $row['subject'];?></a> - <?php echo date("Y-m-d H:i:s", $row['entry_date']);?><br>
  75. <?php
  76. }
  77.  
  78.  
  79. }
  80. /* Reply Search */
  81. $query = $DB->query(
  82. "SELECT exp_user_groups_topics.user_group_id, exp_user_groups_replies.reply_id, exp_user_groups_replies.body, exp_user_groups_replies.entry_date, exp_user_groups_topics.subject, exp_user_groups_topics.topic_id
  83. FROM exp_user_groups, exp_user_groups_replies, exp_user_groups_topics
  84. WHERE exp_user_groups_replies.body LIKE '%".$_POST['group_search_box']."%'
  85. AND exp_user_groups_replies.status = 'open'
  86. AND (
  87. exp_user_groups.user_group_type = 'public'
  88. OR exp_user_groups.user_group_type = 'moderated'
  89. )
  90. AND exp_user_groups.user_group_id = exp_user_groups_topics.user_group_id
  91. AND exp_user_groups_topics.topic_id = exp_user_groups_replies.topic_id"
  92. );
  93. $num_rows=$query->num_rows;
  94.  
  95. if ( $query->num_rows == 0 )
  96. {
  97. echo "No replies could be found matching your search.";
  98. }
  99. else {
  100. echo "<p>You searched Discussion replies for \"<strong>".$_POST['group_search_box']."</strong>\".<br />";
  101. echo "There are ".$query->num_rows." results.<br>";
  102.  
  103. foreach($query->result as $row)
  104. {
  105. ?>
  106. <a href="{homepage}/groups/talk/<?php echo $row['user_group_id'];?>/<?php echo $row['topic_id'];?>/<?php echo $row['reply_id'];?>/" />
  107. Permalink</a> - <?php echo date("Y-m-d H:i:s", $row['entry_date']);?> in the <a href="{homepage}/groups/talk/<?php echo $row['user_group_id'];?>/<?php echo $row['topic_id'];?>/">
  108. <?php echo $row['subject'];?></a> topic.<br>
  109. <?php
  110. }
  111.  
  112.  
  113. }
  114. }
  115.  
  116. }
  117.  
  118. ?>

Report this snippet  

You need to login to post a comment.