Convert an integer from the database into an Array

1

I want to convert an integer from the DB into an Array, for example:

  • I have the number of deliveries registered in the DB, let's assume that the 'x' production is completed in 5 deliveries.
  • this number 5 is entered in the DB as a String, an integer.
  • When in the system the user is delivering the production, he has to inform which delivery he is producing.

Dai I would like to pull a Select from 1 to 5 (which is the number of deliveries in this example). Does anyone know how I can for PHP verify that the number 5 in the example can be read from 1 to 5 ??

    
asked by anonymous 26.12.2014 / 14:49

2 answers

1

Range , generates an array based on a range, simply enter the initial value and limit, if you need a range other than 1 then add the third parameter so the value of the array can be generated by 2 or 2.

<?php
   $inicio = 1;
   $ultimo = 5
   $arr = range($inicio, $ultimo);

   echo '<pre>';
   print_r($arr);

Output:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)
    
26.12.2014 / 15:07
0

You could illustrate with the code you are using, this would make it much easier, for me it was confusing what you want, if you could tell how the situation is now and how you want, it would be much easier.

Method 1: Change the Database!

You can change the DB to INT:

ALTER TABLE tabela MODIFY coluna INT UNSIGNED NOT NULL;

Then just give a SELECT:

SELECT * FROM tabela WHERE entregas >= 1 and entregas <= 5

Method 2: Loop!

If not, make a loop, however I do not recommend:

// Pega tudo do MySQL (SELECT entregas FROM tabela WHERE 1)
// No caso, usando o nome $SQL
if((int)$SQL['entregas'] >= 1 and (int)$SQL['entregas'] <= 5){
echo $SQL['entregas'];
}

After reading and rereading, I think your doubt may be another ...

    
26.12.2014 / 15:00