Compare Explode Array with select option text

Asked

Viewed 91 times

1

How to compare the Array of explode, with the text of option. If the same, mark with Selected.

<?php
//header('Content-Type: application/json; charset=utf-8');
include 'config.php';

$rs2 = $conexao->query("SELECT * FROM tags WHERE id_subcategoria = '{$_GET['id']}' AND idioma = 'pt-br' ");

$rs3 = $conexao->query("SELECT habilidades FROM categoriasub WHERE ID_Subcategoria = '{$_GET['id']}' AND idioma = 'pt-br' ");
$row3 = $rs3->fetch_assoc();


$habilidades = explode(",", $row3['habilidades']);



while($row2 = $rs2->fetch_assoc()) {
    echo '<option value="'.$row2['ID_Tag'].'">'.$row2['tag'].'</option>';
}
?>

While result

<option value="1">PHP</option> 
<option value="2">MYSQL</option>
<option value="3">HTML</option>
<option value="4">Designer de site</option>

Array result $skills

Array
(
    [0] => PHP
    [1] =>  HTML
    [2] =>  Designer de site
)
  • In this case would be 3 options selected? You are using multiple in the select of HTML?

  • @Andersoncarloswoss Exactly. Yes I’m using the multiple.

1 answer

1


Compare the value that comes from the database with the skill array with the function in_array() if assigned $selected the selected value. Then mount the template with printf().

while($row2 = $rs2->fetch_assoc()) {
    $selected = in_array($row2['tag'],$habilidades) ? 'selected="selected"' : ''; 
    printf('<option value="%s" %s>%s</option>', $row2['ID_Tag'], $selected, $row2['tag']);
}
  • Returned so <option value="1" selected="selected">1</option><option value="2" >2</option><option value="3" >3</option><option value="4" >4</option>

  • @Tiago I corrected the code, I had put it twice $row2['ID_Tag']. Now it should work as expected.

  • I was answering with an example, but since it is the same solution, the link is: http://ideone.com/n7fLP2

  • You’re just selecting the first one. <option value="1" Selected="Selected">PHP</option><option value="2" >MYSQL</option><option value="3" >HTML</option><option value="4" >Website designer</option>``

  • 1

    @James check if there are blanks in the skill field. If there are, you should remove them with the function trim.

  • I accessed the link, there it works right, in my code only selects the first

  • I’ll do it now.

  • @James beyond the trim() commenting text uses the same box or all minuscula or all uppercase?

  • @rray It has to be standardized high or low box?

  • @James yes, if the comparison doesn’t go right.

  • @rray This link Anderson made works http://ideone.com/n7fLP2

  • @James his skills are written as?

  • @Andersoncarloswoss I’ve tried using the trim and yet here you will not.

  • @Tiago has how to show the code?

  • @Andersoncarloswoss, it’s space, I did a test taking them out of the comic, because they are separated by virgura and a spaces to incite another. Surely how I’m doing must be wrong. $habilidades = explode(",", trim($row3['habilidades']));&#xA;&#xA;&#xA;while($row2 = $rs2->fetch_assoc()) {&#xA; $selected = in_array(trim($row2['tag']), $habilidades) ? 'selected="selected"' : '';&#xA; printf('<option value="%s" %s>%s</option>', $row2['ID_Tag'], $selected, trim($row2['tag']));&#xA;}&#xA;?>

  • 1

    @James does so, http://ideone.com/UM4uNq, that the spaces will be removed. Just be careful with the text box, as rray quoted. You can use the functions strtoupper or strtolower to avoid problems.

  • @Andersoncarloswoss Ball Show, now worked :D

Show 12 more comments

Browser other questions tagged

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