3

I making a language loop inside a structure that return the result in radio and i need to return the last select that user selected before inside this foreach ... in this code i get duplicated result the checked one and unchecked one. How can I show the result checked without duplicated ?

<?php
    // to select the language that insert in table "x_language" for this user
    // the result is 1 in this case
    $usuario_id = $this->session->userdata('usuario_id');
    $usuario_id = $this->cake->get_current_language_to_user($usuario_id);
    // to get all languages from table "y_languages"
    $paises = $this->cake->get_paises();
    $i = 0;
    foreach($paises as $pais){
        // here i get to checked the "y_languages" -> "$pais->nome" that have the same result on "x_language"
        if($usuario_id['nome'] == $pais->nome){echo "<div class='radio iradio'><label><input type='radio' checked='checked' name='languageselected' value='".$pais->id_pais."'> ".$pais->nome."</label></div>"; }
        ++$i;
        echo "<div class='radio iradio'><label><input type='radio' name='languageselected' value='".$pais->id_pais."'> ".$pais->nome."</label></div>";

        if($i%17===0) { 
            echo "</div></div><div class='col-lg-2'><div class='radio iradio'>";
        }
    } 
?>

1 Answer 1

0

May this will work:

<?php
    // to select the language that insert in table "x_language" for this user
    // the result is 1 in this case
    $usuario_id = $this->session->userdata('usuario_id');
    $usuario_id = $this->cake->get_current_language_to_user($usuario_id);
    // to get all languages from table "y_languages"
    $paises = $this->cake->get_paises();
    $i = 0;
    //Getting unique from array
    $paises_unq = array_unique($paises[0]);
    foreach($paises_unq as $pais){
        // here i get to checked the "y_languages" -> "$pais->nome" that have the same result on "x_language"
        if($usuario_id['nome'] == $pais->nome){echo "<div class='radio iradio'><label><input type='radio' checked='checked' name='languageselected' value='".$pais->id_pais."'> ".$pais->nome."</label></div>"; }
        ++$i;
        echo "<div class='radio iradio'><label><input type='radio' name='languageselected' value='".$pais->id_pais."'> ".$pais->nome."</label></div>";

        if($i%17===0) { 
            echo "</div></div><div class='col-lg-2'><div class='radio iradio'>";
        }
    } 
?>
Sign up to request clarification or add additional context in comments.

9 Comments

@Berney Stinson Message: Object of class stdClass could not be converted to string
this is result of var_dump($paises); array(99) { [0]=> object(stdClass)#27 (3) { ["id_pais"]=> string(1) "1" ["codigo"]=> string(5) "so_SO" ["nome"]=> string(11) "Af-Soomaali" }
There are no any repeated data I think
yes .. because the repeated data came from other array when i made if condition!!!! :) .. i think i have to merge the two arrays in one array and make after that array_unique. do you think is possible ? if yes how can be ?
See this for solution LINK
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.