0

due to an issue i am facing with IE 8 , its not rendering some charts using Highcharts the issue that i am using the below code to extract my data from the database

the PHP code return the below

series: [{
            type: 'pie',


            data: [

            ['FirstRowItem',21],['SecondRowItem',10],['ThirdRowItem',6],                ]
        }]

and the DB request as the following

series: [{
            type: 'pie',

            data: [ <?php

while($row = mysql_fetch_array($getitemscount)){
echo "['";
echo $row['items'];
echo "',";
echo $row['item countt'];
echo "],";`}

?>
            ]
        }]

it works fine on IE9, chrome, Firefox. but on IE8 because of the , by the end of ['ThirdRowItem',6], it doesnt work as it says undefined variable.

what i was thinking about if there is any piece of code that would just delete that last comma (the lazy way) or any other way to get the DB data without it?

Thank you in advance.

*********** EDIT ****************

a much better way HERE

Community
  • 1
  • 1
Ahmed ElGamil
  • 179
  • 1
  • 13

2 Answers2

3

Don't concatenate manually, but use json_encode() instead:

$data = array();
while($row = mysql_fetch_array($GetCallDriversResultPie)) {
    $data[] = array($row['CallDriver'], (int)$row['Calls count']);
}

echo json_encode($data);
zerkms
  • 249,484
  • 69
  • 436
  • 539
  • +1 good solution if your environment is PHP5+ or otherwise supports the JSON lib. – Patrick Moore Mar 10 '13 at 21:29
  • thank you for your reply but the issue is the returned items now look like ["FirstRowItem","21"],["SecondRowItem","10"],["ThirdRowItem","6"] with out the , . but the chart wont load up any values it has to be returned on this format ['firstitem',21],['seconditem',10],['thirditem',6] – Ahmed ElGamil Mar 10 '13 at 21:32
  • @Ahmed ElGamil: I don't see how the issue is related to your current question or my answer. Does the generated output match what you expected? If the type of second element is the issue - see my change with `(int)` modifier – zerkms Mar 10 '13 at 21:33
  • its about Highcharts that draws charts by some inserted values. the chart will not identify the returned items using it this way because it gets returned like ["FirstRowItem","21"] , but it should be like ['FirstRowItem',21] . by that i mean single quotes and no double quotations for the numbers. else the chart wont load up. – Ahmed ElGamil Mar 10 '13 at 21:42
  • @Ahmed ElGamil: so have you checked the second version of the code I've sent 10 minutes ago? – zerkms Mar 10 '13 at 21:43
  • @zerkms yes, its now ["FirstRowItem",21] , its double quotations for the item value. any way to get it to be single quotation ? – Ahmed ElGamil Mar 10 '13 at 21:46
  • @Ahmed ElGamil: there is no difference between single and double quotes (as a string delimiter) in JS. No difference at all. – zerkms Mar 10 '13 at 21:49
  • i am really not sure but Highcharts doesn't render the chart with double quotes, maybe this is how its coded to work like. an example for what i mean here [link](http://jsfiddle.net/gh/get/jquery/1.7.2/highslide-software/highcharts.com/tree/master/samples/highcharts/demo/pie-basic/) – Ahmed ElGamil Mar 10 '13 at 21:59
  • @Ahmed ElGamil: I changed the quotes to double and nothing changed. http://jsfiddle.net/SEtzQ/ "doesn't render"??? I'm sure you've just generated the wrong configuration object for the charts – zerkms Mar 10 '13 at 22:01
  • oh yes i see . forgive me i just noticed that there is an extra [ ] for the whole data set is returned. so instead of just ["FirstRowItem",21] its back like [["FirstRowItem",21]] . which make sense now. sorry for the confusion, i am still on the learn path about all of this. any way to get rid of the extra [ ] ? its not before each returned value its on the whole set. – Ahmed ElGamil Mar 10 '13 at 22:06
  • update . working fine now . just removed the quotes from the code. thank you very much :) – Ahmed ElGamil Mar 10 '13 at 22:13
  • @Ahmed ElGamil: it's not extra `[]` - you're creating array of arrays, so the result is `[ [...], [...], [...] ]` – zerkms Mar 10 '13 at 22:19
0

One method would be to add data to a variable, and after processing your result set from MySQL, use substr() to trim to the desired length (-1 character, the last comma).

series: [{
            type: 'pie',

            data: [ <?php

$result = '';
while($row = mysql_fetch_array($GetCallDriversResultPie))`{
    $results .= "['" . $row['CallDriver'] . "'," . $row['Calls count'] . "]," ;
}

echo ( substr( $results, 0, -1 ) ); // echo all of $results, except the last character

?>
            ]
        }]
Patrick Moore
  • 13,251
  • 5
  • 38
  • 63