Jump to content

show a total based on a column value


jonnyfortis

Recommended Posts

i have a table (cfsCustomersImages)

 

containing the columns

 

imagesID

image

custID

imageWant

 

the value of column imageWant is default "N" if the user wants and image they have a form they can check to change the value to "Y"

 

what i need to show is a value of the total of "Y" values in the column

 

for example 

 

if 6 "Y"'s are in the columns

 

6 images have been selected

 

does this make sense?

Link to comment
https://forums.phpfreaks.com/topic/279960-show-a-total-based-on-a-column-value/
Share on other sites

In this case you need to use MySQL COUNT function (I assume your RDMS is mysql).

 

The COUNT function returns the number of rows in a table. The COUNT function allows you to count all rows in a table or rows that match a particular condition.

 

So now your query should be something like this. 

SELECT COUNT(*)
FROM cfsCustomersImages
WHERE imageWant = 'Y'

What you are looking for, THIS IS SQL FIDDLE

hi, thanks for you response

 

i have tried your suggestion but and not getting the results. i have put in my workings

$colname_rsCustomer = "-1";
if (isset($_SESSION['MM_Username'])) {
  $colname_rsCustomer = $_SESSION['MM_Username'];
}
mysql_select_db($database_cfs, $cfs);
$query_rsCustomer = sprintf("SELECT * FROM cfsCustomers, cfsCustomersImages WHERE custEmail = %s AND cfsCustomersImages.custID = cfsCustomers.custID", GetSQLValueString($colname_rsCustomer, "text"));
$query_limit_rsCustomer = sprintf("%s LIMIT %d, %d", $query_rsCustomer, $startRow_rsCustomer, $maxRows_rsCustomer);
$rsCustomer = mysql_query($query_limit_rsCustomer, $cfs) or die(mysql_error());
$row_rsCustomer = mysql_fetch_assoc($rsCustomer);

mysql_select_db($database_cfs, $cfs);
$query_rsCount = sprintf("SELECT COUNT(*) FROM cfsCustomers, cfsCustomersImages WHERE cfsCustomers.custID = cfsCustomersImages.custID AND custEmail = %s AND imageWANT = 'Y'", GetSQLValueString($colname_rsCustomer, "text"));
$rsCount = mysql_query($query_rsCount, $cfs) or die(mysql_error());
$row_rsCount = mysql_fetch_assoc($rsCount);
$totalRows_rsCount = mysql_num_rows($rsCount);





and echoing out the results

<?php echo $row_rsCount['imageWant']; ?>

basically i was trying to get the results based on the logged in user 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.