Jump to content

Graphing data


doddsey_65

Recommended Posts

Im trying to get some data from the database so i can add it to a graph. The graph points are generated via $datay which is an array built via the database query. The query pulls all the info from the topics table, if the month of which the topic was created changed it adds a new point to the graph with a value of the amount of topics within the month.

 

Based on my database values:

 

$datay[0] should be 1

$datay[1] should be 4

$datay[2] should be 5

 

$datay = array();
$datax = array();
$db = db_pdo::$instance;

$sql = "SELECT * FROM ".TBL_PREFIX."topics
        ORDER BY t_time_posted DESC";
    
$sth = $db->query($sql);

$row = $sth->fetchAll();
$count = 0;
foreach($row as $key => $val)
{
    $date[$key] = date('m', $row[$key]['t_time_posted']);

    if(date('m', $row[$key]['t_time_posted']) != $date[$key-1])
    {
        $count++;
        $datay[] = $count;
        $count = $key;
    }

    $_count = count($row)-$count;

    $datay[0] = $_count;
}    

 

unfortunatly i am not getting those results. instead i get

 

$datay[0] = 1

$datay[1] = 1

$datay[2] = 6

 

This means i should have 8 topics in total, but i have 10.

 

Anyone have any ideas? Its nothing to do with the graph, just the way i am adding data to the datay array.

 

Thanks

 

Link to comment
Share on other sites

Alright, the first thing I would do is change the sql query to this

 

$sql = "SELECT * FROM ".TBL_PREFIX."topics
        GROUP BY YEAR (t_time_posted) , MONTH(t_time_posted)";

 

because all you have is timestamps..

 

Now are you comparing each row based on what given date?

Link to comment
Share on other sites

thanks for the help so far, ive used this:

 

$sql = "SELECT COUNT(*), t_time_posted FROM ".TBL_PREFIX."topics
        GROUP BY MONTH(FROM_UNIXTIME(t_time_posted))";
    
$sth = $db->query($sql);

$row = $sth->fetchAll();
$count = 0;
foreach($row as $key => $val)
{
    $datay[] = $row[$key]['COUNT(*)'];
    //$datax[] = date('M',$row[$key]['t_time_posted']);
    // should have 1 in may, 4 in july and 5 in august
}    

 

which works in the graph, but since im only getting dates from the time_posted, the months which have 0 topics are missed out. How would i go about including them?

 

 

Link to comment
Share on other sites

They arent being included. Even if there were no topics within a month i still want to display that month. For example the data i have so far is:

 

May: 1 topic

July: 4 topics

Aug: 5 topics

 

But i want to include June, march and april as this will make up 6 months back from the current month.

 

Link to comment
Share on other sites

It prints out 3, because the topics in the database only span 3 months(may, july and august)

 

Im thinking i may have to do somthing like a for loop to fill in the blanks but thats where im stuck

 

$sql = "SELECT COUNT(*), t_time_posted FROM ".TBL_PREFIX."topics
        GROUP BY MONTH(FROM_UNIXTIME(t_time_posted))";
    
$sth = $db->query($sql);

$row = $sth->fetchAll();
$count = 0;
foreach($row as $key => $val)
{
    $datay[$key] = $row[$key]['COUNT(*)'];
    $datax[$key] = date('M',$row[$key]['t_time_posted']);
    echo $key;
    // should have 1 in may, 4 in july and 5 in august
}    

Link to comment
Share on other sites

Thanks for all your hard work, ive got it working now with:

 

$sql = "SELECT COUNT(*), t_time_posted FROM ".TBL_PREFIX."topics
        GROUP BY MONTH(FROM_UNIXTIME(t_time_posted))";
    
$sth = $db->query($sql);

$row = $sth->fetchAll();
$count = 0;
foreach($row as $key => $val)
{
    $datay[date('n',$row[$key]['t_time_posted'])-1] = $row[$key]['COUNT(*)'];
    $datax[] = date('M',$row[$key]['t_time_posted']);
    
    // should have 1 in may, 4 in july and 5 in august
}    

for($i=1; $i<=12; $i++)
{
    if(empty($datay[$i]))
    {
        $datay[$i] = 0;
    }
}

ksort($datay);

//dump($datay);

foreach($datay as $key => $val)
{
    $labels[] = date("M", mktime(0, 0, 0, ($key)));
}

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.