SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Wizard spence_noodle's Avatar
    Join Date
    Jan 2004
    Location
    uk, Leeds (area)
    Posts
    1,264
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)

    Red face a script that will build up data for a graph (was "help me guys")

    hi,

    what i'm trying to do is write a script that will build up data for a graph.

    the graph data need to be in this format:

    PHP Code:
     $chart 'chart_data' ][ ] = array ( """Jan""Feb""Mar""Apr""May""Jun""Jul""Aug""Sep""Oct""Nov""Dec" );
     
    $chart 'chart_data' ][ ][ ] = "Hits";
     
    $chart 'chart_data' ][ ][ ] = "Impressions"
    as you can see it's a multi-dimensional array consiting Month, Hits and Impressions.

    I have a database with month, hits and imp data.

    e.g.:

    month = Jan
    hits = 324
    imp = 2431

    so far i've come up with this test script:
    PHP Code:
     <?php
     
    include 'config.inc.php';
     
    $month_array = array ( => "Jan""Feb""Mar""Apr""May""Jun""Jul""Aug""Sep""Oct""Nov""Dec");
     
    $results = array();
     
     
    $bannerinfo mysql_query("SELECT hits, banner_imp, month FROM gallery_stats WHERE banner_id='35' AND year='2006' ");
         while(
    $all_info mysql_fetch_array$bannerinfo ))
         {
             
    $results[] = array('mon' => $all_info['month'], 'hits' => $all_info['hits'], 'imp' => $all_info['banner_imp']);
         }
         
    mysql_free_result$bannerinfo );
     
         foreach(
    $month_array as $num => $mon)
         {
             foreach(
    $results as $key => $value)
             {
                 if(
    $value == $mon)
                 {
                     echo 
    "$mon $hits\n";
                 }
                     else
                 {
                     echo 
    "$mon 0\n";
                 }
             }
         }
     
    ?>
    this prints out: Jan 0 Jan 0 Jan 0 Feb 0 Feb 0 Feb 0 Mar 0 Mar 0 Mar 0 Apr 0 Apr 0 Apr 0 May 0 May 0 May 0 Jun 0 Jun 0 Jun 0 Jul 0 Jul 0 Jul 0 Aug 0 Aug 0 Aug 0 Sep 0 Sep 0 Sep 0 Oct 0 Oct 0 Oct 0 Nov 0 Nov 0 Nov 0 Dec 0 Dec 0 Dec 0

    i'm after(e.g.): Jan 0 Feb 0 Mar 0 Apr 321 May 146 Jun 321... Oct 0 Nov 0 Dec 0

    i've only used the month and hits within the test script. I will be needing the impressions adding too.

    sorry for the lack of // tell me what this does.

    as i said it's a test script and i've working on it far too long. lol

    any help or ideas in the correct direction i would be very appreciative.

    cheers,

    spence

  2. #2
    is_empty(2); foofoonet's Avatar
    Join Date
    Mar 2006
    Posts
    1,000
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try and break this down a bit:

    Try this:
    $all_info = mysql_fetch_array( $bannerinfo )
    echo '<pre>';
    print_r($all_info); //see if your getting the right stuff out of your dbase

    if thats cool, ask yourself why you keep the months in the database AND you make an array in php too?

    Why cant you reuse the month from the dbase?

    If the select is cool, then do the same print_r() trick with each of your arrays.

    (print_r means Recursive Print of an object of an array and is a sanity saver AND I can remember it...)
    Upgrading to Mysql 5? Auto-increment fields now strict
    use NULL
    Or zero or leave the field name out completely.

  3. #3
    SitePoint Wizard spence_noodle's Avatar
    Join Date
    Jan 2004
    Location
    uk, Leeds (area)
    Posts
    1,264
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    thankx for your reply.

    i'll check it out.

    the idea is to show if each month has data as a bar graph.

    will let you know what i get.

  4. #4
    SitePoint Wizard spence_noodle's Avatar
    Join Date
    Jan 2004
    Location
    uk, Leeds (area)
    Posts
    1,264
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)

    I've done it...in away

    the code above was trying to display the months without data as '0' hits and '0' impressons then add the month(s) with data, all as colored bars, but i had problems adding the months with no data with the months with data.

    in the end i decided that i could get the data from the database for the months that had data into an array in the correct order.

    then split the data up into two arrays, 'Hits' and 'Imps'.

    which came out like this:

    PHP Code:
     (hits array)
     array
     {
           [
    5] => 34
           
    [6] => 123
     
    }
     
     (
    imps array)
     array
     {
           [
    5] => 2141
           
    [6] => 1341
     

    so now i've split the data into two arrays i can now place each array into the correct colum month for the bar graph.

    here's the code for the 3D bar Graph for PHP/SWF Charts:

    PHP Code:
     $hits = array();
     
    $imps = array();
     
     
    $ban_id_sent $_REQUEST['banid'];
     
    $ban_year_sent $_REQUEST['year'];
     
     
    $bannerinfo mysql_query("SELECT hits, banner_imp, month FROM gallery_stats WHERE banner_id='".$ban_id_sent."' AND year='".$ban_year_sent."' ORDER BY stats_id");
         while(
    $all_info mysql_fetch_array$bannerinfo ))
         {
             
    $hits[$all_info['month']] = $all_info['hits'];
             
    $imps[$all_info['month']] = $all_info['banner_imp'];
         }
         
    mysql_free_result$bannerinfo );
         
     
    //create the PHP multi-dimensional array in mixed ways
     
    $chart 'chart_data' ][ ] = array ( """Jan""Feb""Mar""Apr""May""Jun""Jul""Aug""Sep""Oct""Nov""Dec" );
     
    $chart 'chart_data' ][ ][ ] = "Hits";
     
    $chart 'chart_data' ][ ][ ] = "Impressions";
     
     for ( 
    $col 1$col <= 12$col++ ) {
         
    $chart 'chart_data' ][ ][ $col ] = $hits[$col];
         
    $chart 'chart_data' ][ ][ $col ] = $imps[$col];
     }
     
     
    the rest.... 
    this will show only the data for that month(s) in the year chosen. The months with no data will just have no colored bar's showing.

    hope this helps others with PHP/SWF Charts coding, etc..

    spence

  5. #5
    SitePoint Wizard spence_noodle's Avatar
    Join Date
    Jan 2004
    Location
    uk, Leeds (area)
    Posts
    1,264
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    cheers, to the person who changed the title

    spence


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •