$value ) { if ( $c > $key ) { $query = 'SELECT SUM(IF(payment_type="check", amount, 0)) AS "check", SUM(IF(payment_type="credit", amount, 0)) AS "credit", SUM(IF(payment_type="cash", amount, 0)) AS "cash" FROM transaction_log WHERE paid=1 AND transaction_id <' . $deposit[$key] . ' AND transaction_id >' . $deposit[$key + 1] . ';'; $sql = mysql_query($query, $YBDB) or die(mysql_error()); $result = mysql_fetch_assoc($sql); $result_obj[$deposit[$key]] = $result; } else { $query = 'SELECT SUM(IF(payment_type="check", amount, 0)) AS "check", SUM(IF(payment_type="credit", amount, 0)) AS "credit", SUM(IF(payment_type="cash", amount, 0)) AS "cash" FROM transaction_log WHERE paid=1 AND transaction_id <' . $deposit[$key] . ';'; $sql = mysql_query($query, $YBDB) or die(mysql_error()); $result = mysql_fetch_assoc($sql); $result_obj[$deposit[$key]] = $result; } } echo json_encode($result_obj); } else { // more deposits than visible $limit = $visible_count + 1; $query = 'SELECT transaction_id FROM transaction_log WHERE transaction_type="Deposit" ORDER BY transaction_id DESC LIMIT ' . $limit . ';'; $sql = mysql_query($query, $YBDB) or die(mysql_error()); while ( $result = mysql_fetch_assoc($sql) ) { $transaction_id[] = $result['transaction_id']; } foreach ( $transaction_id as $key => $value ) { if ($key <= $c) { $query = 'SELECT SUM(IF(payment_type="check", amount, 0)) AS "check", SUM(IF(payment_type="credit", amount, 0)) AS "credit", SUM(IF(payment_type="cash", amount, 0)) AS "cash" FROM transaction_log WHERE paid=1 AND transaction_id <' . $transaction_id[$key] . ' AND transaction_id >' . $transaction_id[$key + 1] . ';'; $sql = mysql_query($query, $YBDB) or die(mysql_error()); $result = mysql_fetch_assoc($sql); $result_obj[$transaction_id[$key]] = $result; } } // foreach echo json_encode($result_obj); } // end else for invisibles } ?>