View Full Version : Can someone break down this code for me please?
I know what it's supposed to do, and I know the outcome, but I'm having trouble determining what all the values are and why.
$this->set('open', "IF(votenum >= 5 AND votetotal/votenum <= 2, 0, IF(votenum >= 5 AND votetotal/votenum >= 3 AND open=0, 1, open))", false);
This little plugin was written for me by Kirby/Andreas, but I'm sure he's pretty busy right now. So if any of you have a minute...
Why the double quotes? What are the zeros for? What does the false at the end mean?
Thanks for the help.
Wired1
10-26-2005, 03:42 PM
Here's a basic pseudocode breakdown:
$this->set('open', "BINARY SWITCH_1", false);
BINARY_SWITCH_1 = IF(votenum >= 5 AND votetotal/votenum <= 2, 0, BINARY_SWITCH_2)
BINARY_SWITCH_2 = IF(votenum >= 5 AND votetotal/votenum >= 3 AND open=0, 1, open)
The first part of the switches seems to relate to a voting schema.
For #2, if there's more than 5 votes with an average vote that is equal to or greater than 3, AND open = 0, then that part is true. Dunno what the 1, open means.
For # 1, there's more than 5 votes with an average vote that is equal to or less than 2, then that part is true. Dunno what the rest means.
Is that the whole plugin? Which plugin hook does it use?
Paul M
10-26-2005, 04:03 PM
Why not just ask Andreas ?
Why not just ask Andreas ?
I could, but I bothered him enough to get this plugin written. Thought maybe someone else could help me out, as I'd like to add more to it but am failing to understand it.
btw, this is a 'thread rating moderation hack' It allows the users to vote crappy threads closed and good threads open...
Wired1
10-26-2005, 06:49 PM
Makes more sense now, but I dunno why the if statement has 3 parts, unless it's another way to do a x ? y : z
Alan @ CIT
10-26-2005, 07:59 PM
The IF() in the original code is an SQL if(), not a PHP if().
The format for SQL if() can be found here: http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html
What the first IF() statement is saying is:
if 'votenum' is greater than or equal to '5' and 'votetotal' divded by 'votenum' is less than or equal to '2', then return '0'
The second one is saying:
if 'votenum' is greater than or equal to 5 and 'votetotal' devided by 'votenum' is greater than or equal to '3', then return '1', otherwise return 'open' (which in this case is 0)
As for the double quotes, they are there because the Jelsoft code standards say that all SQL should be enclosed in double quotes.
The 'false' at the end of the set() method indicates that the data being passed (in this case the SQL statement) doesn't need to be cleaned by vBulletin (characters converted, etc)
Look up the set() method in class_dm.php for more info on it's arguments.
One more question. How do I determine what $this refers to? I'm guessing it just refers to the active table (maybe?), but how do I know what that is?
Thanks for all the help guys.
Alan @ CIT
10-27-2005, 12:16 PM
The plugin that you posted is run within a PHP class. $this, refers to that class.
For more info on Object Orientated PHP, take a look at http://www.spoono.com/php/tutorials/tutorial.php?id=27 or http://www.php.net/manual/en/language.oop.php
The plugin that you posted is run within a PHP class. $this, refers to that class.
For more info on Object Orientated PHP, take a look at http://www.spoono.com/php/tutorials/tutorial.php?id=27 or http://www.php.net/manual/en/language.oop.php
Very helpful. Thank you.
How would I wrap that SQL IF statement inside a PHP contitional?
if ("IF(votenum >= 5 AND votetotal/votenum <= 2")
{
stuff
}
I doubt that is correct, but it's my only guess...
edit: What about this?
if ("IF(votenum >= 5 AND votetotal/votenum <= 2, 1, 0)" == '1')
vBulletin® v3.8.12 by vBS, Copyright ©2000-2024, vBulletin Solutions Inc.