php
I try to build a Notification Popup I have for this a script what seems to work but when I add a code I am = getting the numbers 1 2 3 4 from the 4 records from this user-id
I would like to see only the total number (4)
<?php $q=mysqli_query($conn,"select * from notice where user='".$_SESSION['user']."'"); $rr=mysqli_num_rows($q); if(!$rr) { echo "<h2 style='color:red'>No any notice for You !!!</h2>"; } else { ?> <?php $i=1; while($row=mysqli_fetch_assoc($q)) { echo "<Tr>"; echo "<td>".$i."</td>"; echo "</Tr>"; $i++; } ?> </table> <?php }?>
simply add the echo out of your loop
$i=1;
while($row=mysqli_fetch_assoc($q))
{
$i++;
}
echo "<Tr>";
echo "<td>".$i."</td>";
echo "</Tr>";
Just output the $rr
variable, it contains the number of rows.
UPDATE, example added based on your code:
<?php
$q=mysqli_query($conn,"select * from notice where user='".$_SESSION['user']."'");
$rr=mysqli_num_rows($q);
if(!$rr)
{
echo "<h2 style='color:red'>No any notice for You !!!</h2>";
}
else
{
echo "<h2 style='color:green'>Total Notice for you: $rr</h2>";
}
?>
Use MySQL's COUNT function..
assuming your user
table has a column named id
.. try
$q=mysqli_query($conn,"select COUNT(`id`) as USERS from notice where....
Doing this will result in 1 result, so remove your while
and just do..
$row=mysqli_fetch_assoc($q);
echo $row['USERS'];
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.