There is such code:

<?php echo '<select size="1" name="country">'.str_replace('>'.$_SESSION['USER_COUNTRY'], 'selected>'.$_SESSION['USER_COUNTRY'], '<option value="0">Не скажу</option><option value="1">Украина</option><option value="2">Россия</option><option value="3">США</option><option value="4">Канада</option>').'</select>'; ?> 

After the selection, the information is saved in the database and everything seems to be ok, but the selector field does not show the information about the selected country already saved in the database. How can I fix it?

    1 answer 1

    Well, let's speculate ...

    $ _SESSION ['USER_COUNTRY'] will contain the value of the selected option-a (I assume that somewhere you saved it correctly ... because if not, then this is generally a task with an unknown number of unknowns) .

    Let's calculate the first arguments in your str_replace (suppose I chose Ukraine):

    '>'. $ _ SESSION ['USER_COUNTRY'] will turn into > 1 'selected>'. $ _ SESSION ['USER_COUNTRY'] will turn into selected> 1

    So you take this line here (I shortened it to two opshen to make it easier)

    <select size="1" name="country"><option value="0">Не скажу</option><option value="1">Украина</option></select> , looking for a fragment in it > 1 and change it to selected> 1 .

    Now tell yourself what went wrong? :)

    • Thank you very much! I'm just completely new. Found an error:> 1 changed to value = $ _SESSION ['USER_COUNTRY'] and selected> 1 to selected value = "'. $ _ SESSION [' USER_COUNTRY '] and it worked. Like this: str_replace (' value =" '. $ _ SESSION ['USER_COUNTRY']. '"', 'Selected value ="'. $ _ SESSION ['USER_COUNTRY']. '"', '<Option value =" 0 "> I won’t say </ option> <option value =" 1 "> Ukraine </ option> ').' </ Select > - lovo
    • Well, great :) thought works - this is important. Experience will come. - AlexandrX