我想将我的通知插入静态,但我正在检查变量类型是否为空,但此时插入显示Message: Illegal string offset 'notificationCount'
和Undefined index: 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');
}
答案 0 :(得分:0)
$sql_prev_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_prev_notification
仍然是一个字符串,所以当你这样做时:
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) {
虽然在引用结果中的特定项目之前,您还应检查您的查询是否实际返回任何内容。
答案 1 :(得分:0)
恐慌的答案(https://stackoverflow.com/a/43529550/4132627)是解决非法字符串偏移的正确答案,请继续使用。
对于错误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];
答案 2 :(得分:-1)
你可以试试这个:
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;
}
}