2012-10-20 67 views
0

我的頁面上的代碼似乎沒有加載 - 我認爲程序邏輯對它來說有點多。這段代碼能否產生一個無限循環?

$resultromney = mysql_query("SELECT category, value, timestamp from results where name='".$chartname2."'"); 
if (!$resultromney) { 
    die('Invalid query, please contact administrator'); 
} 

$posromney = 0; 
$negromney = 0; 
$dateromney = "1/1/2012"; 
$romray = array(); 

while ($twitromney = mysql_fetch_assoc($resultromney)) 
{ 

$romneytime = intval($twitromney["timestamp"]); 
$romneydate = date('m/d/Y', $romneytime); 
$romlinearray = array(); 
$romlinearray["date"] = null; 
$romlinearray["value"] = null; 
$romneydiff = date_diff($dateromney, $romneydate); 

$PLromney = 0; 
$NLromney = 0; 
$totalromney = 0; 

if ($romneydiff == 0) { 


switch ($twitromney["category"]) { 
    case "composed": 
     $PLromney = $PLromney + $twitromney["value"]; 
break; 
    case "elated": 
     $PLromney = $PLromney + $twitromney["value"]; 
break; 
    case "clearheaded": 
     $PLromney = $PLromney + $twitromney["value"]; 
break; 
    case "agreeable": 
     $PLromney = $PLromney + $twitromney["value"]; 
break; 
    case "energetic": 
     $PLromney = $PLromney + $twitromney["value"]; 
break; 
    case "unsure": 
     $NLromney = $NLromney + $twitromney["value"]; 
break; 
    case "tired": 
     $NLromney = $NLromney + $twitromney["value"]; 
break; 
    case "depressed": 
     $NLromney = $NLromney + $twitromney["value"]; 
break; 
    case "guilty": 
     $NLromney = $NLromney + $twitromney["value"]; 
break; 
    case "confused": 
     $NLromney = $NLromney + $twitromney["value"]; 
break; 
    case "anxious": 
     $NLromney = $NLromney + $twitromney["value"]; 
break; 
    case "hostile": 
     $NLromney = $NLromney + $twitromney["value"]; 
break; 
    case "confident": 
     $PLromney = $PLromney + $twitromney["value"]; 
break; 
} 
$totalromney = $PLromney - $NLromney; 

$romlinearray["value"] = $totalromney + $romlinearray["value"]; 
} 

else { 

$dateromney = $romneydate; 

switch ($twitromney["category"]) { 
    case "composed": 
     $PLromney = $PLromney + $twitromney["value"]; 
break; 
    case "elated": 
     $PLromney = $PLromney + $twitromney["value"]; 
break; 
    case "clearheaded": 
     $PLromney = $PLromney + $twitromney["value"]; 
break; 
    case "agreeable": 
     $PLromney = $PLromney + $twitromney["value"]; 
break; 
    case "energetic": 
     $PLromney = $PLromney + $twitromney["value"]; 
break; 
    case "unsure": 
     $NLromney = $NLromney + $twitromney["value"]; 
break; 
    case "tired": 
     $NLromney = $NLromney + $twitromney["value"]; 
break; 
    case "depressed": 
     $NLromney = $NLromney + $twitromney["value"]; 
break; 
    case "guilty": 
     $NLromney = $NLromney + $twitromney["value"]; 
break; 
    case "confused": 
     $NLromney = $NLromney + $twitromney["value"]; 
break; 
    case "anxious": 
     $NLromney = $NLromney + $twitromney["value"]; 
break; 
    case "hostile": 
     $NLromney = $NLromney + $twitromney["value"]; 
break; 
    case "confident": 
     $PLromney = $PLromney + $twitromney["value"]; 
break; 
} 
$totalromney = $PLromney - $NLromney; 

$romlinearray["date"] = $romneydate; 
$romlinearray["value"] = $totalromney; 
} 

$romray[] = $romlinearray; 

} 

它應該計算不同天,不同數量的陣列的值,而是頁面被拒絕加載 - 但它確實負荷之前,我把這個代碼在那裏。

+1

首先評論一段代碼,並嘗試反覆運行。 –

+2

對不起,但是羅姆尼的這個問題太多了。 – kalpaitch

+0

在旁邊,你有沒有聽說過跌落? – Sherlock

回答

0

可能是一個錯誤。

檢查出你的Apache的PHP日誌(或不管它是你正在使用)在/var/logs/apache2/error_log.log

或類似的東西...

1

爲什麼你在開關有案件反覆案件?

switch ($twitromney["category"]) { 
    case "composed": 
    $PLromney = $PLromney + $twitromney["value"]; 
break; 
    case "elated": 
    $PLromney = $PLromney + $twitromney["value"]; 
break; 
    case "clearheaded": 
    $PLromney = $PLromney + $twitromney["value"]; 
break; 
    case "agreeable": 
    $PLromney = $PLromney + $twitromney["value"]; 
break; 
    case "energetic": 
    $PLromney = $PLromney + $twitromney["value"]; 
break; 
    case "unsure": 
    $NLromney = $NLromney + $twitromney["value"]; 
break; 
    case "tired": 
    $NLromney = $NLromney + $twitromney["value"]; 
break; 
    case "depressed": 
    $NLromney = $NLromney + $twitromney["value"]; 
break; 
    case "guilty": 
    $NLromney = $NLromney + $twitromney["value"]; 
break; 
    case "confused":ss 
    $NLromney = $NLromney + $twitromney["value"]; 
break; 
    case "anxious": 
    $NLromney = $NLromney + $twitromney["value"]; 
break; 
    case "hostile": 
    $NLromney = $NLromney + $twitromney["value"]; 
break; 
    case "confident": 
    $PLromney = $PLromney + $twitromney["value"]; 
break; 
} 

代替以上使用的:

switch ($twitromney["category"]) { 
    case "composed": 
    case "elated": 
    case "clearheaded": 
    case "agreeable": 
    case "energetic": 
    case "confident": 
    $PLromney = $PLromney + $twitromney["value"]; 
break; 
    case "unsure": 
    case "tired": 
    case "depressed": 
    case "guilty": 
    case "confused": 
    case "anxious": 
    case "hostile": 
    $NLromney = $NLromney + $twitromney["value"]; 
break; 
} 

我修改你的代碼檢查所有轉儲,看看下面的螞蟻測試轉儲:

$resultromney = mysql_query("SELECT category, value, timestamp from results where name='".$chartname2."'") 

    var_dump('[1]:'.$resultromney); 

    if (!$resultromney) { 
     die('Invalid query, please contact administrator'); 
    } 

    $posromney = 0; 
    $negromney = 0; 
    $dateromney = "1/1/2012"; 
    $romray = array(); 

    while ($twitromney = mysql_fetch_assoc($resultromney)) 
    { 

     var_dump('[2]:'.$twitromney); 

    $romneytime = intval($twitromney["timestamp"]); 

     var_dump('[3]:'.$romneytime); 

    $romneydate = date('m/d/Y', $romneytime); 

     var_dump('[4]:'.$romneydate); 

    $romlinearray = array(); 
    $romlinearray["date"] = null; 
    $romlinearray["value"] = null; 

     var_dump('[5]:'.$romlinearray); 

    $romneydiff = date_diff($dateromney, $romneydate); 

     var_dump('[6]:'.$romneydiff); 

    $PLromney = 0; 
    $NLromney = 0; 
    $totalromney = 0; 

    if ($romneydiff == 0) { 

     var_dump('[7]:'.$romneydiff); 


    switch ($twitromney["category"]) { 

     var_dump('[8]:'.$twitromney); 

     case "composed": 
     case "elated": 
     case "clearheaded": 
     case "agreeable": 
     case "energetic": 
     case "confident": 
     $PLromney = $PLromney + $twitromney["value"]; 
    break; 
     case "unsure": 
     case "tired": 
     case "depressed": 
     case "guilty": 
     case "confused": 
     case "anxious": 
     case "hostile": 
     $NLromney = $NLromney + $twitromney["value"]; 
    break; 
    } 

     var_dump('[8]:'.$PLromney); 
     var_dump('[9]:'.$NLromney); 

    $totalromney = $PLromney - $NLromney; 

    $romlinearray["value"] = $totalromney + $romlinearray["value"]; 

     var_dump('[10]:'.$totalromney, $romlinearray); 

    } 

    else { 

    $dateromney = $romneydate; 

    switch ($twitromney["category"]) { 

     var_dump('[11]:'.$twitromney); 

     case "composed": 
     case "elated": 
     case "clearheaded": 
     case "agreeable": 
     case "energetic": 
     case "confident": 
     $PLromney = $PLromney + $twitromney["value"]; 
    break; 
     case "unsure": 
     case "tired": 
     case "depressed": 
     case "guilty": 
     case "confused": 
     case "anxious": 
     case "hostile": 
     $NLromney = $NLromney + $twitromney["value"]; 
    break; 
    } 

     var_dump('[12]:'.$PLromney); 
     var_dump('[13]:'.$NLromney); 

    $totalromney = $PLromney - $NLromney; 

    $romlinearray["date"] = $romneydate; 
    $romlinearray["value"] = $totalromney; 

     var_dump('[14]:'.$totalromney, $romlinearray); 

    } 

    $romray[] = $romlinearray; 


    var_dump('[15]:'.$romray); 

    } 

而且@Tivie說,這代碼可以寫得更好,是的,它是真的。少寫,多做。

1

這段代碼大概可以用另一種方式編寫。

意見建議:

改爲使用MySQLi或PDO。這裏有一些documentation regarding Database APIs choices。例如:

$mysqli = new mysqli("example.com", "user", "password", "database"); 
$result = $mysqli->query( 
    sprint_f("SELECT category, value, timestamp from results where name='%s'", $chartname2) 
); 

使用數組來得到結果後,將其存儲的數據,然後循環:

$romneytime = $romneydate = $romlinearray = $romneydiff = array(); 

while ($twitromney = $result->fetch_assoc()) 
{ 
    $romneytime[] = intval($twitromney["timestamp"]); 
    $romneydate[] = date('m/d/Y', $romneytime); 
    $romneydiff[] = date_diff($dateromney, $romneydate); 
} 

循環每一個試圖找出哪一個是給錯誤。 var_dump($ var)派上用場。