【问题标题】:php - Division by 0php - 除以 0
【发布时间】:2014-01-07 15:59:18
【问题描述】:

非常感谢朋友! 现在我有另一个错误:警告:除以零 这是剩下的代码:

while($i<$n_utenti)
{
    $query3 = $mysqli->query("SELECT * FROM valutazione WHERE email='$lista_utenti[$i]'"); 
    $indice=0;
    while($row = mysqli_fetch_array($query3)) //acquisisco le votazioni dell'utente considerato per il confronto
    {
        $album_utente_confronto[$indice]=$row['id_album'];
        $voti_utente_confronto[$indice]=$row['voto'];
        $indice++;
    }
    //trovo gli album che sono stati valutati da entrambi gli utenti
    $indice=0;
    $n_album_loggato=count($album_utente_loggato);
    $n_album_confronto=count($album_utente_confronto);
    $ind=0;
    for($indice=0;$indice<$n_album_loggato; $indice++)
    {
        $index=0;
        while($index<$n_album_confronto)
        {
            if($album_utente_loggato[$indice]==$album_utente_confronto[$index])
            {
                $album_comuni[$ind]=$album_utente_loggato[$indice];
                $ind++;
            }
            $index++;
        }
    }
    //mi trovo i voti relativi agli album comuni per l'utente loggato
    $n_album_comuni = count($album_comuni);
    $indice=0;
    $i_comuni=0;
    for($indice=0;$indice<$n_album_comuni; $indice++)
    {
        $index=0;
        $trovato=false;
        while($index<$n_album_loggato && $trovato==false)
        {
            if($album_comuni[$indice]==$album_utente_loggato[$index])
            {
                $voti_comuni_logged[$i_comuni] = $voti_utente_loggato[$index];
                $trovato = true;
                $i_comuni++;
            }
            else
            {
                $index++;
            }               
        }           
    }
    //mi trovo i voti relativi agli album comuni per l'utente confrontato
    $indice=0;
    $i_comuni=0;
    for($indice=0;$indice<$n_album_comuni; $indice++)
    {
        $index=0;
        $trovato=false;
        while($index<$n_album_confronto && $trovato==false)
        {
            if($album_comuni[$indice]==$album_utente_confronto[$index])
            {
                $voti_comuni_confronto[$i_comuni] = $voti_utente_confronto[$index];
                $trovato = true;
                $i_comuni++;
            }
            else
            {
                $index++;
            }               
        }           
    }


    $correlation[$i] = Correlation($voti_comuni_logged, $voti_comuni_confronto);


    $i++;//chiusura while
}

//Displaying the calculated Correlation:
print $correlation[0];




}


//The functions that work behind the scene to calculate the
//correlation

function Correlation($arr1, $arr2)
{        
$correlation = 0;

$k = SumProductMeanDeviation($arr1, $arr2);
$ssmd1 = SumSquareMeanDeviation($arr1);
$ssmd2 = SumSquareMeanDeviation($arr2);

$product = $ssmd1 * $ssmd2;

$res = sqrt($product);

$correlation = $k / $res;

return $correlation;
}

function SumProductMeanDeviation($arr1, $arr2)
{
$sum = 0;

$num = count($arr1);

for($i=0; $i<$num; $i++)
{
    $sum = $sum + ProductMeanDeviation($arr1, $arr2, $i);
}

return $sum;
}

function ProductMeanDeviation($arr1, $arr2, $item)
{
return (MeanDeviation($arr1, $item) * MeanDeviation($arr2, $item));
}

function SumSquareMeanDeviation($arr) 
{
$sum = 0;

$num = count($arr);

for($i=0; $i<$num; $i++)
{
    $sum = $sum + SquareMeanDeviation($arr, $i);
}

return $sum;
}

function SquareMeanDeviation($arr, $item)
{
return MeanDeviation($arr, $item) * MeanDeviation($arr, $item);
} 

function SumMeanDeviation($arr)
{
$sum = 0;

$num = count($arr);

for($i=0; $i<$num; $i++)
{
    $sum = $sum + MeanDeviation($arr, $i);
}

return $sum;
}

function MeanDeviation($arr, $item)
{
$average = Average($arr);

return $arr[$item] - $average;
}    

function Average($arr)
{
$sum = Sum($arr);
$num = count($arr);

return $sum/$num;
}

function Sum($arr)
{
return array_sum($arr);
}

错误在于函数相关性,在这一行: $相关 = $k / $res; 为什么它给我错误? 我应该有一个介于 0 和 1 之间的值

【问题讨论】:

  • 如果 $_SESSION['Mail'] 来自用户输入,请注意 SQL 注入。

标签: php undefined offset


【解决方案1】:

你应该改变这个:

while($i<=$n_utenti)

到这里:

while($i<$n_utenti)

$n_utenti$lista_utenti 中的记录数大一。

否则使用起来会更干净

foreach($lista_uteni as $email){

基于循环。 (您可以通过这种方式逐步淘汰一个临时变量。)

【讨论】:

  • 非常感谢!我已经编辑了第一个帖子,出现了一个新错误,你能看看那里吗?
  • 一般来说,在这个网站上改变问题帖子的焦点不是一个好习惯......否则不知何故你的 $res 似乎为零。你应该调试你的变量。也许一些印刷品会有所帮助。
  • 谢谢,我已经更改了数据库中的值,现在标准差不是 0
猜你喜欢
  • 2018-01-09
  • 2022-08-12
  • 2011-06-12
  • 2014-11-04
  • 1970-01-01
  • 1970-01-01
  • 2015-05-29
  • 2020-01-08
  • 1970-01-01
相关资源
最近更新 更多