Jump to content

Recommended Posts

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 by SureshT
Link to comment
https://forums.phpfreaks.com/topic/298708-how-to-use-a-function/
Share on other sites

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 by SureshT

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']);
  • Like 1

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']);

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?

  • Like 1

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 by SureshT

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);
  • Like 1

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) "" } 

 

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();

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
  • Like 1

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 '

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
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.