Invision Community 4: SEO, prepare for v5 and dormant account notifications By Matt Monday at 02:04 PM
Jyoti Rani Posted October 26, 2023 Author Posted October 26, 2023 Can you please tell me which event is called when New topic is created. in the processForm function - It is called during both time while editing and creating. But i need only when its created. I used save() , processAfterCreate(), createFromForm() and some mor but they are not being called while creating new topic.
Adriano Faria Posted October 26, 2023 Posted October 26, 2023 I have a ton of resources using this. Show your code.
Jyoti Rani Posted October 26, 2023 Author Posted October 26, 2023 22 minutes ago, Adriano Faria said: I have a ton of resources using this. Show your code. public function processAfterCreate($comment, $values) { $result = \IPS\Db::i()->query("SELECT * FROM xxxx WHERE user_id = " . $user_id); // SET field_a='value' WHERE id_field=1"); if ($result->num_rows > 0) { while ($row = mysqli_fetch_assoc($result)) { // $row is now an associative array containing column names as keys $cc = $reward_mailing_points + intval($row['cc']); } } return parent::processAfterCreate($comment, $values); } public function save() { $result = \IPS\Db::i()->query("SELECT * FROM xxxx WHERE user_id = " . $user_id); // SET field_a='value' WHERE id_field=1"); if ($result->num_rows > 0) { while ($row = mysqli_fetch_assoc($result)) { // $row is now an associative array containing column names as keys $cc = $reward_mailing_points + intval($row['cc']); } } return parent::save(); }
teraßyte Posted October 26, 2023 Posted October 26, 2023 (edited) A few things: In the query, you are using $user_id, but there is no such value defined anywhere in the code. You are not saving the updated $cc value back to the database, that's why it's not working. You're using a mix of IPS and non-IPS functions to access the DB data. You should stick to using a single method to keep things consistent. Is the table you're trying to update in the same database as Invision Community? If it's in an external database, you're using the wrong code to connect to it. Using simply \IPS\Db::i() will try to load the table from the forum's database, not your external one. Edited October 26, 2023 by teraßyte
Jyoti Rani Posted October 26, 2023 Author Posted October 26, 2023 1 hour ago, teraßyte said: A few things: In the query, you are using $user_id, but there is no such value defined anywhere in the code. You are not saving the updated $cc value back to the database, that's why it's not working. You're using a mix of IPS and non-IPS functions to access the DB data. You should stick to using a single method to keep things consistent. Is the table you're trying to update in the same database as Invision Community? If it's in an external database, you're using the wrong code to connect to it. Using simply \IPS\Db::i() will try to load the table from the forum's database, not your external one. point 2 in the try block i m using below statement also $result = \IPS\Db::i()->query("UPDATE xxx SET credit_balance = " . $credit_balance . " WHERE user_id = '" . $user_id . "'"); point 1 User id i m getting using below code if (\IPS\Member::loggedIn()->member_id) { // // Get the current user's member object $currentUser = \IPS\Member::loggedIn(); // // Access user data # Fetch member details $user_id = $currentUser->member_id; $username = $currentUser->name; $email = $currentUser->email; $user_group_id = $currentUser->member_group_id; } else { $user_id = 0; } point 3 - my table name does not start with tbl_prefix. that is why i have to use mix IPS and non-IPS 4. Is the table you're trying to update in the same database as Invision Community? - YES. If it's in an external database, you're using the wrong code to connect to it. Using simply \IPS\Db::i() will try to load the table from the forum's database, not your external one. - My table is in the same db not the external db.
teraßyte Posted October 26, 2023 Posted October 26, 2023 I'm having a really hard time understanding your code because it's all partial or missing context and variables. Anyway, you can temporarily disable the prefix in the database class if your tables don't have it. Or you can keep using manual query() calls, but there is no need to use mysqli_fetch_assoc. See the code below, I tried to update your processAfterCreate() function above with more consistent code: public function processAfterCreate($comment, $values) { # Remove prefix $previousPrefix = \IPS\Db::i()->prefix; \IPS\Db::i()->prefix = ''; # Vars $reward_mailing_points = 10; # Not sure where you're pulling this value from, so I set an arbitrary 10 here as example $cc = 0; # Get result - If this query always returns only 1 row, it can be changed to use ->first() $result = \IPS\Db::i()->select('*', 'xxxx', array( 'user_id=?', \IPS\Member::loggedIn()->member_id ) ); foreach( $result as $row ) { $cc += $reward_mailing_points + intval($row['cc']); } # Update database with new $cc value \IPS\Db::i()->update( 'xxx', array( 'credit_balance' => $cc ), array( 'user_id=?', \IPS\Member::loggedIn()->member_id ) ); # Reset prefix \IPS\Db::i()->prefix = $previousPrefix; return parent::processAfterCreate($comment, $values); } Jyoti Rani 1
Jyoti Rani Posted October 27, 2023 Author Posted October 27, 2023 14 hours ago, teraßyte said: I'm having a really hard time understanding your code because it's all partial or missing context and variables. Anyway, you can temporarily disable the prefix in the database class if your tables don't have it. Or you can keep using manual query() calls, but there is no need to use mysqli_fetch_assoc. See the code below, I tried to update your processAfterCreate() function above with more consistent code: public function processAfterCreate($comment, $values) { # Remove prefix $previousPrefix = \IPS\Db::i()->prefix; \IPS\Db::i()->prefix = ''; # Vars $reward_mailing_points = 10; # Not sure where you're pulling this value from, so I set an arbitrary 10 here as example $cc = 0; # Get result - If this query always returns only 1 row, it can be changed to use ->first() $result = \IPS\Db::i()->select('*', 'xxxx', array( 'user_id=?', \IPS\Member::loggedIn()->member_id ) ); foreach( $result as $row ) { $cc += $reward_mailing_points + intval($row['cc']); } # Update database with new $cc value \IPS\Db::i()->update( 'xxx', array( 'credit_balance' => $cc ), array( 'user_id=?', \IPS\Member::loggedIn()->member_id ) ); # Reset prefix \IPS\Db::i()->prefix = $previousPrefix; return parent::processAfterCreate($comment, $values); } Hello terabyte, A very big thanks to your help and prompt reply. I was able to do the changes as per your suggestions. Here is my final code and its working when user is adding Topic at my local machine. protected function processAfterCreate($comment, $values) { # Remove prefix $previousPrefix = \IPS\Db::i()->prefix; \IPS\Db::i()->prefix = ''; $user_group_id = \IPS\Member::loggedIn()->member_group_id; if ($user_group_id == 3 || $user_group_id == 4 || $user_group_id == 7 || $user_group_id == 😎 //bronze // || $user_group_id == 4) { # no need to check for double points $assign_point = 1; } if ($assign_point == 1) { $reward_mailing_points = 250; } $result = \IPS\Db::i()->select('*', 'plug_mailing_master', array('user_id=?', \IPS\Member::loggedIn()->member_id)); foreach ($result as $row) { $credit_balance = $reward_mailing_points + intval($row['credit_balance']); } # Update database with new $cc value \IPS\Db::i()->update('plug_mailing_master', array('credit_balance' => $credit_balance), array('user_id=?', \IPS\Member::loggedIn()->member_id)); # Reset prefix \IPS\Db::i()->prefix = $previousPrefix; return parent::processAfterCreate($comment, $values); }
Jyoti Rani Posted October 27, 2023 Author Posted October 27, 2023 26 minutes ago, Jyoti Rani said: Hello terabyte, A very big thanks to your help and prompt reply. I was able to do the changes as per your suggestions. Here is my final code and its working when user is adding Topic at my local machine. protected function processAfterCreate($comment, $values) { # Remove prefix $previousPrefix = \IPS\Db::i()->prefix; \IPS\Db::i()->prefix = ''; $user_group_id = \IPS\Member::loggedIn()->member_group_id; if ($user_group_id == 3 || $user_group_id == 4 || $user_group_id == 7 || $user_group_id == 😎 //bronze // || $user_group_id == 4) { # no need to check for double points $assign_point = 1; } if ($assign_point == 1) { $reward_mailing_points = 250; } $result = \IPS\Db::i()->select('*', 'plug_mailing_master', array('user_id=?', \IPS\Member::loggedIn()->member_id)); foreach ($result as $row) { $credit_balance = $reward_mailing_points + intval($row['credit_balance']); } # Update database with new $cc value \IPS\Db::i()->update('plug_mailing_master', array('credit_balance' => $credit_balance), array('user_id=?', \IPS\Member::loggedIn()->member_id)); # Reset prefix \IPS\Db::i()->prefix = $previousPrefix; return parent::processAfterCreate($comment, $values); } Its working on the betaversion of the site also. Thanks a Ton!
Solution teraßyte Posted October 27, 2023 Solution Posted October 27, 2023 (edited) Based on what you posted I now have a clearer idea of what you're doing, but I still see a few issues and optimizations that can be made: You can use a quick in_array() check for the group instead of multiple OR checks. Also, note that your code doesn't take into account secondary groups, if you want to check also those, you need to adjust the code. The $assign_point variable is not defined before the IF check, if the user is in a group outside of the listed IDs, PHP 8 will throw an error about an unknown variable. I see that you're basically loading the credit balance from a table, adding 250 to it, and then updating the same table back. The whole process can be simplified in a single query instead. The reset DB prefix code can be moved inside the IF check, if the user doesn't need a credit balance update, there's no point running it. Here's an updated code based on the points above: protected function processAfterCreate($comment, $values) { # Comment/uncomment either line below based on what you want #if ( \in_array( \IPS\Member::loggedIn()->member_group_id, array( 3, 4, 7, 8 ) ) ) # This line checks only the primary group if ( \IPS\Member::loggedIn()->inGroup( array( 3, 4, 7, 8 ) ) ) # This line checks also secondary groups { # Temporarily disable the DB prefix $previousPrefix = \IPS\Db::i()->prefix; \IPS\Db::i()->prefix = ''; # Update credit_balance column to add 250 credits to its current value \IPS\Db::i()->update('plug_mailing_master', "credit_balance=credit_balance+250", array('user_id=?', \IPS\Member::loggedIn()->member_id)); # Reset prefix \IPS\Db::i()->prefix = $previousPrefix; } return parent::processAfterCreate($comment, $values); } The code checks if the user has one of the specified groups, and adds 250 to their credit balance value based on their member ID. No need for anything else. P.S.: If you use the Code button in the editor the code you post will look better and formatted for the appropriate language. 😉 Edited October 27, 2023 by teraßyte Jyoti Rani 1
Jyoti Rani Posted October 27, 2023 Author Posted October 27, 2023 Just now, teraßyte said: Based on what you posted I now have a clearer idea of what you're doing, but I still see a few issues and optimizations that can be made: You can use a quick in_array() check for the group instead of multiple OR checks. Also, note that your code doesn't take into account secondary groups, if you want to check also those, you need to adjust the code. The $assign_point variable is not defined before the IF check, if the user is in a group outside of the listed IDs, PHP 8 will throw an error about an unknown variable. I see that you're basically loading the credit balance from a table, adding 250 to it, and then updating the same table back. The whole process can be simplified in a single query instead. Here's an updated code based on the points above: protected function processAfterCreate($comment, $values) { # Remove prefix $previousPrefix = \IPS\Db::i()->prefix; \IPS\Db::i()->prefix = ''; # Comment/uncomment either line below based on what you want #if ( \in_array( \IPS\Member::loggedIn()->member_group_id, array( 3, 4, 7, 8 ) ) ) # This line checks only the primary group if ( \IPS\Member::loggedIn()->inGroup( array( 3, 4, 7, 8 ) ) ) # This line checks also secondary groups { # Update database with new $cc value \IPS\Db::i()->update('plug_mailing_master', "credit_balance=credit_balance+250", array('user_id=?', \IPS\Member::loggedIn()->member_id)); } # Reset prefix \IPS\Db::i()->prefix = $previousPrefix; return parent::processAfterCreate($comment, $values); } The code checks if the user has one of the specified groups, and adds 250 to their credit balance value based on their member ID. No need for anything else. P.S.: If you use the Code button in the editor the code you post will look better and formatted for the appropriate language. 😉 One more query is, when some one replied to this Topic, which event is fired internally.
Recommended Posts