Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php codeigniter中的阵列问题_Php_Codeigniter - Fatal编程技术网

Php codeigniter中的阵列问题

Php codeigniter中的阵列问题,php,codeigniter,Php,Codeigniter,我想插入我的静态通知,但我正在检查变量类型是否为空,但此时insert显示消息:非法字符串偏移量'notificationCount'和未定义的索引:type。我正在尝试动态生成数组,但它似乎不起作用 public function addNotification($message, $product_id, $type = ''){ $types = array('new' => 0, 'pending' => 1, 'low stock' => 3); if

我想插入我的静态通知,但我正在检查变量类型是否为空,但此时insert显示消息:非法字符串偏移量'notificationCount'和未定义的索引:type。我正在尝试动态生成数组,但它似乎不起作用

public function addNotification($message, $product_id, $type = ''){
    $types = array('new' => 0, 'pending' => 1, 'low stock' => 3);
    if (isset($types[$type]) === false) {
        throw new \InvalidArgumentException('Value for third parameter must be one of new, pending, or low stock.');
    }
    $type = $types[$type];
    $time = time();
    $query = "SELECT COUNT(*) AS notificationCount FROM storelte_notifications WHERE product_id = ? AND type = ? ";
    $previousNotification = $this->db->query($query, array($product_id, $type));
    if ($previousNotification[0]['notificationCount'] == 0) {
        $sql = "INSERT INTO storelte_notifications (message,type,product_id,created_at) VALUES(?, ?, ?, ?)";
        $this->db->query($sql, array($message, $type, $product_id, $time));
        try {
            if ($this->db->query($sql)) {
                return true;
            }else{
                return false;
            }
        } catch (Exception $e) {

        }
    }else{
        return true;
    }
}
控制器

public function add(){
        $this->notification->addNotification('low stock',4228,'type');
    }
$sql\u prev\u notification是您在此处创建的字符串:

$sql_prev_notification = "SELECT COUNT(*) AS notificationCount FROM storelte_notifications WHERE product_id = ? AND type = ? ";
您可以使用它执行以下查询:

$this->db->query($sql_prev_notification, array($product_id, $type));
但您尚未将查询返回的结果分配给任何对象

$sql\u prev\u通知仍然是一个字符串,因此执行此操作时:

if ($sql_prev_notification[0]['notificationCount'] == 0) {
$sql_prev_notification[0]指的是字符串S中的第一个字母,它显然不是数组,因此

非法字符串偏移量“notificationCount”

您可能想要更像:

$sql = "SELECT COUNT(*) AS notificationCount FROM storelte_notifications WHERE product_id = ? AND type = ? ";
$sql_prev_notification = $this->db->query($sql, array($product_id, $type));
if ($sql_prev_notification[0]['notificationCount'] == 0) {
尽管在从结果中引用特定项之前,您还应该检查您的查询是否确实返回了任何内容。

不要惊慌的回答是修复非法字符串偏移量的正确答案,请继续

对于错误Undefined index:type,您似乎正在传递字符串类型作为变量$type的值。然后,它将该值用作$types数组的键,但是$types数组没有索引类型-它的索引是新的、挂起的和低库存的

要解决此问题,必须将new、pending或low stock作为第三个参数传递给addNotification函数:

$this->notification->addNotification('low stock',4228,'new');
//or
$this->notification->addNotification('low stock',4228,'pending');
//of
$this->notification->addNotification('low stock',4228,'low stock');
您还应该检查传递的密钥是否有效,否则您将继续收到此通知。事实上,传递错误的值可能会导致代码运行不正常,在这种情况下,抛出异常可能是一个好主意:

if (isset($types[$type]) === false) {
    throw new \InvalidArgumentException('Value for third parameter must be one of new, pending, or low stock.');
}

$type = $types[$type];
您可以尝试以下方法:

public function addNotification( $message, $product_id, $type = '' ){
    $types = array( 'new' => 0, 'pending' => 1, 'low stock' => 3);
    if (isset($types[$type]) === false) {
        throw new \InvalidArgumentException('Value for third parameter must be one of new, pending, or low stock.');
    }
    $type = $types[$type];
    $time = time();
    $this->db->select_sum('notificationCount');
    $this->db->where(['product_id' => $product_id, 'type' => $type]);
    $query = $this->db->get();
    $previousNotification = $query->row_array()
    if ( $previousNotification['notificationCount'] == 0 ) {
        $this->db->trans_start();
        $this->db->insert(storelte_notifications, ['message' => $message, 'type' => $type, 'product_id' => $product_id, 'created_at' => $time]);
        $this->db->trans_complete();
        return $this->db->trans_status();
   } else {
       return true;
   }
}

试一下,或者使用然后与0比较。同样,它不起作用。问题出在这一行$type=$types[$type];是的,但我仍然收到这样的消息:非法字符串偏移量'notificationCount',就像我在第一句中写的那样-要修复两部分问题中的非法字符串偏移量部分,请使用不要惊慌的回答:顺便说一下,消息:不能使用CI_DB_mysqli_result类型的对象作为数组,为什么我可以使用result_数组;还是结果?my bindings查询未以数组形式获取结果文档说明查询函数返回的是对象,而不是数组。要以数组形式获取结果,请在结果对象上调用result\u array。文档位于此处:。请注意,如果没有行返回当前构建if语句的方式,则可能会看到错误。我认为此时,您的原始问题已由Don’t Panic的答案回答,您应该接受它作为正确答案,如果你还有其他问题,你可以打开一个新问题,用新的答案而不是评论来解决新问题。