Jump to content

Archived

This topic is now archived and is closed to further replies.

craygo

Functions variables not working

Recommended Posts

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

here is snips from both files

functions.php
[code]// 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;
}[/code]

servstatus.php
[code]
<?
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>[/code]

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

Share this post


Link to post
Share on other sites
Varibles declared in a function are available only to the function.

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

[code]<?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
}?>[/code]
then where you want to use it:
[code]<?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>[/code]

Ken

Share this post


Link to post
Share on other sites
Exactly what I needed to know!!

Thanks Ken

Ray

Share this post


Link to post
Share on other sites

×

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.