仅在加载页面后打印变量


only print a variable once page is loaded

我正在寻找一种使用 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";
与其在

循环时打印表行,不如将它们连接到一个变量并在以后打印。

喜欢这个:

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

但是,如果您要打印出所有行,则应使用 mysql_num_rows() 而不是以这种方式计算它们。

您可以使用mysql_num_rows()简单地计算结果集中的行数,对吗?然后,您可以在表开始之前设置计数:

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

这里有很多可能性,这既快速又肮脏

<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;

我会把逻辑与页面的呈现分开。因此,您应该首先收集所需的所有数据,然后进行演示。

像 Smarty 这样的模板系统可以对此有所帮助。很难快速修复,只需将表存储在变量中,然后在完成加载所有数据后打印它就足够了。