0

ユーザーが投稿の編集に戻ったときにチェックしたラジオボタンを保持しようとしています。これを機能させるための適切な構文に問題があります。

if ステートメントを追加する前は、すべて正常に機能していました。

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>";
}}
?>
4

3 に答える 3

0

私の知る限り、エコー内に「if」ステートメントを含めることはできません。

代わりにこれを行います:

if($_SESSION['fpi'] == $dirArray[$index])
{
   $checked ="checked";
}
else
{
   $checked = "";
}
echo "...<input type=\"radio\" name=\"fpi\" value=\"" . $dirArray[$index] ."\" $checked /></div> </div>";
于 2013-01-15T19:23:40.110 に答える
0

私はこのようにします、またあなたの引用符をきれいにしました:

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>';
    }
}
于 2013-01-15T19:24:33.323 に答える
0

このタイプのものをテンプレートに引き出すのが好きです。これにより、「エコー」または「印刷」の混乱が解消されます。さらに、これはロジックの優れた分離です (Code Igniter を参照)。それ以外は、以前のポスターが示唆したのとほぼ同じ考え方に従っています。

<?   
    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>

<?  } }   ?>
于 2013-01-15T19:45:57.517 に答える