Site Search Store Search Term

3 posts by 2 authors in: Forums > CMS Builder
Last Post: February 4, 2013   (RSS)

By KCMedia - February 4, 2013

I have a search function built and working on the site but i want to store the search terms that someone types into the search box on the site can someone help with how i would go about this, here is the search forms and the results below in the code.

                        <article class="main-content">
                            <h1>Search results for <?php echo htmlspecialchars(@$FORM['search_terms']); ?></h1>
                            <div class="main-search rel fl">
                            <form method="GET" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
                            <input type="text" name="search_terms" value="<?php echo htmlspecialchars(@$FORM['q']); ?>">
                            <button type="submit"><span class="ir">Seach</span></button>
                            </form>
                            </div><br><br>
                            <h2>Results <?php echo htmlspecialchars(@$searchDetails['pageResultsStart']); ?> to <?php echo htmlspecialchars(@$searchDetails['pageResultsEnd']); ?> of <?php echo htmlspecialchars(@$searchDetails['totalRecords']); ?>.</h2>
                            
                            <!-- show errors -->
                            <?php if ($searchDetails['invalidPageNum']): ?>
                              Results page '<?php echo $searchDetails['page']?>' not found, <a href="<?php echo $searchDetails['firstPageLink'] ?>">start over &gt;&gt;</a>.<br/>
                            <?php elseif ($searchOptions['keywords'] && $searchDetails['noRecordsFound']): ?>
                              No records matched search query!<br/>
                            <?php elseif ($searchOptions['keywords'] == ""): ?>
                              Enter a keyword to search.<br/>
                            <?php endif ?><br>
                            
                            <!-- STEP2: Display Record List -->
                              
                              <ul style="list-style-type:none; list-style:none; margin-left:0; margin-top:0">
                              <?php foreach ($searchRows as $record): ?>
                                 <li style="min-height: 100px;">
                                 <h4><a href="<?php echo $record['_link'] ?>"><?php echo $record['_title'] ?></a></h4>
                                 <span style="color:#F00; font-style:italic"><?php echo $record['_link'] ?></span><br>
                                 <?php if ($record['_summary']): ?>
                                   <?php $maxWords = 40; $words = explode(" ", $record['_summary']); $words = array_slice($words, 0, $maxWords); $summary = join(" ", $words); print $summary; ?>
                                  <?php else: ?>
                                    <br/>
                                 </li>
                                  <?php endif ?>
                              <?php endforeach ?><br><br>
                              </ul>
                            <!-- /STEP2: Display Record List -->

                            <!-- STEP3: Display Page Links (Paste anywhere below "Load Record List") -->
                              <?php if ($searchDetails['prevPage']): ?>
                                 <a href="<?php echo $searchDetails['prevPageLink'] ?>">&lt;&lt; prev</a>
                                 <?php else: ?>
                                  &lt;&lt; prev
                                 <?php endif ?>

                                    - page <?php echo $searchDetails['page'] ?> of <?php echo $searchDetails['totalPages'] ?> -

                                 <?php if ($searchDetails['nextPage']): ?>
                                 <a href="<?php echo $searchDetails['nextPageLink'] ?>">next &gt;&gt;</a>
                                 <?php else: ?>
                                  next &gt;&gt;
                                 <?php endif ?>
                            <!-- /STEP3: Display Page Links -->
                            

                        </article><!-- /main-content -->

Thanks



Craig

KC Media Solutions

www.kcmedia.biz

Hi Craig,

I've created a very simple search term save system that you could base your system on. First I created a section called searches, I've attached an image that shows the fields that I added to it. 

Next I created a simple search form based on the one you are using in the post above. Then I created a PHP if statement that detects if someone has carried out a search and stores their search term in the searches section:

<?php
//If someone has carried out a search 
 if(@$_REQUEST['saveSearch'] && @$_REQUEST['search_terms']){
//Create an array of data to be saved, and validate it.    
$saveFields = array(
      'createdDate' => date('Y-m-d H:i:s'),
      'title'       => mysql_escape($_REQUEST['search_terms'])
    );
    //Add the  terms to the searches section
    mysql_insert('searches',$saveFields);
  }

?>

  <form method="get" action="scratch.php">
    <input type="hidden" name="saveSearch" value="true" />
    <input type="text" value="<?php echo @$_REQUEST['search_terms']; ?>"  name="search_terms" />
    <input type="submit" name="submit" value="Submit" />
  </form>

So if someone has carried out a search, and there is data in the search_terms search box, then it gets saved to the searches section. 

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com
Attachments:

searches.png 3K