Welcome Guest, Not a member yet? Register   Sign In
How to do this query?
#1

[eluser]mvdg27[/eluser]
Hi guys,

for a project I'm working on now, I have to do a database query, and I'm having difficulties to figure out how to get the correct results. Maybe someone here could shed his light on the case Smile

Ok. I have a very simple database setup:

id|username|number|donation

It's a script where people can virtually donate to a good cause. For each 'number', the organization donates 2 Euro's to one out of three causes (donation = 1, 2, or 3).

So the simplest way of doing this for each cause is:

Code:
"SELECT SUM(number * 2) AS cause1 FROM table WHERE donation = 1";

However, there an extra condition, that makes it a bit harder:

The maximum amount that one user (username) can donate is 10 Euro's. But it can be donated in different rows and to different causes.

How would you guys go about doing this?

Any advice is very much appreciated!

-Michiel
#2

[eluser]xwero[/eluser]
The most user friendly option would be that the cause select/radio button checks if the user still can donate or not. So in an ajax controller you can put this code
Code:
echo ($this->db->query('select count(*) as rows from table where donation = ?',array($input))->row()->rows < 5)?'they have enough money already':'You can give a little more';
#3

[eluser]TheFuzzy0ne[/eluser]
Method chaining works with PHP5 only.
#4

[eluser]xwero[/eluser]
it's an ugly snippet, i just wanted to show something that worked but nobody should use it for production code.
#5

[eluser]Draft[/eluser]
Hello, how can I write query with Active Recird?

I need SELECT * FROM table WHERE id = MAX(id).

Thanks.
#6

[eluser]TheFuzzy0ne[/eluser]
I'd try doing it like this:

Code:
$res = $this->db->get_where('table', 'id = MAX(id)');




Theme © iAndrew 2016 - Forum software by © MyBB