2013-03-05 20:10 阅读 103


I have a SQLite database and am trying to graph data on a linechart using Highcharts, PHP, and Javascript. I am graphing one series per user (a user being a text value per tuple), but am running into trouble with retrieving a subsequent user via a PHP loop.

$dbhandle = sqlite_open('db/test.db', 0666, $error);

if (!$dbhandle) die ($error);
$query5 = "SELECT DISTINCT User FROM Events";

$ok0 = sqlite_query($dbhandle, $query5, $error_msg);

if (!$ok0) 
    die("dead" . $error_msg);
$rows = sqlite_num_rows($ok0);

"for(var i=2; i<$rows; i++) // start of JS loop. 
                           //$rows is 4; I am graphing 2 series here

  $array = sqlite_fetch_array($ok0, SQLITE_ASSOC); // $ok0 is the unique list
// of users. After graphing one series, I want to grab the next user to graph

        echo "chart.addSeries({
                name: '{$array["User"]}',
                data: [";
          for($i=0; $i<$diff+1; $i++)
            $target = date("D, j M", (strtotime($_GET["start"]) + $i * 24 * 3600));
            $query6 = "SELECT * FROM Events WHERE User = '{$array["User"]}' AND Start LIKE '%{$target}%'";
            $result6 = sqlite_query($dbhandle, $query6);
            if (!$result6) die("Cannot execute query.");
            $num = sqlite_num_rows($result6);            
              echo $num;
              echo $num . ", ";


$date = DateTime::createFromFormat('D M d Y', urldecode($_GET["start"])); 
echo $date->getTimestamp()*1000;?>,
            pointInterval: 24 * 3600 * 1000 // one day

      <?php echo "}";?> // end of JS loop

The result does graph two additional series, but they are both the same data from the same user. It doesn't look like the loop with sqlite_fetch_array() correctly returns the next user. Can anyone see the problem here? Maybe something with the way I'm integrating Javascript with PHP?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    dongping4901 dongping4901 2013-03-06 20:01

    I've figured it out. I believe that due to PHP being executed server-side, the block of code within the echoed Javascript loop results in the same values when the browser executes the Javascript. The server simply echoes the string that represents the js loop, and will run the code block after. When everything is sent to the browser, the Javascript will loop normally and simply output the results of the code block twice. I've just forgone the JS loop via PHP and replaced it with a PHP loop.

    点赞 评论 复制链接分享
  • drlnwji79147769 drlnwji79147769 2013-03-06 00:03

    This line of code always does the same thing every time you call it:

    $query6 = "SELECT * FROM Events WHERE User = '{$array["User"]}' AND Start LIKE '%{$target}%'";

    $array supposedly has a list of distinct users, but you don't show the query for $ok0. You're doing the user lookup inside this loop for($i=0; $i<$diff+1; $i++), but you use none of those variables in determining which user to fetch.

    I'd like to post the correct way to look up the next user from $array, but I have no idea what the structure is. If you can't figure out how to make your $query6 unique to each user, you'll have to post more information, like a var_dump of $array or the query text of $ok0.

    Maybe you just need to move this line $array = sqlite_fetch_array($ok0, SQLITE_ASSOC); inside your for loop, but then you run the risk of $i and $diff+1 disagreeing with the number of rows available from your result.

    点赞 评论 复制链接分享