Jump to content

Functions variables not working


craygo

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
Link to comment
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
Link to comment
Share on other sites

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.