I asked a question here, so many thanks to Gordon Linoff for his complete answer on SQL section of it.
Problem
I have a sql table ( with PRIMARY KEY = [ ip & id ] ) , like this:
ip | id | visits
and I want to generate a php array from that table, like this:
product_rating_for_ip = array(
ip=>array(
id=>rating, id=>rating, ...
),
ip=>array(
id=>rating, id=>rating, ...
),
.
.
.
);
for example:
product_rating_for_ip = array(
"78.125.15.92"=>array(
1=>0.8,2=>0.2,3=>1.0
),
"163.56.75.112"=>array(
1=>0.1,2=>0.3,3=>0.5,8=>0.1,12=>1.0
),
.
.
.
);
Gordon Linoff suggested this SQL query which works fine:
SQL Query
select v.ip, group_concat(concat(v.id, ':', v.visits / iv.maxvisits)) as ratings
from visit v join
(SELECT ip, id, visits, max(visits) as maxvisits
FROM visit
GROUP BY ip
) iv
on v.ip = iv.ip
group by v.ip;
which produces this:
+-------------+--------------------------+
|ip |ratings |
+-------------+--------------------------+
|154.18.72.15 |2:0.1667,1:1.0000,3:0.1667|
|162.16.18.22 |1:1.0000,3:0.3750,2:0.1250|
|142.128.1.14 |2:1.0000,1:0.2500,3:0.5000|
|78.15.92.131 |1:0.1429,3:1.0000,2:0.2857|
+-------------+--------------------------+
Question in summary
How can I put this result set into this PHP array?
product_rating_for_ip = array(
ip=>array(
id=>rating, id=>rating, ...
),
ip=>array(
id=>rating, id=>rating, ...
),
.
.
.
);
My problematic answer
I think I should do as this (may be incorrect or inefficient):
$stmt = mysqli_prepare($con, "select ...");
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt,$ip,$ratings);
while (mysqli_stmt_fetch($stmt)){
$value_array = explode(",",$ratings);
product_rating_for_ip = array_fill_keys($ip, $value_array);
}