簡體   English   中英

只在加載頁面后才打印變量

[英]only print a variable once page is loaded

我正在尋找一種使用php和javascript的簡單方法,在頁面頂部打印一個php變量,只有在頁面加載並運行while循環后才會這樣。

基本上,我有一個遍歷我的數據庫的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;
}
?>

這顯然結束了循環。 但是我現在別無選擇,但是在此之后回顯set變量$ count,將它放在頁面的底部:

echo "<br />".$count." user's found";

在循環時不是打印表行,而是可以將它們連接到變量並稍后打印。

像這樣:

$output = '';
$i = 0;

while($row = mysql_fetch_array($result))
{
    $i++;
    $output .= ....
}

echo $i;
echo $output;

但是如果要打印出所有行,你應該使用mysql_num_rows()而不是用這種方式計算它們。

這里有很多可能性,這很快就很臟

<html>...<body>
Rows found: <span id="rowcount">still calculating</span>
<table>
....
</table>
<script language="JavaScript">
document.getElementById('rowcount').innerHTML='<?php echo $rowcount; ?>';
</script>
...
</html>

好吧,它不是很清楚,但您可以在變量中創建表,然后首先打印增量:

$i=0;
$table = '<table>';
while($row = mysql_fetch_array($result))
{
  $table .= '<tr><td>'.htmlentities($row['field']).'</td></tr>';
  $i++;
}
$table .= '</table>';

echo $i;
echo $table;

您可以使用mysql_num_rows()簡單地計算結果集中的行數嗎? 然后你可以在表開始之前設置計數:

Count: <?php echo mysql_num_rows($result) ?>
<table>...</table>

我會把邏輯與頁面的呈現分開。 因此,您應首先收集所需的所有數據,然后進行演示。

Smarty這樣的模板系統對此很有幫助。 為了快速修復,它應該足以將表存儲在變量中,然后在完成加載所有數據后打印它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM