CodeIgniter:模型变量中的未定义索引

时间:2018-09-19 03:27:37

标签: arrays codeigniter object indexing model

我有以下模型中的代码。我正在加载商品详情页面,但它给了我错误

    public static class Logger
    {
        private static TelemetryClient _logger;

        public static void Log(string message)
        {
            _GetLogger().TrackTrace(message);
            _Flush();
        }

        public static TelemetryClient _GetLogger()
        {
            if (_logger is null)
            {
                TelemetryConfiguration.Active.InstrumentationKey = "your key";
                _logger = new TelemetryClient();

            }

            return _logger;

        }

        public static void _Flush()
        {
            if (_logger is null)
            {
                TelemetryConfiguration.Active.InstrumentationKey = "your key";
                _logger = new TelemetryClient();

            }

            _logger.Flush();
        }

        public static async Task DoSomethingAsync()
        {
            await Task.Delay(1000);
        }

    }

Statistic_model:

Severity: Notice

Message: Undefined index: viewc

Filename: statistics/Statistic_model.php

Line Number: 551

您知道如何解决此错误吗?

2 个答案:

答案 0 :(得分:2)

该错误表示索引viewc不在数组$query->result_array()中很可能是因为您的查询失败或返回了null。您应该养成这样做的习惯:

$query = $this->db->get();
if ($query->num_rows() > 0) {
    return $query->row()->viewc;
}
return false; // or in this case 0 or whatever

答案 1 :(得分:1)

在您的代码中,您从数据库的多个数组中选择数据,然后将at分配给单个数组之类的变量。 您的代码。...

$row = $query->result_array();
$count=$row['viewc'];

正确的方法是在您分配变量的过程中。...

$data   =   array();
foreah($row as $key => $value){
  $data[$key] = $value['viewc'];
}