2

First, I have rating table in my database like this

=====================================
| id | userid | item_id    | rating |
=====================================
| 1  |   1    |     B      |   5    |
| 2  |   1    |     C      |   4    |
| 3  |   2    |     A      |   4    |
| 4  |   2    |     C      |   3    |
| 5  |   3    |     A      |   2    |
| 5  |   3    |     B      |   2    |
| 6  |   3    |     C      |   2    |
=====================================

Then I want to create an array from the table, and calculate the array. For more details, this is my code

<?php
include './connect.php';
$userid = $_SESSION['ids'];

$sql1 = "SELECT item_id, rating FROM rating WHERE userid='$userid' ";
$result1 = $conn->query($sql1);

$itemI = array();
$itemJ = array();
$items = array();

while($row1 = mysqli_fetch_assoc($result1)){
    $items[$row1["item_id"]] = $row1["item_id"];
    $itemI[$row1["item_id"]] = $row1["rating"];
    $itemJ[$row1["item_id"]] = $row1["rating"];

    $ditemI = $itemI[$row1["item_id"]];
    $ditemJ = $itemJ[$row1["item_id"]];
    $nume = 0;
    $den1 = 0;
    $den2 = 0;
    $rs = 0;

$sqlr = "SELECT AVG(rating) AS avgRatingUser FROM rating WHERE userid='$userid' ";
            $resultr = $conn->query($sqlr);
            $duser  = array();
            while($rowr = mysqli_fetch_assoc($resultr)){
                $duser[$u]["avgRatingUser"] = $rowr["avgRatingUser"];
                $duserU = $duser[$u]["avgRatingUser"];
            }

    for($i = 0; $i<count($itemI); $i++){
        for($j = $i+1; $j<count($itemI); $j++){

            $nume += (($itemI[$i] - $duser[$u]) * ($itemJ[$j] - $duser[$u]));
            $den1 += (pow(($itemI[$i] - $duser[$u]), 2));
            $den2 += (pow(($itemJ[$j] - $duser[$u]), 2));           

        $squart = (sqrt($den1)) * (sqrt($den2));
        $rs += $nume/$squart;
    }
}
}

echo $nume. " ";
echo $den1. " ";
echo $den2. " ";
echo $squart. " ";
echo $rs. " ";

then my question is, why the result of the array calculation is not showing?

and where is the error of my code?

  • you have to learn the scope of variables in https://stackoverflow.com/questions/16959576/reference-what-is-variable-scope-which-variables-are-accessible-from-where-and – A.D. Dec 28 '17 at 04:26
  • try to print the value in each step and u get to know , where you went wrong – Mahesh Hegde Dec 28 '17 at 04:28

1 Answers1

0

Your problem revolve around the fact that you use wrong index for both read and write.

In the first loop after SELECT-ing data, you add the following code (which is fine):

$items[$row1["item_id"]] = $row1["item_id"];
$itemI[$row1["item_id"]] = $row1["rating"];
$itemJ[$row1["item_id"]] = $row1["rating"];

And in the next codes, you read it like this (which also fine):

$nume += (($itemI[$i] - $duser[$u]) * ($itemJ[$j] - $duser[$u]));
$den1 += (pow(($itemI[$i] - $duser[$u]), 2));
$den2 += (pow(($itemJ[$j] - $duser[$u]), 2));

The problem is, from your table information, it is known that item_id is A, B, C, ... and you read the array in the later codes by using integer index taken from for loop code. You need to decide what kind of index you want to use for your array.

ariefbayu
  • 21,849
  • 12
  • 71
  • 92