I've a DB
table where I stored shopping cart data with serialize
. I want to count
shopping cart qty
with duplicates row. How can I solve it ?
Suppose, I've two rows and I want to count qty
for id 2
from those rows.
a:3:{s:32:"3c59dc048e8850243be8079a5c74d079";a:6:{s:5:"rowid";s:32:"3c59dc048e8850243be8079a5c74d079";s:2:"id";s:2:"21";s:3:"qty";s:1:"6";s:5:"price";s:3:"250";s:4:"name";s:20:"Beef SIndhi Biriyane";s:8:"subtotal";i:1500;}s:32:"6ea9ab1baa0efb9e19094440c317e21b";a:6:{s:5:"rowid";s:32:"6ea9ab1baa0efb9e19094440c317e21b";s:2:"id";s:2:"29";s:3:"qty";s:1:"1";s:5:"price";s:2:"90";s:4:"name";s:14:"Egg Fried Rice";s:8:"subtotal";i:90;}s:32:"98f13708210194c475687be6106a3b84";a:6:{s:5:"rowid";s:32:"98f13708210194c475687be6106a3b84";s:2:"id";s:2:"20";s:3:"qty";s:1:"1";s:5:"price";s:3:"120";s:4:"name";s:19:"Hyderabadi Biriyane";s:8:"subtotal";i:120;}}
and
a:3:{s:32:"3c59dc048e8850243be8079a5c74d079";a:6:{s:5:"rowid";s:32:"3c59dc048e8850243be8079a5c74d079";s:2:"id";s:2:"21";s:3:"qty";s:1:"6";s:5:"price";s:3:"250";s:4:"name";s:20:"Beef SIndhi Biriyane";s:8:"subtotal";i:1500;}s:32:"6ea9ab1baa0efb9e19094440c317e21b";a:6:{s:5:"rowid";s:32:"6ea9ab1baa0efb9e19094440c317e21b";s:2:"id";s:2:"29";s:3:"qty";s:1:"1";s:5:"price";s:2:"90";s:4:"name";s:14:"Egg Fried Rice";s:8:"subtotal";i:90;}s:32:"98f13708210194c475687be6106a3b84";a:6:{s:5:"rowid";s:32:"98f13708210194c475687be6106a3b84";s:2:"id";s:2:"20";s:3:"qty";s:1:"1";s:5:"price";s:3:"120";s:4:"name";s:19:"Hyderabadi Biriyane";s:8:"subtotal";i:120;}}
Here is my solution for this problem.
$this->db->select( "*, count(substring_index(substring_index(order_data,';',7),':',-1)) AS total", FALSE );
$this->db->from( "order" );
$this->db->group_by( "order_data" );
$res = $this->db->get();
return ( $res->num_rows() > 0 ) ? $res->result() : false;