Having challenges with Alerts Function

Hello Guys,
I am somewhat new to PHP but have been developing for years.

I have created a function to capture alert values and add them to an array. This function is on the main page admin.php. See code below.

[php]
function AddAlerts($Alert, $Msg, $Page) {

$Alerts[] = '<br /><hr /><img src="template/'. $Alert .'.png" border="0"> <font class="'. $Alert .'">'. $Msg .'</font>';

$query="INSERT INTO AdminLogs (User, IP, Action, Page)
		VALUES ('". $_SESSION['User'] ."', '". $_SESSION['IP'] ."', '$Msg', '$Page')
		";
$result=mysql_query($query);
if (!$result) {
	echo 'Invalid query: '. mysql_error() .'\n Query: '. $result;
	}
}

[/php]

The admin.php page will then include a series of other pages based on different variables Gallery, Blog, Events, etc… These pages call the function as such after every action Success or Fail.

[php]
$Alert = ‘Fail’;
$Msg = 'Invalid query: ‘. mysql_error() .’\n Query: '. $query;
$Page = $_SERVER[‘PHP_SELF’];
AddAlerts($Alert, $Msg, $Page);
[/php]

The reason I chose to do it this was was because I have multiple actions that can take such as multiple file uploads and want to display alerts for each.

So when I run through a page that calls the AddAlerts function I can see that the query is being executed but the array is not getting updated. I have placed several counts around the function and it never changes from 0. Not sure how that can be if the query is being executed. Here is the display code.

[php]
if (count($Alerts) > 0) {
foreach ($Alerts as $Msg) {
echo $Msg;
}
}
[/php]

Any help on this would be greatly appreciated. Please let me know if you need anything else.

Thank you,

I know there are easier ways of accomplishing this such as just putting my AdminLogs insert into the foreach loop but I was really hoping to lean more about functions. I think this would save me code for allot of my other function such as uploads etc… Instead of having to put 50+ lines of code for every page that has the upload option.

Thank you!

Try changing your function to the following:
[php]function AddAlerts($Alert, $Msg, $Page) {
global $Alerts;
$Alerts[] = …[/php]

Thanks for the tip Smoke but same thing. I added an the code to a variable and added an echo to ensure the variables were all ok which they were. It posted fine but the array is still empty see code below. The echo would almost work except placement is hard as it appears to be at the top of the include page where I handle the processing and call the function instead of the display portion where I would like the array to be displayed. I guess I could build my head then run my processing where the alerts section is but don’t know why I should have to seems odd to me.

[php]global $Alerts;
// Functions ----------------------------------------------//
function AddAlerts($Alert, $Msg, $Page) {
$Text = ’
‘. $Msg .’’;
echo $Text;
$Alerts[] = $Text;
[/php]

Here is the call from the included file.

[php]$Alert = ‘Success’;
$Msg = ‘Home Page Updated: See results on website!’;
$Page = $_SERVER[‘QUERY_STRING’];
AddAlerts($Alert, $Msg, $Page);
[/php]

Thank you for your assistance sir! Let me know if you think of anything else.

If you look back at my post, the “global $Alerts;” needs to go inside the function. Move this line in your code and it should work.

Koo ill give it a shot I just figured that if it was in the function that it would reset the value if I called the function 10 times in the same page.

I will give it a shot tho Thank you!

Ok sir you F*&^ing ROCK! I will have to do some research on that tag to see why that one works but that helps me out tremendously!

Thank you once again.

My pleasure. FYI, ‘global’ makes the specified variable(s) available in that ‘scope’.

http://uk3.php.net/manual/en/language.variables.scope.php

Sponsor our Newsletter | Privacy Policy | Terms of Service