2017-03-28 6 views
-2

はSOここに私のコードです:それと変数を連結する方法php/html?

<?php 
$image = ''; 
if (getUserChar($_SESSION['user_login']) == 1) { 
    $image = 'warrior'; 
    echo '<div class="'.$image.'" stlye="top:'.$top_pos.'px;left:'.$left_pos.'px;"></div>'; 
} 
elseif (getUserChar($_SESSION['user_login']) == 2) { 
    $image = 'mage'; 
    echo '<div class="'.$image.'" stlye="top:'.$top_pos.'px;left:'.$left_pos.'px;"></div>'; 
} 
elseif (getUserChar($_SESSION['user_login']) == 3) { 
    $image = 'archer'; 
    echo '<div class="'.$image.'" stlye="top:'.$top_pos.'px;left:'.$left_pos.'px;"></div>'; 
} 
?> 

、クラスが正常に動作するようです。変数top_posに関しては、それはありません。

+2

あなたは "stlye" "スタイル" に変更してみてください – Derek

+0

は "SOここに私のコードは、" それを綴った、私はあなたがそこに何をしたか見る。 – JustCarty

+0

'$のtop_pos'と' $ left_pos') if内部の値を変更しないでください。これを1行または2行に簡略化することはできませんか?試してみてください:https://pastebin.com/cnkKPBGV – JustCarty

答えて

0

$ top_pos変数と$ left_pos変数が設定されていないか、どこかに上書きされている可能性があります。

<?php 

function getUserChar($input) { 
    return $input; 
} 

// I suspect you have not set these anywhere or are being over written somewhere 
$top_pos    = 123; 
$left_pos    = 321; 
$image     = ''; 
$_SESSION['user_login'] = 1; 

$input = getUserChar($_SESSION['user_login']); 

if ($input == 1) { 
    $image = 'warrior'; 
} elseif ($input == 2) { 
    $image = 'mage'; 
} elseif ($input == 3) { 
    $image = 'archer'; 
} 

echo '<div class="'.$image.'" style="top:'.$top_pos.'px;left:'.$left_pos.'px;"></div>'; 

http://sandbox.onlinephpfunctions.com/code/184e6935fa39e733c145441c6a24be6651f41c25

関連する問題