Convert an entire number from the database into an Array

Asked

Viewed 474 times

1

I want to convert an entire number from the BD into an Array, for example:

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

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

  • 4

    You are (very) confused what you want.

  • 1

    You want to add N options of <select> based on the last/highest number of deliveries?

  • Guys, thanks for all your help, but the friend’s code solved, it was really simple: <? php $start = 1; $ultimo = 5; $arr = range($start, $last); echo '<pre>'; print_r($arr); ?>

  • Mark his answer as the correct one.

2 answers

1


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

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

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

Exit:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)
  • Your code solved my problem and saved my life. God pay you in euros kkkkk thank you

  • 1

    $skills['adivinhacao']++; :D

0

You could exemplify with the very code you are using, it would make it much easier, for me got confused what you want, if you could say 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, but 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 believe your doubt may be another...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.