Jump to content

sorting text field as numeric


Cengiz Deniz
 Share

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.

 

Link to comment
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 ?

Link to comment
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
Link to comment
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 :) 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...