Return to Snippet

Revision: 20589
at November 19, 2009 14:32 by lukemcr


Updated Code
<form id="group_search" method="post" action="{homepage}/groups/search/">
<input type="text" id="group_search_box" name="group_search_box" />
<input type="hidden" id="member_id" name="member_id" value="{member_id}" />

<input type="radio" name="search" value="search_discussions" id="search_discussions" checked>Discussions
<input type="radio" name="search" value="search_groups" id="search_groups"> Groups<br>
<input type="submit" value="Submit"/>
</form>

<?php

if (isset($_POST['group_search_box'])) 
{
		global $DB, $REGX;

		$_POST = $REGX->xss_clean( $_POST );
		$_POST = $REGX->strip_quotes( $_POST );

	if ($_POST['search'] == 'search_groups')
	{
		$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'");
		$num_rows=$query->num_rows;

			if ( $query->num_rows == 0 )
				    {
				      echo "No groups could be found matching your search.";
				    }
			else {
				echo "<p>You searched Groups for \"<strong>".$_POST['group_search_box']."</strong>\".<br />";
				echo "There are ".$query->num_rows." results.<br>";

				foreach($query->result as $row)
					  {
					      ?>
					      <a href="{homepage}/groups/<?php echo $row['user_group_url_title'];?>/<?php echo $row['user_group_id'];?>/">
					      <?php echo $row['user_group_name'];?></a> - <?php echo $row['user_group_description'];?><br>
					      <?php
					  }


			      }
	}

 	if ($_POST['search'] == "search_discussions")
 	{
/* Topic Search*/
		$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
				      FROM exp_user_groups, exp_user_groups_topics
				      WHERE (
						  exp_user_groups_topics.subject LIKE '%".$_POST['group_search_box']."%'
						  OR exp_user_groups_topics.body LIKE '%".$_POST['group_search_box']."%'
					    )
				      AND exp_user_groups_topics.status = 'open'
				      AND (
					      exp_user_groups.user_group_type = 'public'
					      OR exp_user_groups.user_group_type = 'moderated'
					  )
				      AND exp_user_groups.user_group_id = exp_user_groups_topics.user_group_id"
				   );
		$num_rows=$query->num_rows;

			if ( $query->num_rows == 0 )
				    {
				      echo "No discussion topics could be found matching your search.";
				    }
			else {
				echo "<p>You searched Discussions topics for \"<strong>".$_POST['group_search_box']."</strong>\".<br />";
				echo "There are ".$query->num_rows." results.<br>";

				foreach($query->result as $row)
					  {
					      ?>
					      <a href="{homepage}/groups/talk/<?php echo $row['user_group_id'];?>/<?php echo $row['topic_id'];?>/">
					      <?php echo $row['subject'];?></a> - <?php echo date("Y-m-d H:i:s", $row['entry_date']);?><br>
					      <?php
					  }


			      }
/* Reply Search */
		$query = $DB->query(
				"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
				FROM exp_user_groups, exp_user_groups_replies, exp_user_groups_topics
				WHERE exp_user_groups_replies.body LIKE '%".$_POST['group_search_box']."%'
				AND exp_user_groups_replies.status = 'open'
				AND (
					  exp_user_groups.user_group_type = 'public'
					  OR exp_user_groups.user_group_type = 'moderated'
				    )
				AND exp_user_groups.user_group_id = exp_user_groups_topics.user_group_id
				AND exp_user_groups_topics.topic_id = exp_user_groups_replies.topic_id"
				   );
		$num_rows=$query->num_rows;

			if ( $query->num_rows == 0 )
				    {
				      echo "No replies could be found matching your search.";
				    }
			else {
				echo "<p>You searched Discussion replies for \"<strong>".$_POST['group_search_box']."</strong>\".<br />";
				echo "There are ".$query->num_rows." results.<br>";

				foreach($query->result as $row)
					  {
					      ?>
					      <a href="{homepage}/groups/talk/<?php echo $row['user_group_id'];?>/<?php echo $row['topic_id'];?>/<?php echo $row['reply_id'];?>/" />
					      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'];?>/">
					      <?php echo $row['subject'];?></a> topic.<br>
					      <?php
					  }


			      }
 	}

}

?>

Revision: 20588
at November 19, 2009 14:30 by lukemcr


Initial Code
<?php

// print_r($_POST);

if (isset($_POST['group_search_box'])) 
{
		global $DB, $REGX;

		$_POST = $REGX->xss_clean( $_POST );
		$_POST = $REGX->strip_quotes( $_POST );

	if ($_POST['search'] == 'search_groups')
	{
		$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'");
		$num_rows=$query->num_rows;

			if ( $query->num_rows == 0 )
				    {
				      echo "No groups could be found matching your search.";
				    }
			else {
				echo "<p>You searched Groups for \"<strong>".$_POST['group_search_box']."</strong>\".<br />";
				echo "There are ".$query->num_rows." results.<br>";

				foreach($query->result as $row)
					  {
					      ?>
					      <a href="{homepage}/groups/<?php echo $row['user_group_url_title'];?>/<?php echo $row['user_group_id'];?>/">
					      <?php echo $row['user_group_name'];?></a> - <?php echo $row['user_group_description'];?><br>
					      <?php
					  }


			      }
	}

 	if ($_POST['search'] == "search_discussions")
 	{
/* Topic Search*/
		$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
				      FROM exp_user_groups, exp_user_groups_topics
				      WHERE (
						  exp_user_groups_topics.subject LIKE '%".$_POST['group_search_box']."%'
						  OR exp_user_groups_topics.body LIKE '%".$_POST['group_search_box']."%'
					    )
				      AND exp_user_groups_topics.status = 'open'
				      AND (
					      exp_user_groups.user_group_type = 'public'
					      OR exp_user_groups.user_group_type = 'moderated'
					  )
				      AND exp_user_groups.user_group_id = exp_user_groups_topics.user_group_id"
				   );
		$num_rows=$query->num_rows;

			if ( $query->num_rows == 0 )
				    {
				      echo "No discussion topics could be found matching your search.";
				    }
			else {
				echo "<p>You searched Discussions topics for \"<strong>".$_POST['group_search_box']."</strong>\".<br />";
				echo "There are ".$query->num_rows." results.<br>";

				foreach($query->result as $row)
					  {
					      ?>
					      <a href="{homepage}/groups/talk/<?php echo $row['user_group_id'];?>/<?php echo $row['topic_id'];?>/">
					      <?php echo $row['subject'];?></a> - <?php echo date("Y-m-d H:i:s", $row['entry_date']);?><br>
					      <?php
					  }


			      }
/* Reply Search */
		$query = $DB->query(
				"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
				FROM exp_user_groups, exp_user_groups_replies, exp_user_groups_topics
				WHERE exp_user_groups_replies.body LIKE '%".$_POST['group_search_box']."%'
				AND exp_user_groups_replies.status = 'open'
				AND (
					  exp_user_groups.user_group_type = 'public'
					  OR exp_user_groups.user_group_type = 'moderated'
				    )
				AND exp_user_groups.user_group_id = exp_user_groups_topics.user_group_id
				AND exp_user_groups_topics.topic_id = exp_user_groups_replies.topic_id"
				   );
		$num_rows=$query->num_rows;

			if ( $query->num_rows == 0 )
				    {
				      echo "No replies could be found matching your search.";
				    }
			else {
				echo "<p>You searched Discussion replies for \"<strong>".$_POST['group_search_box']."</strong>\".<br />";
				echo "There are ".$query->num_rows." results.<br>";

				foreach($query->result as $row)
					  {
					      ?>
					      <a href="{homepage}/groups/talk/<?php echo $row['user_group_id'];?>/<?php echo $row['topic_id'];?>/<?php echo $row['reply_id'];?>/" />
					      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'];?>/">
					      <?php echo $row['subject'];?></a> topic.<br>
					      <?php
					  }


			      }
 	}

}

?>

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

Initial Description
This is a simple search function for the [User Groups](http://www.dabbledoo.com/design/docs/User-Groups-Module/) 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.

Initial Title
Search function for the User Groups module for ExpressionEngine

Initial Tags
php, search, user

Initial Language
PHP