SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot silverviper's Avatar
    Join Date
    May 2005
    Location
    Norway
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need help with mysql data to cake chart

    Hi

    I have some kind of contact form that collects

    name
    adress
    zip code
    city

    and stores it in a mysql database.

    I need to count the occurance of each zip code in order to calculate
    the percentage each zip code represents. Then store the querry result in an array

    the four digit index represents the zip code, and the key represents the occurrance of each zip code

    array( '4512' => 4, '2356' => 9, '9785' => 15, '0145' => 26 );


    it's been a few years since i've done any php. any help is much apprechiated.

    thx

    Silverviper
    He who knows that enough is enough will always have enough

  2. #2
    SitePoint Zealot silverviper's Avatar
    Join Date
    May 2005
    Location
    Norway
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've tried the following

    Code:
    $query = "SELECT pstnr,COUNT(pstnr) AS count FROM addresses GROUP BY pstnr";
    $result = mysqli_query($dbc, $query);
        
    $zipcounts = array();
        
    while ($row = mysqli_fetch_array($result, MYSQL_ASSOC)) {
        $zipcounts[$row['pstnr']] = $row['count'];
    }
    give me this error

    Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result
    He who knows that enough is enough will always have enough

  3. #3
    SitePoint Zealot silverviper's Avatar
    Join Date
    May 2005
    Location
    Norway
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile

    found a typo

    Code:
    $query = "SELECT pstnr,COUNT(pstnr) AS count FROM addresses GROUP BY pstnr";
    $result = mysqli_query($dbc, $query);
        
    $zipcounts = array();
        
    while ($row = mysqli_fetch_array($result, MYSQL_ASSOC)) {
        $zipcounts[$row['pstnr']] = $row['count'];
    }
    corrected code

    Code:
    $query = "SELECT pstnr,COUNT(pstnr) AS count FROM addresses GROUP BY pstnr";
    $result = mysqli_query($dbc, $query);
        
    $zipcounts = array();
        
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
        $zipcounts[$row['pstnr']] = $row['count'];
    }
    He who knows that enough is enough will always have enough

  4. #4
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Your query failed - try executing it outside of php or see the contents of msqli_error_*.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  5. #5
    SitePoint Zealot silverviper's Avatar
    Join Date
    May 2005
    Location
    Norway
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi

    I got it working, the code follows for those who are interessted

    PHP Code:
    <?

    include 'includes/config.php';
    include 
    'includes/functions.php';

    connectDB($host$dbUser$dbPass$dbName);
        
    $results queryDB("SELECT pststed,COUNT(pststed) AS zipcount FROM konkurranse GROUP BY pststed"); 
    closeDB(); 

    $pststedcounts = array();   
    $psttot 0;   
    while (
    $row mysql_fetch_array($resultsMYSQL_ASSOC)) {
       
    $pststedcounts[trim($row['pststed'])] = $row['zipcount'];
       
    $psttot $psttot $row['zipcount'];

    ?>
    <table border="0">
      <tr>
    <?


    foreach( $pststedcounts as $key => $value){
        
    $width = ($value 100 $psttot)*30;
        if(
    $key==""){
            
    $key="???";
        }
        print 
    "<tr><td width='130'>$key</td>";
        print 
    "<td align='right'>".$value*100/$psttot."%</td>";
        print 
    '<td><img src="img/graph.jpg" width="'.$width.'" height="14"></td></tr>';
    }



    ?>
      </tr>
    </table>
    I usually use 3 functions when doing mysql stuff, probably slows down my code, but speed isn't very important in this case.

    My results are shown in a bar chart for now, i'll replace the bar chart with a Pie chart later.
    He who knows that enough is enough will always have enough


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
  •