私は、php と javascript を使用して、ページの上部に php 変数を出力する簡単な方法を探しています。
基本的に、データベースを通過するwhileループがあり、ユーザーの詳細を含むテーブルの行を出力します。ユーザーが見つかるたびに、ページの先頭で宣言されている変数に 1 が追加されます。
これは、検索後に見つかったユーザー数をカウントする基本的な方法です。
次に、変数を出力して通知します。唯一の問題は、ループが終了した後にのみ変数を印刷できることです。つまり、ページの下部とテーブルの下にある必要があります。番号を見つけるために長いテーブルの一番下までスクロールする必要があるため、これはばかげています。
ループが終了し、カウントを変数として設定したら、スクリプトを元に戻してテーブルの上に変数を出力する方法はありますか?
皆さんが提供できるアドバイスに感謝します。
乾杯、エド
編集:コード例を追加:
<?php
$count = '0';
//Print all users with similar names
while($row = mysql_fetch_assoc($result))
{
//Start the form for each user to enable editing, along with row onhover colour
echo "<form method='post' action='editor.php'>
<tr class='tablehover'>";
//Depending on searchby, change which column is bold. Helps pick out users
if($searchby == 'username') {
echo "<td><div class='userhighlight'>".$row['Username']."</div></td>";
}
else {
echo "<td>".$row['Username']."</td>";
}
if($searchby == 'firstname') {
echo "<td><div class='userhighlight'>".$row['First Name']."</div></td>";
}
else {
echo "<td>".$row['First Name']."</td>";
}
if($searchby == 'lastname') {
echo "<td><div class='userhighlight'>".$row['Last Name']."</div></td>";
}
else {
echo "<td>".$row['Last Name']."</td>";
}
if($searchby == 'office') {
echo "<td><div class='userhighlight'>".$row['Office']."</div></td>";
}
else {
echo "<td>".$row['Office']."</td>";
}
if($searchby == 'lastlogin') {
echo "<td><div class='userhighlight'>".$row['Last_Login']."</div></td>";
}
else {
echo "<td>".$row['Last_Login']."</td>";
}
//Set ID of found user
$id = $row['User ID'];
//Create query to load user roles
$roleqry = "SELECT * FROM `site roles` WHERE `User ID`='$id'";
$roleresult = mysql_query($roleqry);
//Set $roles to gather roles
while($roles = mysql_fetch_assoc($roleresult)) {
//Echo out the rest of the user details into the table
echo "<td>".$roles['Admin']."</td>";
echo "<td>".$roles['Create User']."</td>";
echo "<td>".$roles['Edit User']."</td>";
echo "<td>".$roles['SandS']."</td>";
echo "<td>".$roles['SandS Admin']."</td>";
//Echo the hidden ID field, for editing, along with the edit button
}
echo "<td><input type='text' name='id' size='1' value='".$row['User ID']."' readonly style='visibility:hidden;width:1px;'>
<input type='submit' value='Edit'></td></tr></form>";
echo "<tr><td colspan='11'><hr /></td></tr>";
$count = $count + 1;
}
?>
これは明らかにループを終了します。しかし、今は仕方がありませんが、セット変数 $count をこの後にエコーして、ページの一番下に配置するしかありません。
echo "<br />".$count." user's found";