1

What I need is to take an issue id and track user submitted upvotes by the hour. I'm running into a wall when I try to get the data out of the array coherently for PHPExcel charting purposes.

I end up with this $hourlyUpvotesTodayResult[] array, which has the correct number of votes for each hour (verified through print_r), but I want to group them in such a way where I can do something like "for each issue id display the hours 0-24 and the total votes for each hour". Maybe I'm overthinking it and missing something simple.

Also, please ignore the ancient MySQL - I'm working on some existing code, I know it's deprecated and I'll get around to updating it someday :D

while ($todayInfoRow = mysql_fetch_array($todayInfoQuery))
    {
    $issue_entry_id_today = array($todayInfoRow['issue_entry_id']);
    foreach ($issue_entry_id_today as $issue_entry_id_today)
        {
        $todayInfoUpvotes = "SELECT id from $db.$tb2 WHERE date = '".$date."' and issue_entry_id = '".$issue_entry_id_today."' ORDER BY issue_entry_id DESC";
        $todayInfoUpvotesQuery = mysql_query($todayInfoUpvotes)or die(mysql_error());
        $todayInfoUpvotesRow = mysql_num_rows($todayInfoUpvotesQuery);
        $todayInfoResult = array($issue_entry_id_today, $todayInfoUpvotesRow);
        }
    // get votes for individual hours for today 
    for ($hour_today=0; $hour_today<25; $hour_today++)
        {
            $hourlyUpvotesToday = "SELECT id from $db.$tb2 WHERE date = '".mysql_real_escape_string($date)."' AND issue_entry_id = '".$issue_entry_id_today."' AND hour = '".$hour_today."' ORDER BY issue_entry_id DESC";
            $hourlyUpvotesTodayQuery = mysql_query($hourlyUpvotesToday)or die(mysql_error());
            $hourlyUpvotesTodayRow = mysql_num_rows($hourlyUpvotesTodayQuery);
            $hourlyUpvotesTodayResult[] = array('id'=>$issue_entry_id_today, 'upvotes'=>$hourlyUpvotesTodayRow, 'hour'=>$hour);
        } 
    }
//debug print array
print_r($hourlyUpvotesTodayResult);
4

1 回答 1

1

Your code creates the array with a little extra info. Instead of making an array of:

Hour number

ID #

hour

votes

You'll make one that results in:

ID #

[Hour] -> Votes

This time, ID # will be the key, your Hour will be the X value and your Votes will be the Y value (referencing a basic X-Y axis chart). Your array will change to this:

$hourlyUpvotesTodayResult[$issue_entry_id_today] = array($hour => $hourlyUpvotesTodayRow);

You'll have to put the for statement entirely inside the above foreach statement. Then you can print each one by its issue number like you need.

Not sure about PHPExcel but you can post this by doing essentially the same foreach { for {} } group-loop, just this time without the SQL calls since it's already in the array.

Outside of the for() loop you would put something like:

print "ID number for this issue: " . $issue_entry_id_today . "<br>";

Inside that loop you'd have:

print "Hour " . $hour . " produced " . $hourlyUpvotesTodayResult[$issue_entry_id_today][$hour] . " votes.";

And you could put that into a table or div's or whatever you want.

Hope this helps.

于 2013-09-18T03:55:31.713 回答