0

I am trying to retain the radio button that was checked by the user when they return to edit posting. I am having difficulty in the proper syntax to get this to work.

Prior to adding the if statement everything worked fine.

for($index=0; $index < $indexCount; $index++)
{
if (substr("$dirArray[$index]", 0, 1) != ".")
{
echo"<div class=\"imageselect\"><br><div class=\"imageselectholder\"><img src =\"server/php/files/".$me."/medium/" . $dirArray[$index] ." \" /><br></div><div class=\"imageselecttxt\">Check to use this image</div><div class=\"imageselectcheck\"><input type=\"radio\" name=\"fpi\" value=\"" . $dirArray[$index] ."\" .if($_SESSION['fpi'] == $dirArray[$index]) \"checked=\"checked\" \"/></div> </div>";
}}
?>
user1946891
  • 955
  • 1
  • 11
  • 18

3 Answers3

0

AFAIK, You can't have an "if" statement inside an echo.

Do this instead:

if($_SESSION['fpi'] == $dirArray[$index])
{
   $checked ="checked";
}
else
{
   $checked = "";
}
echo "...<input type=\"radio\" name=\"fpi\" value=\"" . $dirArray[$index] ."\" $checked /></div> </div>";
Sylverdrag
  • 8,898
  • 5
  • 37
  • 54
0

i would do it like this, also cleaned up your quotes:

for($index = 0 ; $index<$indexCount ; $index++){
    if(substr("$dirArray[$index]",0,1)!="."){
        if($_SESSION['fpi']==$dirArray[$index]){
            $checked = ' checked="checked" ';
        }else{
            $checked = '';
        }

        echo '<div class="imageselect"><br><div class="imageselectholder">
        <img src ="server/php/files/'.$me.'/medium/'.$dirArray[$index].' " />
        <br></div>
        <div class="imageselecttxt">Check to use this image</div><div class="imageselectcheck">
        <input type="radio" name="fpi" value="'.$dirArray[$index].'" '.$checked.' "/></div> </div>';
    }
}
0

I like to pull this type of stuff out into a template. This eliminates the "echo" or "print" confusion. Plus it's a nice separation of logic (see Code Igniter). Other than that I'm pretty much following the same line of thought that the previous posters have suggested.

<?   
    for($index=0; $index < $indexCount; $index++) 
    {
        if (substr("$dirArray[$index]", 0, 1) != ".")  
        {  
            $checked = ($_SESSION['fpi'] == $dirArray[$index] ? 'checked' : '');
?>
<div class="imageselect"><br>
    <div class="imageselectholder">
        <img src ="server/php/files/<?=$me?>/medium/<?=$dirArray[$index]?>"/><br>
    </div>
    <div class="imageselecttxt">Check to use this image</div>
    <div class="imageselectcheck">
        <input type="radio" name="fpi" value="<?= $dirArray[$index] ?>" <?= $checked ?> />
    </div> 
</div>

<?  } }   ?>
ChronoFish
  • 3,589
  • 5
  • 27
  • 38
  • Note: I don't thin you need "checked=checked" I think just "checked" will suffice. Also, using single quotes (') instead of double quotes (") in your html will also clean up the echo/prints: Ex echo (""; rather than ""; – ChronoFish Jan 15 '13 at 19:50
  • checked=checked for XHTML (CHECKED only for HTML), which, based on his code, is the standard he is using –  Jan 15 '13 at 19:54