View Full Version : Convert $userinfo[posts] from a string to a number
davide101
02-01-2008, 12:47 PM
First, I've never created a plugin before, so please be gentle!
$userinfo[posts] and $post[posts] outputs a user's posts as a string (why, I don't know). This makes it useless for doing comparisons. I need to create a plugin that will take that output, convert it to a string, and store it in a variable so I can do comparison.
Where exactly do I begin? Is there something out there that does something similar that I can reverse engineer? Do you have any pointers on how to get going?
Opserty
02-01-2008, 01:43 PM
Are you sure? I thought vB converted them to integers. Otherwise its a string because MySQL returns data from a query as an array of strings (it doesn't convert them to integers automatically). You can use the intval() (http://php.net/intval) function to convert a string to a integer.
Nifty_xxl
02-01-2008, 02:15 PM
Plugins are pure php, so there is no need to convert it by youreself. Php has no types if a variable contains a number you can use it as such.
// This one will use it as number
if( $userinfo[posts] >5){
}
// THis will use it as String
echo("Posts:" . $userinfo[posts] );
davide101
02-01-2008, 03:27 PM
Opserty, I'm pretty sure. You can read my thread (http://www.vbulletin.com/forum/showthread.php?t=258483) on vbulletin.com about it. In short, I used the conditional below and it only worked on threads above 2000. If someone can explain why that is, I'd love to know.
<if condition="$post[posts] >= '1000'"> >1000<else /> $post[posts]</if>
Nifty_xxl, that's good to know. So far, I can see two possible approaches. Is either of these the best way to do it?
Way 1 I override the behavior of $userinfo[posts] and $post[posts] within a plugin. This is ideal because I could change them to output the number below 1000 and just > 1000 above. There would be no template edits.
Way 2 Create new variables with the relevant post count and edit the templates. Are those two variables even accessible by a plugin? Are they available everywhere or just within a certain hook?
If I'm conceptually off, please let me know. I really appreciate the feedback. If I can find a way to do this that's straightforward and doesn't require edits, it would make a great mod.
Opserty
02-01-2008, 04:58 PM
You could use str_replace() on the variable:
// I think its $post if not then try $this->post
$post['posts_int'] = intval(str_replace(',', '', $post['posts']));
davide101
02-01-2008, 05:52 PM
THANK YOU! I created three plugins, one for the memberlist_resultbit, profile_complete (or whatever it's called), and postbit_complete. It worked like a charm and all the conditionals are now functioning. At some point in time, I may just overwrite the string rather than using template conditionals to make upgrading easier, but at least it works. I really appreciate the help. Hopefully this will ease some of the fluff posts that have become so common.
Opserty
02-01-2008, 06:13 PM
No problem, the only reason I chose not to overwrite the variable is that it is used in the template in its comma separated form (10,000 e.t.c.) in the postbit template.
vBulletin® v3.8.12 by vBS, Copyright ©2000-2025, vBulletin Solutions Inc.