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.

Form Questions /

drop down filter search


Reply


4 Posts   2401 Views

Avatar
dacar

Community Member, 168 Posts

14 January 2010 at 12:31pm

Hi,

is there anybody out there who can explain, how to build a drop-down-search-filter for data objects. E.g. filtering news by year and Month.

Thanxx, Carsten.

Attached Files
Avatar
dacar

Community Member, 168 Posts

21 January 2010 at 2:02am

Hi again,

i think it is better to give some more information on this topic.

In my backend i have build a "news administration section". Every news is saved as a data object (not site tree). The last 10 News are shown on a NewsHolderPage in the frontend.

Lets guess there is a databasefield called "date":

In the frontend i want to show a search form with 2 dropdownfields. Both dropdownfields should be populated with data from the "date" field from the news table. The first one should show all years from "date" (grouped by DATE_FORMAT(date, '%Y') and the second one should show all 12 month (hardcoded).

After submitting the form i want to be redirected to the same page and show the dataobjects (news) matching the submitted Year and Month.
My silverstripe skills are still not very high. For this reason it would be a great help if someone could guide me through this problem.

Thanks a lot, Carsten.

Avatar
dacar

Community Member, 168 Posts

21 January 2010 at 12:47pm

Hi,

i think, i have solved the main problem. i have hardcode a form into the template. then i build a control that gets the creation date from the news (group by year) and the second menu is completely hardcoded (monthnames won't change ;-)).

But if i submit the formdata Silverstripe returns an XML Error: PHP Fatal error: Call to a member function XML_val() on a non-object

I think, this is something i still havent understood?! How can i access my variables correctely in the templates? Why does DataObject::get return a result (in the template) and sqlQuery not? Both queries work fine.

Hope that someone can help.

Code to get the "year" for the first selectbox:

   public    function getJahreszahl() {
   $sqlQuery = new SQLQuery();
      $sqlQuery->select = array(
      'DATE_FORMAT(News.Datum, "%Y") AS Jahr',
      'DATE_FORMAT(News.Datum, "%m") AS Monat',
      'News.ClassName AS ClassName',
      'News.ClassName AS RecordClassName',
      'News.ID AS ID'
      );
      $sqlQuery->from = array(
      "News"
      );
      $sqlQuery->groupby = array(
      "DATE_FORMAT(News.Datum, '%Y')"
      );
      $sqlQuery->orderby = "DATE_FORMAT(News.Datum, '%Y') DESC";
      $sqlQuery->limit = "10";
      $rawSQL = $sqlQuery->sql();
      $result = $sqlQuery->execute();
      $jahreszahl = new DataObjectSet();
       foreach($result as $row) {
         $jahreszahl->push(new ArrayData($row));
      }    
      return $jahreszahl;
   }

This (control) code is used after submittig the $_POST Data

   public function getNewsAll() {
      if(isset($_POST['jahr'])) {
      $sqlQuery = new SQLQuery();
      $sqlQuery->select = array(
      'News.ID AS ID',
      'News.NewsbildID AS NewsbildID',
      'News.Datum AS Datum',
      'News.Headline AS Headline',
      'News.Subheadline AS Subheadline',
      'News.Copytext AS Copytext',
      'News.Anzeigen AS Anzeigen',
      'News.ClassName AS ClassName',
      'News.ClassName AS RecordClassName',
      'Newskategorie.ID AS NewskategorienID',
      'News.NewskategorieID AS NewskategorieID'
      );
      $sqlQuery->from = array(
      "News",
      "LEFT JOIN Newskategorie ON Newskategorie.ID = NewskategorieID"
      );
      $sqlQuery->where = array(
       "DATE_FORMAT(News.Datum, '%Y') = ".$_POST['jahr']." AND DATE_FORMAT(News.Datum, '%m') = ".$_POST['monat'].""
       );
      $sqlQuery->groupby = array(
      "DATE_FORMAT(News.Datum, '%Y')"
      );
      $sqlQuery->orderby = "DATE_FORMAT(News.Datum, '%Y') DESC";
      $sqlQuery->limit = "10";
      $rawSQL = $sqlQuery->sql();
      $result = $sqlQuery->execute();
      $news = new DataObjectSet();
       foreach($result as $row) {
         $news->push(new ArrayData($row));
      }    
      return $news;
} else {
      $news = DataObject::get(
         "News",
         "Anzeigen = 1",
         "Created DESC",
         "LEFT JOIN Newskategorie ON Newskategorie.ID = NewskategorieID",
         "20");
      return $news;
      }
   }

And here is the template code:

<form id="archiv" action="{$BaseHref}{$Top.URLSegment}" method="post">
<select name="jahr" id="jahr">
<% control getJahreszahl %><option value="$Jahr">$Jahr</option><% end_control %>
</select>
<select name="monat" id="monat">
<option value="01">Januar</option>
<option value="02">Februar</option>
<option value="03">M&auml;rz</option>
<option value="04">April</option>
<option value="05">Mai</option>
<option value="06">Juni</option>
<option value="07">Juli</option>
<option value="08">August</option>
<option value="09">September</option>
<option value="10">Oktober</option>
<option value="11">November</option>
<option value="12">Dezember</option>
</select>
<input type="submit" value="suchen" />
</form>
<% control getNewsAll %>
<% if Anzeigen = 1 %>
<div id="news$ID" class="absatz">
<img src="$Newsbild.WebsiteThumbnail.URL" title="$Headline" alt="$Headline: $Copytext.NoHTML" class="newsthumb" />
<div class="contenspalte"><h2>$Headline</h2>
<h3>$Subheadline</h3>
<div class="marginalspalte">
<span class="datum">$Datum.Format(d.m.Y)</span>&nbsp;[Rubrik: <% control getKategorie(ID) %>$Top.Name ffff<% end_control %>]
</div>
<br />
$Copytext.LimitWordCount(30)
<a href="$URLSegment" title="Lesen Sie mehr in $Title" class="weiterlesen">... weiter lesen.</a>
</div>
</div>
<br /><br />
<% end_if %>
   <% end_control %>

Avatar
zenmonkey

Community Member, 528 Posts

23 January 2010 at 3:10am

This may be a case of over thinking it. Depending on how your Data data is stored you should just be able to create a function on your page controller using the DataObject::get

public function getNews() {
if(isset($_POST['jahr'])){
$query=_POST['jahr'];
return DataObject::get("News","Datum =" . $query);
} else {
return false;
}
}

I know the above query is a little basic it requires your Datum column to formates as jahr but Its just a matter of reassembling your $_POST variables into the right format for Datum. Then in your template it would simply be

<% if getNews %>
<% control getNews %>

<% end_control %>
<% end_if %>


Further more you could create the form on the Controller and populate the year drop down with
http://doc.silverstripe.org/doku.php?id=dataobjectset#mapping_for_dropdowns