Display one's search query

7 posts by 2 authors in: Forums > CMS Builder
Last Post: August 18, 2011   (RSS)

By Hansaardappel - August 17, 2011

This may be very easy, but I have no idea how to accomplish it.

I want my search page to look like this:

Search Results for "The search query they entered":

result1
result2 etc...

I got it working as I want, but how do I display the search query?

Thanks!

Re: [Hansaardappel] Display one's search query

By Jason - August 17, 2011

Hi,

Are they entering just 1 search term, or multiple terms? Can you show us the form that they would be filling out to be searching?

Thanks
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Hansaardappel] Display one's search query

By Hansaardappel - August 17, 2011

Figured it out already :)

Re: [Hansaardappel] Display one's search query

By Hansaardappel - August 17, 2011 - edited: August 17, 2011

Ran into a new problem and I found out it has to do with my solution to the previous problem I had. To display the search query I did the following:

form code:
<form method="post" action="search.php">
<input type="text" name="tags,content,title_keyword" id="textfield" value="Search..." />
<button class="btn" title="Submit Search" name="submit"></button>


And in my search.php page I added the following line:
$searchquery = $_POST["tags,content,title_keyword"];


To display the results on search.php I use this code:
<h3>Search Results for "<?php echo $searchquery ?>"</h3><p></p>
<?php foreach ($articlesRecords as $record): ?>
Title: <?php echo $record['title'] ?><br/>
Category: <?php echo $numToName[$record['category']] ?><br/>
Tags: <?php echo $record['tags'] ?><br/>
_link : <a href="<?php echo $record['_link'] ?>"><?php echo $record['_link'] ?></a><br/>
<hr/>
<?php endforeach; ?>

<?php if (!$articlesRecords): ?>
No records were found!<br/><br/>
<?php endif ?>




The result is good, except for the category. The notice I get:

Notice: Undefined index: 2 in C:\xampp\htdocs\xampp\cmsbuilder\search.php on line 173

I found out that this has to do with the fact that I use $numToName, because I do get the result 2 (category 2) when I remove the $numToName part. Normally the $numToName function works fine though and it ALSO does in search.php if I delete $searchquery = $_POST["tags,content,title_keyword"];

Could anyone explain to me why the $numToName function doesn't work when I add the $searchquery = $_POST["tags,content,title_keyword"];
part? And even more important, does anyone have any suggestions on how to fix it?

Thanks again for your help :)

EDIT:
This is the code I use for $numToName at the top of my page:

$numToName=array();

foreach($categoryRecords as $record){
$numToName[$record['num']]=$record['name'];
}

Re: [Hansaardappel] Display one's search query

By Jason - August 18, 2011

Hi,

What must be happening is that your query for $categoryRecords must not be returning a value for record number 2. The most likely reason for this is that your query for $categoryRecords is being affected by your search. You would want to return all records in that query.

You can try using the:
'allowSearch' => false,

option in the getRecords call for $categoryRecords.

If you are using CMS Builder version 2.04 or above, you can skip the $numToName code all together and use the ':label' pseudo field. The code would look like this:

Category: <?php echo $record['category:label'] ?><br/>

Hope this helps
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] Display one's search query

By Hansaardappel - August 18, 2011

Worked perfect, thanks!