Jump to content


Photo

Functions variables not working


  • Please log in to reply
2 replies to this topic

#1 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 28 February 2006 - 03:22 PM

I have 2 files. function.php and servstatus.php

here is snips from both files

functions.php
// Check if TE CSS Private 1 server is running
function tecsspriv1(){
$serv1 = win32_scheduler_get_task_info("start_TE_CSS_Priv1");
foreach ($serv1 as $k1[] => $v1[]){
//echo "$key: $value<br />\n";
}
if($v1[12] == '267009'){
$priv1status = 'Online';
} else {
$priv1status = 'Offline';
}
echo $priv1status;
}

servstatus.php
<?
include('includes/functions.php');
?>
<table width=600 align=center>
<form name=servmanager method=POST action="<?=$_SERVER['PHP_SELF']?>">
  <tr>
    <td width=400>The Elders CS:S Private 1 IP:66.29.112.102</td>
    <td colspan=2 align=center><strong><?=tecsspriv1()?></td>
  </tr>
  <tr>
    <td> </td>
    <td width=100><input type=submit name=shutdown value="Shutdown Priv1"></td>
    <td width=100><input type=submit name=start value="Start Priv1" <?if($priv1status == 'Online'){print 'disabled'; }?>></td> <---- Line 29
  </tr>
</form>
</table>

When I go to the page is says
Notice: Undefined variable: priv1status in c:\Inetpub\wwwroot\gamestatus\servstatus.php on line 29

How come the variable is not being carried over from the functions file.

thanks

Ray

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 28 February 2006 - 04:12 PM

Varibles declared in a function are available only to the function.

Change your function to return the status instead of echoing it.

<?php
function tecsspriv1(){
    $serv1 = win32_scheduler_get_task_info("start_TE_CSS_Priv1");
    foreach ($serv1 as $k1[] => $v1[]){
    //echo "$key: $value<br />\n";
    }
    $priv1status = 'Offline';
    if($v1[12] == '267009') $priv1status = 'Online';
    return ($priv1status); // return status
}?>
then where you want to use it:
<?php
include('includes/functions.php');
$priv1status = tecsspriv1(); // get the status
?>
<table width=600 align=center>
<form name=servmanager method=POST action="<?=$_SERVER['PHP_SELF']?>">
  <tr>
    <td width=400>The Elders CS:S Private 1 IP:66.29.112.102</td>
    <td colspan=2 align=center><strong><? echo $priv1status ?></td>
  </tr>
  <tr>
    <td> </td>
    <td width=100><input type=submit name=shutdown value="Shutdown Priv1"></td>
    <td width=100><input type=submit name=start value="Start Priv1" <?if($priv1status == 'Online') echo 'disabled'; ?>></td>
  </tr>
</form>
</table>

Ken

#3 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 28 February 2006 - 04:29 PM

Exactly what I needed to know!!

Thanks Ken

Ray




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users