简体   繁体   中英

only print a variable once page is loaded

I'm looking for a simple way using php and javascript, to print a php variable at the top of the page, and only once the page has loaded and run through a while loop.

Basically, I have a while loop that goes through my database, and prints out a row in a table with a users details. Each time it finds a user, it adds 1 to a variable that is declared at the beginning of the page.

This is my basic way of counting how many users have been found after a search.

I then print out the variable to inform me. The only issue, is that I can only print the variable after the loop has fiished, meaning it has to be at the bottom of my page and underneath the table. This is stupid, as I then have to scroll to the bottom of a long table to find the number.

Is there A way that once the loop has finished, and set the count as a variable, I can then have a script go back and print the variable above the table?

Many thanks for any advice you guys can offer.

Cheers, Eds

EDIT: added code example:

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

This obviously ends the loop. But I now have no choice, but to echo the set variable $count after this, putting it at the bottom of the page:

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

Instead of printing the table rows while you loop, you could concatenate them to a variable and print it later.

Like this:

$output = '';
$i = 0;

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

echo $i;
echo $output;

But if you're going to print out all rows you should use mysql_num_rows() instead of counting them this way.

Lots of possibilities here, this is quick and dirty

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

Well, it's not very clear, but you can create the table inside a variable and then print the incremental first:

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

You can simply count the number of rows in the result set using mysql_num_rows() right? Then you could set the count before the table begins:

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

I'd go the way to separate the logic from the presentation of the page. So you should first collect all data you need and then do the presentation.

A templating system like Smarty can be helpful for this. Tough for a quick fix it should be enough to just store the table inside a variable and then print it after you finished loading all the data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM