Markob1984 Posted November 7 Share Posted November 7 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 https://forums.phpfreaks.com/topic/325371-count/ Share on other sites More sharing options...
Barand Posted November 7 Share Posted November 7 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 https://forums.phpfreaks.com/topic/325371-count/#findComment-1639489 Share on other sites More sharing options...
Markob1984 Posted November 7 Author Share Posted November 7 (edited) sorted thank you Edited November 7 by Markob1984 Quote Link to comment https://forums.phpfreaks.com/topic/325371-count/#findComment-1639490 Share on other sites More sharing options...
Barand Posted November 7 Share Posted November 7 The code you posted should show 0 if there are no pending orders. Post your actual code. Quote Link to comment https://forums.phpfreaks.com/topic/325371-count/#findComment-1639491 Share on other sites More sharing options...
Markob1984 Posted November 7 Author Share Posted November 7 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 https://forums.phpfreaks.com/topic/325371-count/#findComment-1639492 Share on other sites More sharing options...
Barand Posted November 7 Share Posted November 7 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 https://forums.phpfreaks.com/topic/325371-count/#findComment-1639495 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.