Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Search Results Page Problem


Reply

2 Posts   1246 Views

Avatar
timcole

5 March 2010 at 5:03am Community Member, 32 Posts

I have a project with a song library... a song dataobject and two related data objects, theme and subtheme. I've created a search form where the user can search using a combination of text boxes and drop downs and limited it to showing 10 songs per page. The search works - but when you click the link to move to the next page in the set of results it seems to effectively start a "blank" search - i.e. instead of moving to page 2 of results matching the users query it moves to page 2 of a result set that contains every song in the library.

Code for SongSearch_Results.ss below:

<div id="rightcol">
<% include SongLibrarySideSearch %>
$Sidebar
</div><!-- End Right Col Div -->
<div id="leftcol">
<h1>SEARCH RESULTS</h1>
<% if Results %>
      <% if Results.MoreThanOnePage %>
      <p><b>We found over 10 songs matching your search, this is page $Results.CurrentPage of $Results.TotalPages :</b></p>
      <% else %>
      <p><b>We found $Results.Count song(s) matching your search :</b></p>
      <% end_if %>
      <% control Results %>
         <h3>$Title</h3><p><b>Written by: </b>$Author<p><b>Lyrics: </b>$Lyrics.LimitWordCount(30)...</p>
         <p><a href="/song-library/showsong/$ID" class="morebtn" title="$Title"><span class="displace">$Title</span></a>
         <div class="line"></div>
         <div class="clear"></div>
      <% end_control %>
      <p><b>You can <a href="/song-library">click here</a> to start a new search.</b></p>
<% else %>
   <p>Sorry, we couldn't find any songs matching your search. Please <a href="/song-library">click here</a> to try again.</p>
<% end_if %>

<% if Results.MoreThanOnePage %>
   <div id="PageNumbers">
      <p>
         <% if Results.NotFirstPage %>
            <a class="prev" href="$Results.PrevLink" title="View the previous page">Prev</a>
         <% end_if %>
      
         <span>
             <% control Results.PaginationSummary(4) %>
               <% if CurrentBool %>
                  $PageNum
               <% else %>
                  <% if Link %>
                     <a href="$Link" title="View page number $PageNum">$PageNum</a>
                  <% else %>
                     &hellip;
                  <% end_if %>
               <% end_if %>
            <% end_control %>
         </span>
      
         <% if Results.NotLastPage %>
            <a class="next" href="$Results.NextLink" title="View the next page">Next</a>
         <% end_if %>
      </p>
   </div>
<% end_if %>
</div>
<!-- End Left Col Div --><script type="text/javascript">
   
   jQuery.noConflict();
   
   var IE = /*@cc_on!@*/false;
   
   function setSubThemeMenu() {
      myparam = document.getElementById('Form_SearchForm_SubThemes__Theme__Title').value;
      myparam = myparam.replace(/ /g, "%20");
      jQuery("#dropDownHolder").load("/song-library?theme=" + myparam + " #SubThemes__Title");
      if (myparam == "") {
         jQuery("#dropDownHolder").load("/song-library #dropDownHolder");
      }
   }
   
   if(IE){
      document.getElementById('Form_SearchForm_SubThemes__Theme__Title').attachEvent('onchange', setSubThemeMenu);
   }else{
      document.getElementById('Form_SearchForm_SubThemes__Theme__Title').addEventListener('change', setSubThemeMenu, false);
   }
   MediaPlayerUnload();

</script>

and code for songlibrary.php (including the search function itself)

class SongLibrary_Controller extends Page_Controller {
   
   public function SearchForm() {
      $context = singleton('Song')->getCustomSearchContext();
      
      $SubThemeList = Dataobject::get("Theme","","Title ASC")->toDropdownMap("Title", "Title");
      
      $fields = new FieldSet(
         new TextField("Title", "Song Title"),   
         new TextField("Lyrics", "Lyrics"),   
         new TextField("Author", "Author"),   
         new DropdownField(
            'SubThemes__Theme__Title',
            'Please Select a theme from the list below:',
            $SubThemeList, "", null, "Any Theme"
            ),
         new DropdownField(
            'dropDownHolder',
            'Please Select a detail theme from the list below:',
            array(""=>"Any Theme")
            )
         );
   
      //$fields = $context->getSearchFields();
      
      $form = new Form($this, "SearchForm",
         $fields,
         new FieldSet(
            new FormAction('doSearch','SEARCH')
         )
      );
      
      return $form;
   }

public function doSearch($data, $form) {
   
   $context = singleton('Song')->getCustomSearchContext();
   $results = $this->getResults($data);
   return $this->customise(array(
      'Results' => $results
   ))->renderWith(array('SongSearch_results', 'Page'));
}
   
   function getResults($searchCriteria = array()) {
   $start = ($this->request->getVar('start')) ? (int)$this->request->getVar('start') : 0;
   $limit = 10;
      
   $context = singleton('Song')->getCustomSearchContext();
   $query = $context->getQuery($searchCriteria, null, array('start'=>$start,'limit'=>$limit));
   $records = $context->getResults($searchCriteria, null, array('start'=>$start,'limit'=>$limit));
   if($records) {
      $records->setPageLimits($start, $limit, $query->unlimitedRowCount());
   }
      
   return $records;
}

   function ThemeSearch () {
   
         $fields = new FieldSet(
         new DropdownField(
            'SubThemes__Theme__Title',
            'Please Select a theme from the list below:',
            Dataobject::get("Theme")->toDropdownMap("Title", "Title")
            )
         );
         $actions = new FieldSet(new FormAction("doSearch", "SHOW"));
         return new Form($this, "ThemeSearch", $fields, $actions);
   }
   
   function SubThemeSearch () {
         
      if (isset($_GET['theme'])) {
         $sql = "Title = '" . $_GET['theme'] . "'";
         $theme = DataObject::get_one("Theme", $sql);
         $sql = "SubTheme.ThemeID = " . $theme->ID;
         $SubThemeList = Dataobject::get("SubTheme",$sql,"Title ASC")->toDropdownMap("Title", "Title");
         $fields = new FieldSet(
         new DropdownField(
            'SubThemes__Title',
            'Please Select a theme from the list below:',
            $SubThemeList, " ", null, "Any Theme"
            )
         );
         $actions = new FieldSet(new FormAction("doSearch", "SHOW"));
         return new Form($this, "SubThemeSearch", $fields, $actions);
      }
   }
   
   function SongDetail() {
      $newID = Director::urlParam("ID");
      if ($newID != "") {
       return DataObject::get_one("Song", "ID = $newID");
      }
}
   
}

Any help appreciated!!

Avatar
timcole

6 March 2010 at 6:16am Community Member, 32 Posts

OK, so I found my own answer:

Add $form->setFormMethod('get');
just before return $form; in the songlibrary.php and it works.

Anyone else having trouble with this sort of thing see the following thread:
http://silverstripe.org/general-questions/show/274867