私はソーシャルメディアサイトを作成しようとしています。 4行目のonclick='func_cmnt_open(".$row['post_ID'].")
には、各エントリに固有のdivが表示されます。それは構文エラーのように思えるが、私はそれを絞り込むことができないPHPやjavascriptの場合。PHPを介して文字列としてMySQLデータをJavaScriptに渡す
6行目では、id属性がcomment".$row['post_ID']."
に設定されているため、各divがwhileループ反復から一意になりました。
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo "<h4>".$row["username"]." said ".$row["cm_text"]." at ".$row["time"]." comment".$row['post_ID']."</h4>";
echo "<button class='w3-button w3-teal w3-xlarge' onclick='func_cmnt_open(".$row['post_ID'].")'>☰</button>";
echo "<button class='w3-button w3-teal w3-xlarge' onclick='func_like()' id='like'>☰</button>";
echo "<div class='w3-black' id='comment".$row['post_ID']."' style='display: none'>";
echo " <div class='w3-container''>";
echo " <h4>";
echo " ///code was here ";
echo " </h4>";
echo " <button class='w3-button w3-grey w3-xlarge' onclick='document.getElementById('id02').style.display='block''>☰</button>";
echo " <div id='id02' class='w3-modal'>";
echo " <div class='w3-modal-content w3-card-4'>";
echo " <header class='w3-container w3-teal'> ";
echo " <span onclick='document.getElementById('id02').style.display='none'' class='w3-button w3-display-topright'>×</span>";
echo " ///code was here";
echo " </header>";
echo " <div class='w3-container'>";
echo " <form class='w3-container' action='home.php' method='GET'>";
echo " <p><input type='text' class='w3-input' name='post_comment' placeholder='Whats happening there...'/></p>";
echo " <p><button class='w3-button w3-black'>Post</button></p>";
echo " </form>";
echo " </div>";
echo " <footer class='w3-container w3-teal'>";
echo " <p>nothing here</p>";
echo " </footer>";
echo " </div>";
echo " </div>";
echo " </div>";
echo "</div>";
}
}
JAVASCRIPT:私がdiv要素の一意のIDを取得するには、「コメント」や「NUM」を結合するために、文字列の連結を使用してみました 。それはどちらもうまくいかなかった。
function func_cmnt_open(num) {
if (document.getElementById('comment'+num.tostring()).style.display == "none")
{
document.getElementById('comment'+num.tostring()).style.width = "100%";
document.getElementById('comment'+num.tostring()).style.display = "block";
}
else
{
document.getElementById('comment'+num.tostring()).style.width = "0%";
document.getElementById('comment'+num.tostring()).style.display = "none";
}
}
私はこれのわからないんだけど、それを行うための最も効率的な方法です。同じ出力を達成するための他の方法がある場合はアドバイスをお願いします。
あなたが知っているのは、複数行の文字列を正しく処理できることです。 'echo'を1回呼び出すだけですべてを行うことができます。 JavaScriptはできませんが、PHPはできます。 – ArtisticPhoenix
はポストID番号または文字列ですか? – ArtisticPhoenix
post_IDは整数です。データベースから。その自動化された。 –