全局变量在codeigniter中不起作用

时间:2016-02-24 06:36:21

标签: php codeigniter

我的application / libraries / globals.php中的代码是:

#!/bin/bash
# USE AT YOUR OWN RISK
# For help: $0 --help
VERSION=0.1.1

# Requires: awk

BN=$(basename "$0")

function help {
cat <<EOF
Syntax: $0 [n1 [FILE]]

Compute the sample mean, the sample variance, and the t-statistic for
the array of numbers in column n1 of the input, where n1 defaults to 1,
and FILE defaults to STDIN.

The output consists of a single line of four values:

n mean variance t

where "n" is the sample size (possibly 0),
      "mean" is the sample mean,
      "variance" is the average of the squared deviations from the mean, and
      "t" is the sample t-statistic, computed as 
           mean * sqrt( n * ss / (n-1));

If a statistic cannot be computed, an asterisk (*) is used in its place.

If the value in the specified field in the input is null or
non-numeric, it is ignored.

If a file is specified as "-" then STDIN is read.

Example:

$BN <<< $'1\n2\n3'
3 2 2 3.4641

EOF
}

function die     { echo "$BN: $@" >&2 ; exit 1 ; }

case "$1" in
    -h | --help )
    help
    exit
    ;;
    -v | --verbose )
    VERBOSE=1
    shift
    ;;
esac

# Default:

file=-

if [ $# = 0 ] ; then
    f1=1
elif [ "$1" = - ] ; then
    f1=1
else
   f1="$1"
   shift
   check=`expr "$f1" : "\([0-9]*\)"`
   if [ "$f1" != "$check" ] ; then die a non-numeric field was specified ; fi
fi

if [ $# != 0 ] ; then file="$1" ; fi

awk -v verbose="$VERBOSE" -v f1="$f1" '
  BEGIN { a1=0; s1=0; n=0; }
  NF < f1 { next }
  $f1 == "" || ($f1+0 != $f1) { next }
  { n++; a1 += $f1; s1 += $f1*$f1; }

  END {
    s="*";
    if ( n == 0 ) {print 0, s, s, s; exit }
    avg = a1/n;
    var = s1/n - avg*avg;
    if (var == 0) { print n, avg, var, s; exit}
    tstat = avg * sqrt( (n-1) / var);
    print n, avg, var, tstat; 
  } ' "$file"

并在application / config / globals.php

<?php
class Globals {
public function __construct($config = array()) {
foreach ($config as $key => $value) {
$data[$key] = $value;
}
$CI = & get_instance();
$CI->load->vars($data);
}
}
?>

在控制器中我正在加载

 <?php

 $config['tmp2'] = "testing 2";
?>

在名为about.php的视图中:

        function aboutus()
{     
$autoload['libraries'] = array('globals');  
$this->load->view('aboutus.php',$data);
} 

但是全局变量在这方面不起作用。如果我缺乏任何意义,那么请告诉我

2 个答案:

答案 0 :(得分:0)

一切都很好看。请尝试将库文件名从global.php更改为Global.php。注意资本G

答案 1 :(得分:0)

所以我尝试复制你在我的安装上所做的事情,老实说我不明白你最初在全局库文件中做了什么......但是为了获得你所说的价值,在你加载之前将它添加到你的控制器视图

function aboutus()
{     
$autoload['libraries'] = array('globals');  
$my_config = $this->config->load('globals', true);
$data=array();
$data['tmp2'] = $this->config->item('tmp2', 'globals');
$this->load->view('aboutus.php',$data);
} 

$data是您传递给视图的变量数组,因此在将配置值存储在$data['tmp2']后,您可以在视图中将其称为$tmp2

一切顺利。