1

I have a table created in Mysql with this colums

  • secretNumber (Primary key & AI)
  • Name
  • Date
  • etc...

My idea is when I'm going to add a new item to this table, automatically generate a "secret number" but must has 3 requirements:

  • Unique number
  • Randon number everyime I add a new item
  • between 0-1000000

Tried to use funtions like uniqid(); and mt_rand(); but without success.

Dann
  • 21
  • 4
  • What is your PHP version? – Paul Spiegel Sep 30 '17 at 18:21
  • Possible duplicate of [PHP: How to generate a random, unique, alphanumeric string?](https://stackoverflow.com/questions/1846202/php-how-to-generate-a-random-unique-alphanumeric-string) – laminatefish Oct 06 '17 at 10:31
  • There are many questions already answered regarding this topic. A quick search for php unique id returned hundreds of results. Each of which are easily adaptable to suit your requirements. – laminatefish Oct 06 '17 at 10:33
  • PHP Version: 7.0.22 Yes, I tried but its not alphanumeric, what I really need its only numbers. – Dann Oct 06 '17 at 11:53
  • You should try and adapt the answers given to your problem at least. Also , you don't give any details and your question is very generic.Anyway I m giving you an answer below. – skechav Nov 02 '18 at 23:02

2 Answers2

2

you can create tmptable for between 0-1000000 when you insert to the table, use from tmptable, after than you must delete that using row from this table. or forward query but this is slowly because every time creating temp table. if you create temptable once, so it could be fast process.

CREATE TEMPORARY TABLE IF NOT EXISTS listtable(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, tmp INT NOT NULL); SET @s = CONCAT('INSERT INTO listtable (tmp) VALUES ',REPEAT('(1),',1000000),'(1)'); PREPARE stmt1 FROM @s; EXECUTE stmt1; SELECT id FROM listtable WHERE id NOT IN(select id from your table) order by rand() limit 1; DROP TABLE listtable;

sametcilli
  • 121
  • 2
0

The logic first : In order to achieve this you have to add a callback_before_insert() function in your grocery crud controllers' method. Inside this callback function you will create the random number you want, then add it to the $post_array variable and then return the $post_array back to your controllers' method (There are already examples on this at grocery cruds' official page). So, somewhere in your controller you add this :

function _create_unique_secret_number()
{
    /*Create a random secret_Number between 0 and 1000000
     * and assign it to a variable
     */
    $random_unique_secret_number = mt_rand( 0, 1000000 );

    /* Now make sure that your random secret number is not already "in use"
     * which means that the generated id is already stored in your table.
     */
    $query = $this->db->where( 'secretNumber', $random_unique_secret_number )
                      ->get_where( 'your_table_name_goes_here' );

    if( $query->num_rows() > 0 )
    {
        $query->free_result();

        // Try again in case the randomly generated number above is in use
        return $this->create_unique_secret_number();
    }

    $post_array['secretNumber'] = $random_unique_int;
    return $post_array;

}

/* And finally call the function inside your controllers' method.
 * I mean inside the method that is handling your table.
 */
$crud->callback_before_insert( array ($this, '_create_unique_secret_number')  );

You can then access the generated number in your grocery crud controller by accessing theenter code here $post_array['secretNumber'] value ..

skechav
  • 126
  • 1
  • 6