Markob1984 Posted Thursday at 10:29 AM Share Posted Thursday at 10:29 AM 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">'; } ?> Quote Link to comment Share on other sites More sharing options...
Barand Posted Thursday at 10:51 AM Share Posted Thursday at 10:51 AM 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 Quote Link to comment Share on other sites More sharing options...
Markob1984 Posted Thursday at 10:58 AM Author Share Posted Thursday at 10:58 AM (edited) sorted thank you Edited Thursday at 11:01 AM by Markob1984 Quote Link to comment Share on other sites More sharing options...
Barand Posted Thursday at 11:12 AM Share Posted Thursday at 11:12 AM The code you posted should show 0 if there are no pending orders. Post your actual code. Quote Link to comment Share on other sites More sharing options...
Markob1984 Posted Thursday at 11:23 AM Author Share Posted Thursday at 11:23 AM 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)'>"; } Quote Link to comment Share on other sites More sharing options...
Barand Posted Thursday at 11:52 AM Share Posted Thursday at 11:52 AM 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.