Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8531955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:36:07+00:00 2026-06-11T09:36:07+00:00

I need to create a basic column chart using highcharts. Here’s my PHP code:

  • 0

I need to create a basic column chart using highcharts. Here’s my PHP code:

$query = "SELECT campaign_id, kpi, label, val FROM table WHERE id = $1";

$result = pg_prepare($db, "report", $query);
$result = pg_execute($db, "report", array($campaignID));

while ($row = pg_fetch_array($result, NULL, PGSQL_ASSOC))
{
    $response['xlabels'][] = $row['label'];
    $response['xdata'][] = (float)$row['val'];
}

pg_free_result($result);
pg_close($db);

echo json_encode($response);

The PostgreSQL query returns data as follows (campaign_id = 5, for instance):

 id |    kpi    | label | val  
----+-----------+-------+------
  5 | Voter     | East  | 0.18 
  5 | Non-Voter | East  | 0.12 
  5 | Voter     | West  | 0.82 
  5 | Non-Voter | West  | 0.88 

My API saves xlabels and xdata to xAxis.categoriesand series:

$.get('vote_api.php', {'id' : id}, function(data) {

    options.chart.type = 'column';

    options.xAxis.categories = data.xlabels;
    options.series = data.xdata;   

    chart = new Highcharts.Chart(options);

    }, "json");

The right data format for kpi (Voter vs. Non-Voter) as my series and label (East vs West) as my xAxis should be (please refer to JSFiddle):

xAxis.categories = ['East', 'West'];
series = [{name: 'Voter',
            data: [0.18, 0.82]},
          {name: 'Non-Voter',
            data: [0.12, 0.88]}]

However my while loop part $response['xlabels'][] = $row['label']; $response['xdata'][] = (float)$row['val']; give me something like this:

[xlabels] => Array
    (
        [0] => East
        [1] => East
        [2] => West
        [3] => West
    )

[xdata] => Array
    (
        [0] => 0.18
        [1] => 0.12
        [2] => 0.82
        [3] => 0.88
    )

Would some one give some hints how to do this? I’d really appreciate it!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T09:36:08+00:00Added an answer on June 11, 2026 at 9:36 am

    You may try :

    $query = "SELECT campaign_id, kpi, label, val FROM table WHERE id = $1";
    
    $result = pg_prepare($db, "report", $query);
    $result = pg_execute($db, "report", array($campaignID));
    
    while ($row = pg_fetch_array($result, NULL, PGSQL_ASSOC))
    {
        if (!in_array($row['label'], $response['xlabels']))
        {
            $response['xlabels'][] = $row['label'];
        }
        $response['xdata'][ $row['kpi'] ][] = (float)$row['val'];
    }
    
    pg_free_result($result);
    pg_close($db);
    

    The first change :

        if (!in_array($row['label'], $response['xlabels']))
        {
            $response['xlabels'][] = $row['label'];
        }
    

    avoids to save several times “East” and “West” to your xlabels array.

    The second change :

        $response['xdata'][ $row['kpi'] ][] = (float)$row['val'];
    

    Will add a dimension to the xdata array : values are assigned to different arrays according to their “kpi”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create a prn file using java. Did a basic googling to
I've got some code here that will create a basic table in a Word
We need to create a basic PDF reader running on J2ME. While there are
i need create a variable with parent subclass. Example: Parent Class <?php class parentClass
I have a basic MySQL query: $getFeed = SELECT posts.postID, posts.postTitle, ..... FROM posts
I'm using a gridview with checkboxes and i need to create a table row
I need to create input fields on the basis of which number was chosen
I need create custom dialog and put JPanel into it. Is it possible?
i need create an email list sending to many emails. what is best solution
I need create clone repository. but I do not know where can I get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.