SureshT Posted October 20, 2015 Share Posted October 20, 2015 (edited) I have certain code in Angular which I need to display under certain condtion. The condition, I was told is $context['user']['mentions'] is greater than zero I have written code for a function, use global array, and then if (!empty) to make sure the array condition is not zero. But it seems I need to add something further to this code. I am really new to php and would be happy to have a helping hand. Please find what I have now below function growl_notification() { global $context; if (!empty($context['user']['mentions'])) // Say they have unread alerts. echo ' <body ng-app="af_notify" ng-controller="growlCtrl"> <growl-notifications> <growl-notification> Psst ! You have unread notifications </growl-notification> </growl-notifications> </body>'; } Edited October 20, 2015 by SureshT Quote Link to comment Share on other sites More sharing options...
hansford Posted October 20, 2015 Share Posted October 20, 2015 Just because it's possible doesn't mean it's best practice. function growl_notification( $context ) { if ($context['user']['mentions'] > 0) what not pass the value to function 1 Quote Link to comment Share on other sites More sharing options...
SureshT Posted October 20, 2015 Author Share Posted October 20, 2015 (edited) Thanks for the reply. But it didn't displayed the output of echo. Edited October 20, 2015 by SureshT Quote Link to comment Share on other sites More sharing options...
SureshT Posted October 20, 2015 Author Share Posted October 20, 2015 (edited) This is the way I used in the project /* Popup alert notification using Growl Notifications */ function growl_notification( $context ) { if ($context['user']['mentions'] > 0) // Say they have unread alerts. echo ' <body ng-app="af_notify" ng-controller="growlCtrl"> <growl-notifications></growl-notifications> <growl-notification> Psst ! You have unread notifications </growl-notification> </body>'; } Edited October 20, 2015 by SureshT Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted October 20, 2015 Share Posted October 20, 2015 are you calling the function? a function consists of two parts - 1) the definition, which starts with the 'function' keyword, and 2) calling the function at the point in your code where you want to make use of what the function does. next, the input parameter(s) to the function should be the actual value that the function expects, so that the function is general purpose, and can be called with the input coming from any source. this function is testing if the input parameter is a number greater than zero. the input parameter should just be the number, without any context about where it is coming from. it's your calling code that knows the context of where the value is coming from. use the following for the first few lines of your function definition - function growl_notification($num) { if ($num > 0) and use this where you are calling the function - growl_notification($context['user']['mentions']); 1 Quote Link to comment Share on other sites More sharing options...
SureshT Posted October 20, 2015 Author Share Posted October 20, 2015 My bad. I was not use the calling piece of code.But when I use this, it is still not giving any display of content in echo. PS : Beginner in php, sorry for being noob /* Popup alert notification using Growl Notifications */ function growl_based_notification( $context ) { if ($context['user']['mentions'] > 0) // Say they have unread alerts. echo ' <body ng-app="af_notify" ng-controller="growlCtrl"> <growl-notifications></growl-notifications> <growl-notification> Psst ! You have unread notifications </growl-notification> </body>'; } growl_based_notification($context['user']['mentions']); Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted October 20, 2015 Share Posted October 20, 2015 are you sure the code is running at all? are you getting any output on the page (a blank .php page is usually due to a fatal parse or runtime error.) what is the actual value in $context['user']['mentions'] at the point where you are calling the function? 1 Quote Link to comment Share on other sites More sharing options...
SureshT Posted October 20, 2015 Author Share Posted October 20, 2015 (edited) I am adding notification bar to alerts of likes and @mentions for a forum software. The code inside echo runs ok if I remove function and if. The home page is loading without the notification. It is just that the condition is not met using the code I am using. Edited October 20, 2015 by SureshT Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted October 20, 2015 Share Posted October 20, 2015 that would indicate that $context doesn't contain what you expect. what does adding var_dump($context);, right before the call to the function show? 1 Quote Link to comment Share on other sites More sharing options...
Barand Posted October 20, 2015 Share Posted October 20, 2015 Your function expects the $context array function growl_based_notification( $context ){ if ($context['user']['mentions'] > 0) When you call it you pass just the single element of the array growl_based_notification($context['user']['mentions']); Call the function with growl_based_notification($context); 1 Quote Link to comment Share on other sites More sharing options...
SureshT Posted October 20, 2015 Author Share Posted October 20, 2015 that would indicate that $context doesn't contain what you expect. what does adding var_dump($context);, right before the call to the function show? This, on top of the page array(25) { ["html_headers"]=> string(80) " " ["user"]=> array(13) { ["id"]=> int(1) ["is_logged"]=> bool(true) ["is_guest"]=> &bool(false) ["is_admin"]=> &bool(true) ["is_mod"]=> &bool(false) ["is_moderator"]=> &bool(false) ["can_mod"]=> bool(true) ["username"]=> string(6) "wizard" ["language"]=> string(7) "english" ["email"]=> string(20) "wizard@adminforum.in" ["ignoreusers"]=> array(0) { } ["name"]=> string(6) "wizard" ["smiley_set"]=> string( "Sunshine" } ["linktree"]=> array(1) { [0]=> array(2) { ["url"]=> string(35) "http://localhost/elktheme/index.php" ["name"]=> string(11) "Admin Forum" } } ["open_mod_reports"]=> string(1) "0" ["admin_preferences"]=> array(0) { } ["minmax_preferences"]=> array(0) { } ["links"]=> array(0) { } ["javascript_files"]=> array(0) { } ["css_files"]=> array(1) { ["index.css"]=> array(2) { ["filename"]=> string(54) "http://localhost/elktheme/themes/notify1/css/index.css" ["options"]=> array( { ["subdir"]=> string(3) "css" ["extension"]=> string(3) "css" ["index_name"]=> string(9) "css_files" ["debug_index"]=> string(6) "sheets" ["basename"]=> string(9) "index.css" ["local"]=> bool(true) ["dir"]=> string(46) "/opt/lampp/htdocs/elktheme/themes/notify1/css/" ["url"]=> string(40) "http://localhost/elktheme/themes/notify1" } } } ["javascript_inline"]=> array(2) { ["standard"]=> array(0) { } ["defer"]=> array(0) { } } ["javascript_vars"]=> array(0) { } ["menu_separator"]=> string(1) " " ["session_var"]=> string(7) "a583269" ["session_id"]=> string(32) "6690911edfa716ddb2456b5fdf71f5ea" ["forum_name"]=> string(11) "Admin Forum" ["forum_name_html_safe"]=> string(11) "Admin Forum" ["current_action"]=> NULL ["current_subaction"]=> NULL ["can_register"]=> bool(true) ["theme_header_callbacks"]=> array(0) { } ["upper_content_callbacks"]=> array(0) { } ["server"]=> array(9) { ["is_iis"]=> bool(false) ["is_apache"]=> bool(true) ["is_litespeed"]=> bool(false) ["is_lighttpd"]=> bool(false) ["is_nginx"]=> bool(false) ["is_cgi"]=> bool(false) ["is_windows"]=> bool(false) ["iso_case_folding"]=> bool(false) ["needs_login_fix"]=> bool(false) } ["browser_body_id"]=> string(7) "firefox" ["browser"]=> array(9) { ["is_opera"]=> bool(false) ["is_webkit"]=> bool(false) ["is_konqueror"]=> bool(false) ["is_gecko"]=> bool(true) ["is_firefox"]=> bool(true) ["is_firefox41"]=> bool(true) ["is_opera_mini"]=> bool(false) ["is_opera_mobi"]=> bool(false) ["possibly_robot"]=> bool(false) } ["insert_after_template"]=> string(0) "" } Quote Link to comment Share on other sites More sharing options...
SureshT Posted October 20, 2015 Author Share Posted October 20, 2015 Your function expects the $context array When you call it you pass just the single element of the array Call the function with growl_based_notification($context); I am using this code now. Can I ? /* Popup alert notification using Growl Notifications */ function growl_based_notification() { global $context; if (!empty($context['user']['mentions']) > 0) // Say they have unread alerts. echo ' <body ng-app="af_notify" ng-controller="growlCtrl"> <growl-notifications> <growl-notification> Psst ! You have unread notifications </growl-notification> </growl-notifications> </body>'; } var_dump($context); growl_based_notification(); Quote Link to comment Share on other sites More sharing options...
Strider64 Posted October 20, 2015 Share Posted October 20, 2015 (edited) I personally when find doing this when debugging - echo "<pre>" . print_r($context, 1) . "<pre>\n"; for it gives a nicer readable output. Edited October 20, 2015 by Strider64 1 Quote Link to comment Share on other sites More sharing options...
SureshT Posted October 20, 2015 Author Share Posted October 20, 2015 It does Quote Link to comment Share on other sites More sharing options...
Barand Posted October 20, 2015 Share Posted October 20, 2015 You should forget the global keyword exists and avoid using it. Pass the value to the function. You can either pass the array function growl_based_notification($context) { if (!empty($context['user']['mentions']) > 0) // Say they have unread alerts. ... } growl_based_notification($context) // call function or just pass the part of the array that you need function growl_based_notification($mentions) { if (!empty($mentions) > 0) // Say they have unread alerts. ... } growl_based_notification($context['user']['mentions']) // call function 1 Quote Link to comment Share on other sites More sharing options...
SureshT Posted October 20, 2015 Author Share Posted October 20, 2015 Used the pass the array method Parse error: syntax error, unexpected 'function' (T_FUNCTION) in .../themes/notify1/index.template.php on line 737 728: 729: growl_based_notification($user_info['mentions']) // call function 730: 731: 732: 733: /** 734: * Very simple and basic template to display a legend explaining the meaning 735: * of some icons used in the messages listing (locked, sticky, etc.) 736: */ 737: function template_basicicons_legend() 738: { 739: global $context, $modSettings, $txt; 740: 741: echo ' Quote Link to comment Share on other sites More sharing options...
maxxd Posted October 20, 2015 Share Posted October 20, 2015 You're missing the semicolon to end the call to the function at line 729. 1 Quote Link to comment Share on other sites More sharing options...
SureshT Posted October 20, 2015 Author Share Posted October 20, 2015 Thank you. Here is the code /* Popup alert notification using Growl Notifications */ function growl_based_notification($mentions) { if (!empty($mentions) > 0) // Say they have unread alerts. echo ' <html ng-app="af_notify" ng-controller="growlCtrl"> <growl-notifications> <growl-notification> Psst ! You have unread notifications <a href="#" ng-click="$growlNotification.remove()"> x</a> </growl-notification> </growl-notifications> </html>'; } growl_based_notification($user_info['mentions']); // call function Quote Link to comment Share on other sites More sharing options...
SureshT Posted October 20, 2015 Author Share Posted October 20, 2015 Still no out put displayed for echo. Quote Link to comment Share on other sites More sharing options...
Barand Posted October 20, 2015 Share Posted October 20, 2015 Where are you getting $user_info? You were originally using $context['user']['mentions'] when you called the function, why have you changed it? Quote Link to comment Share on other sites More sharing options...
SureshT Posted October 21, 2015 Author Share Posted October 21, 2015 As I said, I was trying it for an open source forum software. When I followed the instructions here and was told that $context could be the issues, the forum software guys told me to try $user_info. I tried all possible combinations. Still no luck. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.