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