Jump to content
Cengiz Deniz

sorting text field as numeric

Recommended Posts

I use pagination modul.

I have a field which has born year info. But I put the years with sign "-" for BC (Before Christ).

my selector sorts like as text. -384, -4, -412 .. it must be: -412, -384, -4

selector: $pages=$page->children("limit=60, sort=date_field");

I want to order my pages like as php natsort function. 

thank you.

 

Share this post


Link to post
Share on other sites

Is there a reason why your year field is a text field and not an integer field? If you use an integer field it will sort as you want it to.

  • Like 2

Share this post


Link to post
Share on other sites
On 10.5.2017 at 11:07 PM, Cengiz Deniz said:

I want to order my pages like as php natsort function. 

Without changing the fieldtype your only option is customizing the mysql query generated by processwire manually.

  • Like 1

Share this post


Link to post
Share on other sites
2 minutes ago, LostKobrakai said:

Without changing the fieldtype your only option is customizing the mysql query generated by processwire manually.

i think this is answer. but i dont know how can i do ?

 

21 hours ago, Robin S said:

Is there a reason why your year field is a text field and not an integer field? If you use an integer field it will sort as you want it to.

is it possible changing field type to integer in PW ?

Share this post


Link to post
Share on other sites
1 hour ago, Cengiz Deniz said:

is it possible changing field type to integer in PW ?

You cannot directly change a text field to an integer field, but you can...

1. Create a new integer field

2. Add the integer field to the template that has your text field

3. Copy the value of the text field to the integer field using the API:

$items = $pages->find("template=my_template");
foreach($items as $item) {
    $item->of(false);
    $item->my_integer_field = $item->my_text_field;
    $item->save();
}

4. Check that the values have been copied successfully, and if so remove the text field from your template and then delete the text field.

  • Like 3

Share this post


Link to post
Share on other sites
24 minutes ago, Robin S said:

You cannot directly change a text field to an integer field, but you can...

1. Create a new integer field

2. Add the integer field to the template that has your text field

3. Copy the value of the text field to the integer field using the API:


$items = $pages->find("template=my_template");
foreach($items as $item) {
    $item->of(false);
    $item->my_integer_field = $item->my_text_field;
    $item->save();
}

4. Check that the values have been copied successfully, and if so remove the text field from your template and then delete the text field.

It works now. Thank you very much. You are realy hero :) 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By MateThemes
      Hello everyone!
      I have a problem with pagination. I have following code:
      <div class="uk-section"> <?php $results = $pages->find("template=gallery, limit=10, sort=title"); $pagination = $results->renderPager(); echo $pagination; echo "<ul class='uk-pagination uk-flex-center'>"; foreach($results as $result) { echo "<li><a href='{$result->url}'>{$result->title}</a></li>"; } echo "</ul>"; echo $pagination; ?> </div> Which works fine. And this markup results in following html code:
      <div class="uk-section"> <ul class='uk-pagination uk-flex-center'> <li> <a href='/galerie-bisheriger-projekte/'>Galerie bisheriger Projekte</a> </li> <li> <a href='/galerie-bisheriger-projekte/galerie-1/'>galerie-1</a> </li> </ul> </div> But instead of "Galerie bisheriger Projekte" and "galerie-1" I want to have a number.
      And as last question, how can I add a previous and next button?
      Thanks for your help!
    • By jds43
      Hello, I have a Page Reference by template radio button field to promote a certain page (only two options), but I'd like to target the page that isn't selected. This would be used dynamically throughout the site.
      Does anyone know how I could accomplish this? Would I use something like remove() or not()?
       
    • By Liam88
      Hi all,
      Quick question as I haven't found anything from my Googling. 
      I have a blog on the site which utilises pagination. On the standard blog page I have a custom header which has featured posts.
      Below that I then have recent posts like mosts blogs.
      Now if i click to page two or three I want the header to disappear and just show a continuation of the standard posts.
      I'm not sure how to go about this so any direction would be helpful.
      Thanks
    • By stanoliver
      Good morning everyone!
      I have a growing number of posts about cars, bikes, airplanes, etc. 
      The following code (below) just works fine and returns only the posts of the category=cars as I desired together with pagination.
      In my url I have for example /categories/cars/car1 or /categories/bikes/bike1
      I do want to filter my posts not only with ... category=cars ... but also with category=bikes or category=airplanes and at best:
      If my url is /categories/bikes/ then ... category=cars ... should be overwritten or replaced by ... category=bikes ... 
      If my url is /categories/airplanes/ ... then the filter should be ... category=airplanes ... (I know a work around by creating almost identical templates where I could just change the "category=cars" part of my code but that's comes of a prize by repeating a lot of identical code and is not a good habit). In the documentation I read something about the "has_parent" selector but I could not get to work it related to the urls mentioned above.
      <?php foreach ( $results = $pages->find('id>1, template=templateblogpost, category=cars, limit=5, sort=-postdate') as $post 😞?> <!-- Blog entry --> <div class="g8-card-4 g8-margin g8-white"> <!--<img src="/g8images/bridge.jpg" alt="Norway" style="width:100%">--> <div class="g8-container"> <h3><b><?= $post->title; ?></b></h3> <h5>Datum: <span class="g8-opacity"><?= $post->postdate; ?></span></h5> </div>   <div class="g8-container"> <p><?= $post->posteditor; ?></p> <div class="g8-row"> <div class="g8-col m8 s12"> <p> <a href="<?= $post->url; ?>"><button class="g8-button g8-padding-large g8-white g8-border"><b>Details lesen &raquo;</b></button></a> </p> </div> <div class="g8-col m4 g8-hide-small"> <!--<p><span class="g8-padding-large g8-right"><b>Comments &nbsp;</b> <span class="g8-badge">2</span></span></p>--> </div> </div> </div> </div> <!-- END BLOG ENTRIES --> <?php endforeach; ?>   <? echo $results->renderPager(array( 'nextItemLabel' => "rückwärts", 'previousItemLabel' => "vorwärts")); ?>
    • By Hubris
      Hi there!
      I'm using some page reference fields to create lists of tags, categories, years, etc.. I'm able to find the pages like so:
      $pages->find("template=project, {$filter}={$page->title}"); Which dynamically does something like: 
      $pages->find("template=project, tags=Experimental"); Only if the value (the page name, like "Experimental") starts with letters. If it starts with numbers, find returns nothing.
      Why is this and how can I fix it?
×
×
  • Create New...