taking out the commas and sorting with php mysql

Asked

Viewed 73 times

1

I have a problem here to solve... I take from the bank the numbers that are grouped with comma. Split with explode Ex:

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

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

Now comes the question... how to list this data in a while? This 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>";
  • But these numbers are to put on labels <input> or are id’s of something you have to go to the bank to get ?

2 answers

1

If you have already used the explode, then $separate is already an array, then it is more practical to go through the array using the 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*/
}
  • Perfect!! Never been so easy and practical!! Thank you all!!

0

If it’s multiple records each with multiple numbers separated by comma 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++;
}

Browser other questions tagged

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