tags:

views:

52

answers:

3

I have a database/store and here is my simple query :

$query_Recordset1 = "SELECT * FROM orders WHERE products = 30";

Now, we want LIVE results of how much total money we donate when people by this product. This product is around $30 dollars (which is irrelevant) but we donate $5 for every purchase to a certain cause and would like to show the total amount in dollars:

Like this: We have donated $755 to this cause or something like that!!

Anyways, here is my php. We need a $start variable, which is how much money we have donated before we implemented the database and store..

Here it is. I need a little help to get this to work out: Thanks in advance:

<?php 
  $start = 20; // how much money we started with before the database
  $limit = $start + $totalRows_Recordset1 + 5; // what we started with + the total number of rows that equal the product '30'


  echo $limit; 

?>
+4  A: 

Is this what you're looking for?

<?php 
  $query_Recordset1 = mysql_query("SELECT * FROM orders WHERE products = 30");
  $num_rows = mysql_num_rows($query_Recordset1);

  $start = 20; // how much money we started with before the database
  $limit = $start + ($num_rows * 5); // what we started with + the total number of rows that equal the product '30'

  echo $limit; 

?>
bradenkeith
Good catch. Apologies, I started going one route with it and flipped half way through. I've updated my code.
bradenkeith
I will up-vote and delete my comment then!
Buggabill
I also wonder if that other way might be better. Doing a `COUNT(*) AS thecount` and then doing a `mysql_fetch_assoc` or `mysql_fetch_array` might be faster than retrieving the whole recordset.
Buggabill
PERFECT!! THANKS GUYS!!!
eberswine
Would depend on the size of the database, but it very well may be. eberswine will have to make that call. However, both will work. Thanks @Buggabill btw
bradenkeith
@eberswine Please accept one of the answers by clicking the checkmark beside it. It helps others with the same question find the answer, and keeps people willing to answer your question down the road. Thx.
bradenkeith
A: 
<?php 
  $start = 20; // how much money we started with before the database
  $totalRows_Recordset1 = mysql_num_rows($RecordSet1);
  $limit = $start + $totalRows_Recordset1 * 5; // what we started with + the total number of rows that equal the product '30'

  echo $limit; 

?>
Femaref
+1  A: 

Why not just change your SQL statement to get the value you're looking for?

SELECT (Count(*) * 5) + 20 FROM orders WHERE products = 30;
Rachel