Random row from Custom Model / Custom Table?

Support MB Custom Table Random row from Custom Model / Custom Table?

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #38792
    JohnErikssonJohnEriksson
    Participant

    Hey, sorry for my novel question...

    I have a custom model with only one field per object/row, it is for a daily quote (text), I just want to get a random quote/row from the model/custom table, not sure how to do that... Appreciate if you could please give me a quick example to work with?

    Thanks a lot
    John

    #38795
    Long NguyenLong Nguyen
    Moderator

    Hi John,

    You can follow the documentation to know how to get the field value of the custom model
    https://docs.metabox.io/extensions/mb-custom-table/#getting-field-values-for-models

    and PHP function rand() to generate a random number (model ID) https://www.php.net/manual/en/function.rand.php

    $model_id = rand(1, 15);
    // Example: returns 'completed'
    $status = \MetaBox\CustomTable\API::get_value( 'status', $model_id, 'transactions' );
    echo $status;
Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.