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;

}

它应该用不同的数值计算不同日期的数组,但页面拒绝加载 - 但在我把这段代码放在那里之前它确实加载了。

4

3 回答 3

1

这段代码可能可以用另一种方式编写。

建议:

请改用 MySQLi 或 PDO。这是一些有关数据库 API 选择的文档。例子:

$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) 派上用场。

于 2012-10-20T22:00:51.913 回答
1

为什么你在 switch case 中有重复的情况?

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;
}

我已修改您的代码以检查所有 DUMPS,请查看下面的 ant 测试转储:

    $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 说这段代码可以写得更好,是的,确实如此。少写,多做。

于 2012-10-20T21:52:13.620 回答
0

很可能是一个错误。

在 /var/logs/apache2/error_log.log 查看您的 apache 的 php 日志(或您正在使用的任何日志)

或者类似的东西...

于 2012-10-20T21:43:03.240 回答