removing the commas and sorting with php mysql

1

I have a problem here to solve ... I take the numbers that are grouped with commas in the bank. Separate with explode Ex:

$dados = "36,38,40,42";

$separar = explode(",",$dados);
$result = $separar[0];

Now comes the doubt ... how to list these data in a while? These data will be part of inputs. Example:

<ul>
while($array = mysql_fetch_array($prod)){
$dados = $array['$dados'];
echo "
<li><input name='dados' type='radio' value='$dados'><label>$dados</label></li>";
}
</ul>";
    
asked by anonymous 03.05.2018 / 00:26

2 answers

1

If you have already used the explode, then $ separate is already an array, so it is more practical to go through the array using foreach instead of while.

foreach($separar as $item){
/*Aqui, você pode criar seus inputs, a variável item vai ter o valor do item atual*/
}
    
03.05.2018 / 00:39
0

If it is multiple records each with multiple comma-separated numbers you do so:

$mysqli = new mysqli('host','user','pass','db');

$array = array();

if($th = $mysqli->query("SELECT * FROM 'suatabela' WHERE 'sua condição for verdadeira'"){
    while($row = $th->fetch_assoc()){
        //Faço a separação do registro atual
        $v = explode(",", $row['campo']);
        $i = 0

        while($i < count($v)){
            //Então o add no array
            array_push($array, $v[$i]);
            $i++;
        }
   }
}


//Então faço o que quero
$i = 0
while ($i < count($array)) {
    echo $array[$i];
    $i++;
}
    
03.05.2018 / 01:11