Jump to content

count


Markob1984

Recommended Posts

hello

I'm using the below code to count the rows then display them but  its showing $rowcount instead of the value from the database

 

  <?php
        
        if ($user_level !=1){
        } else {
    
        $sql = "SELECT * from orders WHERE status='Pending'";
        
        if ($result = mysqli_query($con, $sql)){
            $rowcount = mysqli_num_rows($result);
            
        echo '<input type="button" class="button" value="$rowcount">';
        
        }
        
        ?>

 

Link to comment
Share on other sites

To display the value of a variable inside a string, the string needs to enclosed in double-quotes.

You need

echo "<input type='button' class='button' value='$rowcount'>";
        
        

or

echo "<input type=\"button\" class=\"button\" value=\"$rowcount\">";
        
        

If you are only interested in the number of records, and not the individual records, it is far more efficient to get SQL to count them and return the total intead of returning the data then counting them. EG

SELECT COUNT(*) as rowcount FROM orders WHERE status='Pending'

or

SELECT SUM(status='Pending') as rowcount from orders 

 

Link to comment
Share on other sites

Sorry it did.

i need to do it like this show it shows different ones for Pending , Paid 

 

  $sql = "SELECT * from orders WHERE status='Pending'";
        $sql2 = "SELECT * from orders WHERE status='Paid'";
        
        if ($result = mysqli_query($con, $sql)){
            $rowcount = mysqli_num_rows($result);
            
        echo "<input type='button' class='button' value='Pending Payments ($rowcount)'>";
        
        }
        
         if ($result2 = mysqli_query($con, $sql2)){
            $rowcount2 = mysqli_num_rows($result2);
            
        echo "<input type='button' class='button' value='Pending Payments ($rowcount2)'>";
        
        }

 

Link to comment
Share on other sites

You only need one query. For example

TABLE : product
+----+-------------+--------------+--------+
| id | productName | category     | status |
+----+-------------+--------------+--------+
|  1 | Room 1      | Guestroom    | Active |
|  2 | Room 2      | Guestroom    | Active |
|  3 | Room 3      | Guestroom    | Active |
|  4 | Room 4      | Guestroom    | Active |
|  5 | Function 1  | Functionroom | NULL   |
+----+-------------+--------------+--------+

code

$sql = "SELECT SUM(status='Pending')as pending
             , SUM(status='Active') as active
        FROM product";
        
$result = mysqli_query($con, $sql);
$row = mysqli_fetch_assoc($result);

echo "Pending : <input type=\"button\" class=\"button\" value=\"{$row['pending']}\">
Active : <input type=\"button\" class=\"button\" value=\"{$row['active']}\">
";

output

image.png.35977fa47efb7bb8cd0394b10086c43c.png

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.