2016-04-18 14 views
0

PHPの出力に問題があります。印刷しようとしています: ようこそ! 管理者 テスター番号:3 [ログアウトとコメント]ボタンの下にあります。 しかし、最初にボタンとテキストを出力します。PHPの出力が間違った順序で出力される

Output result

コード:

<?php 
 
session_start(); 
 
if (isset($_SESSION['id'])) { 
 
\t // session variables into local variables. 
 
\t $id = $_SESSION['id']; 
 
\t $username = $_SESSION['username']; 
 
\t $result = "Welcome back! <br>".$username. "<br> You are tester number: ".$id; 
 
\t echo ' <button class="btn" type="button" onclick=window.parent.location.href="logout.php" target="_parent">Log out</button> 
 
\t <button class="btn" type="button" onclick=window.parent.location.href="blog/post.php" target="_parent">Comment</button> 
 
\t '; 
 

 
\t } else { 
 
\t \t $result = "You are not logged in yet"; 
 

 
\t } 
 

 
?> 
 
<?php 
 
echo $result; 
 
?> 
 
<title>Welcome - <?php echo $username ;?></title>

+0

あなたは結果の前にボタンをエコーし​​ています... 'echo $ result;'の下にあるボタンを使ってエコーを入れてください – Daan

+0

@Daanが言っていることはまさに 'echo'をこれに変更してください:' $ result。= ' ' – Boratzan

+0

私はそれを別の方法で切り替えようとしましたが、それはまだ同じです。 – user6184639

答えて

0

あなたはウェルカムテキストをエコーする前にボタンのHTML部分をエコー。

あなたは何ができるか:あなたは、ボタン、次に$結果をエコーされ

$result = "Welcome back! <br>".$username. "<br> You are tester number: ".$id; 
$result .= ' <button class="btn" type="button" onclick=window.parent.location.href="logout.php" target="_parent">Log out</button> 
<button class="btn" type="button" onclick=window.parent.location.href="blog/post.php" target="_parent">Comment</button> 
'; 
+0

ありがとうございました。 – user6184639

+1

@ user6184639「遅延エコー」(変数を変数に格納して後でエコーする)をミックスし、文字列を直接エコーするときは注意が必要です。それがあなたの問題でした。あなたは1つを選択してそれに固執する必要があります: – peuh

+0

また、問題が解決した場合は、回答を受け入れたものとしてマークする必要があります – peuh

0

...次の操作を行います。

<?php 
session_start(); 
if (isset($_SESSION['id'])) { 
    // session variables into local variables. 
    $id = $_SESSION['id']; 
    $username = $_SESSION['username']; 
    $result = "Welcome back! <br>".$username. "<br> You are tester number: ".$id."</br>"; 
    $result.=' <button class="btn" type="button" onclick=window.parent.location.href="logout.php" target="_parent">Log out</button><button class="btn" type="button" onclick=window.parent.location.href="blog/post.php" target="_parent">Comment</button>'; 

?> 
<?php 
    echo $result; 
?> 
<title>Welcome - <?php echo $username ;?></title> 

はそれがお役に立てば幸いです。

関連する問題