Jump to content

Big Numbers, when integer is not enough. What to do?


bobbit6k
 Share

Recommended Posts

Hi @bobbit6k

Welcome to the forums :)

MySQL INT (unsigned) can store up to 4.2 billion. I think ProcessWire uses signed INT for integer fields, so, you get about 2.1 billion. If you need something bigger, there is MySQL BIGINT. If you need to manipulate the data as numbers, one solution  is to create your own custom Fieldtype that extends FieldtypeInteger and uses BIGINT in its database schema. We can show you how if you wish...Otherwise, if you just need a "number", you can go with a text field.

FYI

https://dev.mysql.com/doc/refman/5.7/en/integer-types.html

https://planet.mysql.com/entry/?id=13825

 

 

 

Edited by kongondo
  • Like 3
Link to comment
Share on other sites

Storing it as text is fine if you're not performing numeric operations on DB,

But you can copy /wire/modules/Fieldtype/FieldtypeInteger.module to /site/modules and open up the file, find and change getDatabaseSchema() from

public function getDatabaseSchema(Field $field) {
    $schema = parent::getDatabaseSchema($field);
    $schema['data'] = 'int NOT NULL';
    return $schema;
}

to

public function getDatabaseSchema(Field $field) {
    $schema = parent::getDatabaseSchema($field);
    $schema['data'] = 'BIGINT NOT NULL';
    return $schema;
}

Once you refresh module list from Modules > Refresh, it'll ask you which version to use, pick the one under /site/modules

image.png.7e252739c17fcf60ac01f40ac635cbdf.png

Then create a new integer field, it will be set up as BIGINT in DB.

  • Like 2
Link to comment
Share on other sites

46 minutes ago, abdus said:

Storing it as text is fine if you're not performing numeric operations on DB,

But you can copy /wire/modules/Fieldtype/FieldtypeInteger.module to /site/modules and open up the file, find and change getDatabaseSchema() from


public function getDatabaseSchema(Field $field) {
    $schema = parent::getDatabaseSchema($field);
    $schema['data'] = 'int NOT NULL';
    return $schema;
}

to


public function getDatabaseSchema(Field $field) {
    $schema = parent::getDatabaseSchema($field);
    $schema['data'] = 'BIGINT NOT NULL';
    return $schema;
}

Once you refresh module list from Modules > Refresh, it'll ask you which version to use, pick the one under /site/modules

image.png.7e252739c17fcf60ac01f40ac635cbdf.png

Then create a new integer field, it will be set up as BIGINT in DB.

Very Very Nice and quick!

Thanks Man!

  • Like 1
Link to comment
Share on other sites

4 hours ago, abdus said:

But you can copy /wire/modules/Fieldtype/FieldtypeInteger.module to /site/modules and open up the file, find and change getDatabaseSchema()

I think there will be a bit more to it than this, because there are quite a few places in FieldtypeInteger and InputfieldInteger where the value is cast with (int), which will truncate the value of high integers depending on platform.

https://stackoverflow.com/questions/670662/whats-the-maximum-size-for-an-int-in-php/2842548#2842548

 

2 hours ago, szabesz said:

Another option might be the FieldtypeDecimal module

This one works well.

  • Like 3
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Similar Content

    • By prestoav
      Hi all,
      I could have sworn I used to be able to use the site generic 'title' field as a sub field of a repeater field. However I've tried to do this on two 3.0.165 sites recently and, while it will add the title field in the repeater field setup, it wont save the repeater title sub field's content when the repeater is used in a page template and edited.
      It;'s not a big issue but I wondered if this was a known restriction?
    • By spercy16
      I was previously using individual fields to capture and output multiple cards onto my web page but after recently learning about repeaters would prefer to use this field type, as it will be a lot more user friendly for end users to create new cards. I tried to modify my code to grab the fields from the new Repeater field(s) but am getting this error message on the page:
      Here is the code I'm currently trying to use for the cards:
      <?php // https://processwire.com/api/arrays/ // check if the array of images has items foreach($page->tall_card as $card) { $count = 0; $count++; $options = array( 'quality' => 70 ); $image = $card->image; $img = $card->image->size(550, 400, $options); $cardHeading = $card->title; $cardBody = $card->plain_paragraph; $raised = $card->raised_amount; $goal = $card->goal_amount; $link = $card->link; if ($raised == 0 ) : $percent = 0; else: $percent = $raised_amount / $goal_amount * 100; endif; if ($percent <= 5) : $percent = 0; endif; ?> <span id="card<?php echo $count?>" class="card"> <img class="cardThumb" src="<?php echo $img->url; ?>" alt="<?php echo $image->description; ?> Thumbnail" /> <div class="cardBody"> <div class="cardText"> <h2><?php echo $cardHeading; ?></h2> <?php echo $cardBody; ?> </div> <div class="progressBarSection"> <progress class="progressBar" id="progressbar<?php echo $count; ?>" value="<?php echo $percent; ?>" max="100"></progress> <span class="raisedAmount"> $<?php echo $raised; ?> usd </span> <span class="goalAmount"> $<?php echo $goal; ?> usd </span> </div> <div class="primaryBtn"> <a href="https://www.paypal.com/donate?hosted_button_id= <?php echo $link; ?> &source=url"> <button> <i class="fas fa-donate"></i> Donate </button> </a> </div> </div> </span> <?php } ?> Thanks in advance for any help!
    • By spercy16
      Coming from Perch (CMS) to Processwire, the one major downside I've found is that every place I want to insert content into my pages requires a single field (be that an image, integer, string, etc.). This can be a real pain to setup and manage if you have a lot of pages, many of them with dozens of unique fields. For example, on the Projects page of a site I'm working on right now I have to have fields for donate_amount (1-15, meaning fifteen unique fields that I have to duplicate manually), goal_amount(1-15), card_body(1-15), etc. After using Perch for a while, the way their system works is simply easier. You instead create your fields in a single PHP template (for every unique section, like a card) and their CMS imports those fields into the CMS when you choose to use that card, then you just fill in the values. You can also choose to reuse that card multiple times and each one you add gives you the several fields you need to fill in. So you can easily add your 15 cards, each with a picture, title, paragraph, and link, etc. and each one is basically given a unique ID that you can reference in your CSS files. If there's any way to make fields and templates work more like that, it would be a truly outstanding and efficient CMS. The great advantage of the way Perch is setup is that I can make a card with 10 fields, if need be, and easily duplicate that card out 40 times if I need 40 cards on the page and wouldn't need 10 fields for 40 cards (meaning in PW I would need 400 fields). If there's an easier way to do this in PW I would love to know, but as of right now, from what little I do know about the CMS 400 fields would be the only way to set that up, and that would be much more time consuming than if I could create one card (in PHP) with 10 fields that automatically added a number to the end of each field for each card (if that makes sense).
    • By spercy16
      I was hoping to be able to do this entirely in PHP but was having all kind of issues getting it to see my values as numbers instead of strings (got error messages because my PW debugging is currently set to true). I currently have about 10 cards that PW is generating that include thumbnails, descriptions, donation amounts (raised amounts and goal amounts), and donate buttons. What I was trying to do was take the goal amount divide it by the raised amount (using PHP) and simply insert that value into the Progress element's Value attribute. Sounded simple enough but I couldn't get PHP to simply divide those two fields. First I tried dividing the goal value by the raised value and inserting it into the value attribute, like so:
      <? php $raised = $page->get("raisedAmount$count"); $goal = $page->get("goalAmount$count"); $percent = $goalNum / $raisedNum; ?> <progress value="<?php echo $percent; ?>" max="100"></progress> Which gave me this error message:
      Uncaught TypeError: Unsupported operand types: string / string
      Then I tried converting the two strings to integers because apparently PHP couldn't detect they were numbers and do it, like so:
      <?php $raised = $page->get("raisedAmount$count"); $goal = $page->get("goalAmount$count"); $goalNum = (int) $goal; $raisedNum = (int) $raised; if ($raisedNum != 0) : $percent = $goalNum / $raisedNum; else: $percent = 0; endif; ?> <progress value="<?php echo $percent; ?>" max="100"></progress> but all of the progress bars remain at zero percent (not shown), even when I have values in some of the $raised variables.
      Please note, if I add this code:
      <?php echo $percent; ?> below the Progress element, it shows 0 on every single card, so the $percent is never calculated (as per the $percent = $goalNum / $raisedNum;) even though $raisedNum should not equal 0 during that iteration of the loop (the original code includes a loop, which I omitted to keep the code sections above smaller, notice the $count at the end of the $raised and $goal variable declarations). When the loop goes over "raisedAmount1" there is a value in there; however, after typecasting it to an integer ($raisedNum) the value is 0 for someodd reason instead of 40,000, which is what is in the $raisedAmount1 field in Processwire...
      I'm new to relatively new to PHP and Processwire and could really use some help on this one. Thanks in advance for any helpful replies!
    • By benbyf
      Never really got my head around field groups and it shows, In my module uninstall I think I figured out how to delete a page, then a template and it's fieldgroup so that nothing gets upset. But for fields that belong to templates that need to stoick around I'm still abit stuck to how you would remove the field and when I try I still get:
      Unable to delete field '[field name]' because it is in use by these fieldgroups: 'user'
      is there a tutorial or best method for removing added fields, pages and templates?
×
×
  • Create New...