SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Evangelist
    Join Date
    Apr 2004
    Location
    Boston
    Posts
    482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    array of values from database to data string

    Hello,

    I need to create an array of numerical data from some mathematical queries from my DB for some graphs using Jpgraph. I have the arrays displaying correctly but I am having some problems getting them into the axis points on the graph.

    I basically need to transform this:

    PHP Code:
    //horizontal placment
    $val[1] = 18;
    $val[2] =  16;
    $val[3] = 5;

    //vertical placment
    $pl[1] = 14;
    $pl[2] = 9;
    $pl[3] = 22
    into this
    PHP Code:
    $data1 = array(); //should have the $val array here
    $data2 = array(); //should have the $pl array here. 
    Currently it works fine with one value in each array but when there are multiples it seems to either average or add the values and place them on the plot when I need them to list them one by one.

    $data1 and $data2 should correspond with each other. $val[1] and $pl[1] mark the horizontal and vertical placment for tick mark 1 on the plot respectively.

    Currenty my $data1 and $data2 varialbes look like this:

    PHP Code:
    $data1 = array($val);
    $data2 = array($pl); 
    Thanks.

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    They're already plain old arrays, although indexing usually starts at 0. There is no transformation needed:

    PHP Code:
    $data1 $val;
    $data2 $pl

  3. #3
    Non-Member
    Join Date
    Apr 2009
    Posts
    115
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How are you adding the array with array()? Are you seperating the values by commas? Can you post the code you attempted in which the values either add or average the values?

    ~

  4. #4
    SitePoint Evangelist
    Join Date
    Apr 2004
    Location
    Boston
    Posts
    482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by a2K View Post
    How are you adding the array with array()? Are you seperating the values by commas? Can you post the code you attempted in which the values either add or average the values?

    ~
    I'm not the addition comes from a sum(column) query.

    PHP Code:
    $val1 select sum(valfrom table where placement ='horiz';
    $val2 select sum(valfrom talbe where placment ='vert'

    $i=0;
    while(
    $row_val1){
    $i++;
    $val[$i] = $row_val1['sum(val)'];
    }

    while(
    $row_val2){
    $i++;
    $pl[$i] = $row_val2['sum(val)'];


  5. #5
    SitePoint Evangelist
    Join Date
    Apr 2004
    Location
    Boston
    Posts
    482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dan Grossman View Post
    They're already plain old arrays, although indexing usually starts at 0. There is no transformation needed:

    PHP Code:
    $data1 $val;
    $data2 $pl
    this didn't work.

  6. #6
    Non-Member
    Join Date
    Apr 2009
    Posts
    115
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You have a mistake in your SQL

    $val2 = select sum(val) from talbe where placment ='vert'

    Should be table

    Edit:

    You also need a semi-colon at the end of that statement. You omitted one in your code

    ~

  7. #7
    SitePoint Evangelist
    Join Date
    Apr 2004
    Location
    Boston
    Posts
    482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by a2K View Post
    You have a mistake in your SQL

    $val2 = select sum(val) from talbe where placment ='vert'

    Should be table

    ~
    Thanks, The queries are returning the correct values in my code. That was a typo in an abbreviated version of my actual queries. It's the array's that aren't working.

  8. #8
    SitePoint Evangelist
    Join Date
    Apr 2004
    Location
    Boston
    Posts
    482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    when I var_dump() each of the queries they are showing the proper key and value. It's something with the $datax and $datay line.

    This is the error I am getting now:

    array(1) { [0]=> array(2) { [0]=> string(2) "18" [1]=> string(2) "16" } }
    array(1) { [0]=> array(2) { [0]=> string(2) "14" [1]=> string(2) "15" } }

    JpGraph Error Either X or Y data arrays contains non-numeric values. Check that the data is really specified as numeric data and not as strings. It is an error to specify data for example as '-2345.2' (using quotes).
    The array values are correct but it looks like i have to strip the quotes out of the values. I've tried str_replace(), number_format() and int() and nothing is working. Any ideas?
    Last edited by Rmazin; Apr 10, 2009 at 14:18.


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
  •