Solved

Changing structure of array

Posted on 2013-01-21
39
525 Views
Last Modified: 2013-01-23
Hi all,

I have the results of an array printing like this:

Array ( [exercise_timestamp] => 2012-11-25 12:13:49 [exercise_time] => 10 ) [1] => Array ( [exercise_timestamp] => 2012-12-01 12:20:37 [exercise_time] => 15 ) [2] =>)

Open in new window


Here is the code being used to generate this array

$result=array();
$query = mysql_query("SELECT exercise_timestamp,exercise_time FROM exercising_table WHERE uname = '$username' ");
while( $result[]=mysql_fetch_assoc($query)){}
print_r($result);

Open in new window



but I need them in this format:

          ['24/01/12',  25],
          ['25/01/12',  26],
          ['28/01/12',  27],
          ['24/02/12',  25],
          ['15/03/12',  26],
          ['24/04/12',  27],
          ['21/06/12',  11]

Open in new window


Could someone tell me how to do this?

Thanks,

D
0
Comment
Question by:TLN_CANADA
  • 19
  • 17
  • 2
  • +1
39 Comments
 
LVL 12

Expert Comment

by:sivagnanam chandrakanth
ID: 38804200
See below example for sorting by date value

$data = array(
    array(
        "title" => "Another title",
        "date"  => "Fri, 17 Jun 2011 08:55:57 +0200"
    ),
    array(
        "title" => "My title",
        "date"  => "Mon, 16 Jun 2010 06:55:57 +0200"
    )
);

function sortFunction( $a, $b ) {
    return strtotime($a["date"]) - strtotime($b["date"]);
}

usort($data, "sortFunction");
var_dump($data);

Open in new window

0
 
LVL 18

Expert Comment

by:ingwa
ID: 38804205
You can use the powerful built in date functions directly from mysql without having to iterate through php and waste precious milliseconds. I wrote a very detailed article on all the date formatting you can use as well as date adding, subtracting etc:

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_3935-Working-with-Dates-and-Time-using-PHP-and-MySQL.html

The first 3 samples deal with your specific request...just use date_format. Hope this helps.
0
 

Author Comment

by:TLN_CANADA
ID: 38804209
Thanks sivagnanam_c,

At the moment though it is still printing the same results of the array I mentioned above.

Here is what I have on the page:

$username = Phpfox::getUserBy('full_name'); 

	
$username=mysql_real_escape_string($username);

$result=array();
$query = mysql_query("SELECT exercise_timestamp,exercise_time FROM exercising_table WHERE uname = '$username' ");
while( $result[]=mysql_fetch_assoc($query)){} 
print_r($result);

function sortFunction( $a, $b ) {
    return strtotime($a["date"]) - strtotime($b["date"]);
}

usort($data, "sortFunction");
var_dump($data);

Open in new window


Thanks for your help,

D
0
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 38804218
You can try this:

<?php
$a = array(
	array( "exercise_timestamp" => "2012-11-25 12:13:49", "exercise_time" => 10 ),
	array( "exercise_timestamp" => "2012-12-01 12:20:37", "exercise_time" => 15 )
);                                  

foreach ($a as $arr){
	$a2[$arr["exercise_timestamp"]] = $arr["exercise_time"];
}

echo "<pre>";
var_dump($a2);

Open in new window

0
 

Author Comment

by:TLN_CANADA
ID: 38804229
Hi marqus,

In your example, how much of this code should I keep in order for it to work?

$username=mysql_real_escape_string($username);
$result=array();
$query = mysql_query("SELECT exercise_timestamp,exercise_time FROM exercising_table WHERE uname = '$username' ");
while( $result[]=mysql_fetch_assoc($query)){} 
print_r($result);

Open in new window

0
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 38804257
You should use my foreach immediately after the query:

$username=mysql_real_escape_string($username);
$result=array();
$query = mysql_query("SELECT exercise_timestamp,exercise_time FROM exercising_table WHERE uname = '$username' ");
while( $result[]=mysql_fetch_assoc($query)){} 
//print_r($result);
foreach ($result as $r){
	$result2[$r["exercise_timestamp"]] = $r["exercise_time"];
}

echo "<pre>";
var_dump($result2);

Open in new window


Cheers
0
 

Author Comment

by:TLN_CANADA
ID: 38804270
Thanks Marqus:

Here is what is printed :

array(23) {
  ["2012-11-25 12:13:49"]=>
  string(2) "10"
  ["2012-12-01 12:20:37"]=>
  string(2) "15"
  ["2012-12-01 12:20:55"]=>
  string(2) "15"

Open in new window


which is almost correctly except it needs to be in the format like this:

          ['24/01/12',  25],
          ['25/01/12',  26],
          ['28/01/12',  27],
          ['24/02/12',  25],
          ['15/03/12',  26],
          ['24/04/12',  27],
          ['21/06/12',  11]

Open in new window

0
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 38804289
Sorry, I had misunderstood:

foreach ($result as $r){
	$result2[] = array($r["exercise_timestamp"],  $r["exercise_time"]);
}

Open in new window

0
 

Author Comment

by:TLN_CANADA
ID: 38804304
Thank you for your time, it is still printing the timestamp in the full format though

 [0]=>
    string(19) "2013-01-14 20:23:27"
    [1]=>
    string(2) "34"
  }
  [11]=>
  array(2) {
    [0]=>
    string(19) "2013-01-17 16:31:23"
    [1]=>
    string(0) ""
  }

Open in new window


 and for it to work it has to look like this :

24/01/12

Thanks so much!

D
0
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 38804337
Sorry, I should read more carefully the questions before to answer them :-)

foreach ($result as $r){
	$d = explode(' ', $r["exercise_timestamp"]);
	$result2[] = array($d[0],  $r["exercise_time"]);
}

Open in new window

0
 

Author Comment

by:TLN_CANADA
ID: 38804356
That's okay :) I really appreciate your help with this. The date field is now printing in the format :

2013-01-19

but it needs to be in the format

19-01-2013

I need to know what code to put here so that I can echo the PHP and make it in this format exactly:

          ['24/01/12',  25],
          ['25/01/12',  26],
          ['28/01/12',  27],
          ['24/02/12',  25],
          ['15/03/12',  26],
          ['24/04/12',  27],
          ['21/06/12',  11]

Open in new window

0
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 38804374
I confirm what I said above about my precision...

foreach ($result as $r){
	$firstDate = explode(' ', $r["exercise_timestamp"]);
	$secondDate = explode('-', $firstDate[0]);
	$thirdDate = array_reverse($secondDate);
	$finalDate = $thirdDate[0] . '-' . $thirdDate[1] . '-' . $thirdDate[2];
	$result2[] = array($finalDate,  $r["exercise_time"]);
}

Open in new window


But it is not so elegant. Maybe you might be interested in consider the comment http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28003765.html#a38804205: I didn't read that article, but I think you could format the date in mysql query and then use the foreach illustarted in my comment ID: 38804289 to build the final array.
0
 

Author Comment

by:TLN_CANADA
ID: 38804383
This is perfect! Thank you! So now how do I echo this array so it appears in the format like this:

          ['24/01/12',  25],
          ['25/01/12',  26],
          ['28/01/12',  27],
          ['24/02/12',  25],
          ['15/03/12',  26],
          ['24/04/12',  27],
          ['21/06/12',  11]

Open in new window

0
 
LVL 30

Accepted Solution

by:
Marco Gasi earned 500 total points
ID: 38804410
foreach ($result2 as $r){
	echo "['" . $r[0] . "', '" . $r[1] . "']<br />";
}

Open in new window

0
 

Author Comment

by:TLN_CANADA
ID: 38804424
Here it is hardcoded in a javascript function right now. Can I just put your code into it?

function drawChart() {
        var data = google.visualization.arrayToDataTable([
          ['Months', 'Exercising Time (Mins)'],
          ['2012-11-25',  25],
          ['2012-11-26',  26],
          ['2012-11-28',  27],
		  ['2012-12-05',  25],
          ['2012-12-11',  26],
          ['2012-12-13',  27],
          ['2012-12-25',  11]
        ]);

Open in new window


I tried what you wrote about here instead of the hardcode but it did not work. Please could you write how to insert it here.

Thanks so much!

D
0
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 38804442
You can try this code, but in my opinion this is a totally different and new question.
function drawChart() {
        var PhpArray = <?php echo json_encode($result2); ?>;
        var data = google.visualization.arrayToDataTable([
          ['Months', 'Exercising Time (Mins)'],
          $PhpArray;
        ]);

Open in new window

0
 

Author Comment

by:TLN_CANADA
ID: 38804450
Yes, I agree with you. This last part doesn't work but that's okay. Thank you so much for your help! Have a great day!
0
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 38804463
Thanks for points and good luck with your project!
0
 

Author Comment

by:TLN_CANADA
ID: 38804468
Thank you!
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 18

Expert Comment

by:ingwa
ID: 38804490
FYI, to optimize this at the mysql level without having to iterate through with php, looping, converting etc:

 SELECT date_format(exercise_timestamp, '%Y-%c-%d') AS exercise_timestamp ,exercise_time FROM exercising_table WHERE uname = '$username'

// %Y      Year, numeric, four digits
// %c      Month, numeric (0..12)
// %d      Day of the month, numeric (00..31)
// as per first example in url posted. 

Open in new window


Hope this helps.
0
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 38804497
Try to change the foreach loop this way:

foreach ($result as $r){
	$firstDate = explode(' ', $r["exercise_timestamp"]);
	$secondDate = explode('-', $firstDate[0]);
	$thirdDate = array_reverse($secondDate);
	$finalDate = $thirdDate[0] . '-' . $thirdDate[1] . '-' . $thirdDate[2];
	// $result2[] = array($finalDate,  $r["exercise_time"]);
	$result2[] = "['" . $finalDate . "', '" . $r["exercise_time"] . "']";
}

Open in new window


As you can see, now we build the $result2 array not with subarrays but with a string formatted as you want. Maybe this way you can pass it correctly to the Javascript function. In fact with the previous format, Google received wrongly formatted data. A simple example is this:

	<?php
$result = array(
	array( "exercise_timestamp" => "2012-11-25 12:13:49", "exercise_time" => 10 ),
	array( "exercise_timestamp" => "2012-12-01 12:20:37", "exercise_time" => 15 )
);                                  

foreach ($result as $r){
	$firstDate = explode(' ', $r["exercise_timestamp"]);
	$secondDate = explode('-', $firstDate[0]);
	$thirdDate = array_reverse($secondDate);
	$finalDate = $thirdDate[0] . '-' . $thirdDate[1] . '-' . $thirdDate[2];
	// $result2[] = array($finalDate,  $r["exercise_time"]);
	$result2[] = "['" . $finalDate . "', '" . $r["exercise_time"] . "']";
}

// foreach ($result2 as $r){
	// echo "['" . $r[0] . "', '" . $r[1] . "']<br />";
// }	
	?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://ogp.me/ns/fb#">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
      <script type="text/javascript">  
	  function drawChart() {
        var PhpArray = <?php echo json_encode($result2); ?>;
		alert(PhpArray);
        var data = google.visualization.arrayToDataTable([
          ['Months', 'Exercising Time (Mins)'],
          $PhpArray
        ]);
		}
      </script>
</head>
  <body onload="drawChart()">
  <h1>Hello World!</h1>
  </body>
</html>
                                

Open in new window


Hope this helps
0
 

Author Comment

by:TLN_CANADA
ID: 38804523
Thank you so much for looking at this even when the question is finished! It is coming up with a blank page though unfortunately :(

Perhaps it would help if I included the full HTML working code for the chart:

<html>
  <head>
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">
      google.load("visualization", "1", {packages:["corechart"]});
      google.setOnLoadCallback(drawChart);
      function drawChart() {
        var data = google.visualization.arrayToDataTable([
          ['Months', 'Exercising Time (Mins)'],
          ['2012-11-25',  25],
          ['2012-11-26',  26],
          ['2012-11-28',  27],
		  ['2012-12-25',  25],
          ['2012-11-25',  26],
          ['2012-11-25',  27],
          ['2012-11-25',  11]
        ]);

        var options = {
          title: 'Meditation and Exercise Times'
        };

        var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
        chart.draw(data, options);
      }
    </script>
  </head>
  <body>
    <div id="chart_div" style="width: 900px; height: 500px;"></div>
  </body>
</html>

Open in new window

0
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 38805155
Hi, here you go! Please, let me do some side note:

1. You don't need to output dates as day-month-year as you requested in post ID: 38804383: Google script expects to receive the standard date format Y-m-d
2. You don't have to pass a string built as the hardcoded array, but a multi-dimensional array built as in code below
3. The first element of this multi-dimensional array must be an array with the table headers, the others must be the raw data
4. the php array must be encoded within the php section of code
5 I used the javascript variable PhpArray in order to print it for testing purposes, but you don't need it: you can simply write:
var data = google.visualization.arrayToDataTable(<?php echo $data; ?>);

Open in new window


Anyway, I post here the testing page I used in my localhost:

	<?php
$result = array(
	array( "exercise_timestamp" => "2012-11-25 12:13:49", "exercise_time" => 10 ),
	array( "exercise_timestamp" => "2012-12-01 12:20:37", "exercise_time" => 15 )
);                                  

foreach ($result as $r){
	$firstDate = explode(' ', $r["exercise_timestamp"]);
	$result2[] = array($firstDate[0],  $r["exercise_time"]);
}
array_unshift($result2, array("Months", "Exercising Time (Mins)"));
$data = json_encode($result2);
	?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://ogp.me/ns/fb#">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
	<script type="text/javascript" src="https://www.google.com/jsapi"></script>
      <script type="text/javascript">  
      google.load("visualization", "1", {packages:["corechart"]});
      google.setOnLoadCallback(drawChart);
      var PhpArray = <?php echo $data; ?>;
	  for(var i=0;i<PhpArray.length;i++){
			document.write("<b>PhpArray["+i+"] is </b>=>"+PhpArray[i]+"<br>");
	  }
      function drawChart() {
        var data = google.visualization.arrayToDataTable(PhpArray);
        var options = {
          title: 'Meditation and Exercise Times'
        };
        var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
        chart.draw(data, options);
      }		
      </script>
</head>
  <body onload="drawChart()">
  <div id="chart_div" style="width: 900px; height: 500px;"></div>
  </body>
</html>

Open in new window


Cheers
Marco
0
 

Author Comment

by:TLN_CANADA
ID: 38807435
Thank you so much Marco. For some reason though your new code is only printing two results in the array:

PhpArray[1] is =>2012-11-25,10
PhpArray[2] is =>2012-12-01,15

in the last code you sent it was printing all of the results (About 20).

Do you know why this is happening? I have tried replacing your variable above as you mentioned but this did not work also.

Here is what I have on the page now:

<?php


	/**
	* Directory Seperator
	*
	*/
	define('PHPFOX_DS', DIRECTORY_SEPARATOR);

	/**
	* phpFox Root Directory
	*
	*/
	define('PHPFOX_DIR', dirname(__FILE__) . PHPFOX_DS);

	define('PHPFOX_START_TIME', array_sum(explode(' ', microtime())));

	// Require phpFox Init
	require(PHPFOX_DIR . 'include' . PHPFOX_DS . 'init.inc.php');
	
	mysql_connect('localhost','foxdbmain','!') or die ( mysql_error() );
	mysql_select_db("foxdbmain") or die ( mysql_error() );
	
	date_default_timezone_set('America/Vancouver');
	
	//Chart array part 

$username = Phpfox::getUserBy('full_name'); 
//echo $username;
	
$username=mysql_real_escape_string($username);
$result = array(
	array( "exercise_timestamp" => "2012-11-25 12:13:49", "exercise_time" => 10 ),
	array( "exercise_timestamp" => "2012-12-01 12:20:37", "exercise_time" => 15 )
);                                  

foreach ($result as $r){
	$firstDate = explode(' ', $r["exercise_timestamp"]);
	$result2[] = array($firstDate[0],  $r["exercise_time"]);
}
array_unshift($result2, array("Months", "Exercising Time (Mins)"));
$data = json_encode($result2);

	
?>
<html>
  <head>
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">  
      google.load("visualization", "1", {packages:["corechart"]});
      google.setOnLoadCallback(drawChart);
      var PhpArray = <?php echo $data; ?>;
	  for(var i=0;i<PhpArray.length;i++){
			document.write("<b>PhpArray["+i+"] is </b>=>"+PhpArray[i]+"<br>");
	  }
      function drawChart() {
        var data = google.visualization.arrayToDataTable(PhpArray);
        var options = {
          title: 'Meditation and Exercise Times'
        };
        var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
        chart.draw(data, options);
      }		
      </script>
  </head>
  <body>
     <body onload="drawChart()">
	 <div id="chart_div" style="width: 900px; height: 500px;"></div> 
  </body>
</html>

Open in new window




Thanks,

D
0
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 38808613
Lol. The previous time you had replaced these two lines

$result = array(
      array( "exercise_timestamp" => "2012-11-25 12:13:49", "exercise_time" => 10 ),
      array( "exercise_timestamp" => "2012-12-01 12:20:37", "exercise_time" => 15 )
);

with your query

$result=array();
$query = mysql_query("SELECT exercise_timestamp,exercise_time FROM exercising_table WHERE uname = '$username' ");
while( $result[]=mysql_fetch_assoc($query)){}
print_r($result);

I used that lines for testing purposes since I don't have you database :-)
0
 

Author Comment

by:TLN_CANADA
ID: 38808635
Oh :) Sorry to bother you again but this but I'm getting the google error now:

Data column(s) for axis #0 cannot be of type string×

Here is the printing output on the screen now:

PhpArray[1] is =>2012-11-25,10
PhpArray[2] is =>2012-12-01,15
PhpArray[3] is =>2012-12-01,15
PhpArray[4] is =>2012-12-21,10
PhpArray[5] is =>2012-12-21,14
PhpArray[6] is =>2012-12-23,34
PhpArray[7] is =>2012-12-23,34
PhpArray[8] is =>2013-01-10,44
PhpArray[9] is =>2013-01-14,15
PhpArray[10] is =>2013-01-14,15
PhpArray[11] is =>2013-01-14,34
PhpArray[12] is =>2013-01-17,
PhpArray[13] is =>2013-01-17,
PhpArray[14] is =>2013-01-18,
PhpArray[15] is =>2013-01-18,1
PhpArray[16] is =>2013-01-18,12
PhpArray[17] is =>2013-01-19,25
PhpArray[18] is =>2013-01-19,34
PhpArray[19] is =>2013-01-19,34
PhpArray[20] is =>2013-01-19,34
PhpArray[21] is =>2013-01-19,34
PhpArray[22] is =>2013-01-19,34

Open in new window


I'm not sure why it's giving this error? As when I used the hardcoded example you gave it worked correctly.

Here's the code on the page now

$username=mysql_real_escape_string($username);
$result=array();
$query = mysql_query("SELECT exercise_timestamp,exercise_time FROM exercising_table WHERE uname = '$username' ");
while( $result[]=mysql_fetch_assoc($query)){}
print_r($result);                                  

foreach ($result as $r){
	$firstDate = explode(' ', $r["exercise_timestamp"]);
	$result2[] = array($firstDate[0],  $r["exercise_time"]);
}
array_unshift($result2, array("Months", "Exercising Time (Mins)"));
$data = json_encode($result2);

	
?>
<html>
  <head>
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">  
      google.load("visualization", "1", {packages:["corechart"]});
      google.setOnLoadCallback(drawChart);
      var PhpArray = <?php echo $data; ?>;
	  for(var i=0;i<PhpArray.length;i++){
			document.write("<b>PhpArray["+i+"] is </b>=>"+PhpArray[i]+"<br>");
	  }
      function drawChart() {
        var data = google.visualization.arrayToDataTable(PhpArray);
        var options = {
          title: 'Meditation and Exercise Times'
        };
        var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
        chart.draw(data, options);
      }		
      </script>
  </head>
  <body>
     <body onload="drawChart()">
	 <div id="chart_div" style="width: 900px; height: 500px;"></div> 
  </body>
</html>

Open in new window

0
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 38808715
I think you get that error because the records 12, 13, 14 in the PhpArray are missing the second value. Try to set the default value for that record in your database (exercise_time) to 0 and make it to be NOT NULL . I think this way the code will work correctly
0
 

Author Comment

by:TLN_CANADA
ID: 38808723
I changed this in the db and deleted the records that had no value for now but it is still giving the error. I see the 20th record in the array is blank, would this be it?

Here is what I have now when printing the variables

PhpArray[1] is =>2012-11-25,10
PhpArray[2] is =>2012-12-01,15
PhpArray[3] is =>2012-12-01,15
PhpArray[4] is =>2012-12-21,10
PhpArray[5] is =>2012-12-21,14
PhpArray[6] is =>2012-12-23,34
PhpArray[7] is =>2012-12-23,34
PhpArray[8] is =>2013-01-10,44
PhpArray[9] is =>2013-01-14,15
PhpArray[10] is =>2013-01-14,15
PhpArray[11] is =>2013-01-14,34
PhpArray[12] is =>2013-01-18,1
PhpArray[13] is =>2013-01-18,12
PhpArray[14] is =>2013-01-19,25
PhpArray[15] is =>2013-01-19,34
PhpArray[16] is =>2013-01-19,34
PhpArray[17] is =>2013-01-19,34
PhpArray[18] is =>2013-01-19,34
PhpArray[19] is =>2013-01-19,34
PhpArray[20] is =>,

Open in new window

0
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 38808741
Maybe: I don't know how Google code interprets a blank line in an array. Try to delete empty vlaues:

foreach ($result as $r){
        if (!empty($r) && $r != ''){
              $firstDate = explode(' ', $r["exercise_timestamp"]);
              $result2[] = array($firstDate[0],  $r["exercise_time"]);
        }
}
0
 

Author Comment

by:TLN_CANADA
ID: 38808750
No :( This doesn't solve it I'm afraid.

Just to double check, what you've created here looks like this when printed on the page?

   var data = google.visualization.arrayToDataTable([
          ['Months', 'Exercising Time (Mins)'],
          ['2012-11-25',  25],
          ['2012-11-26',  26],
          ['2012-11-28',  27],
          ['2012-12-05',  25],
          ['2012-12-11',  26],
          ['2012-12-13',  27],
          ['2012-12-25',  11]
        ]);

Open in new window

0
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 38808761
I can't know: I don't have a database with your values: I used a two records array for testing the code and it worked fine. If you want you can export your table (create and values) so I can use the table to run the code and test the real work. I received the same error during my test before to correctly build the array, so if you don't send me the table, I will create one with some value because now I want absolutely understand why it's not working! ;-)
0
 

Author Comment

by:TLN_CANADA
ID: 38808765
Thank you so much! Here is the table:
exercising-table--1-.cs
0
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 38808791
Sorry, but I don't understand the code you posted: I expected to have a mysql dump file... what is that code? Anyway, it doesn't matter: I'll create a small table with only the two columns we need in this issue (exercise_timestamp and exercise_time) and I'll fill it with the values you posted in your last array, then I'll let you know something...

Cheers
0
 

Author Comment

by:TLN_CANADA
ID: 38808800
Sorry, if you want to see how the structure and table looks, here it is attached. db
0
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 38808872
Solved. The problem was that when retrieving data from the database, php got the exercise_time as string instead of as integer. Try to use var_dump instead of print_r: var_dump gives you more information and it's an incredibly useful tool for debugging purposes! Var_dump tell you not only the values of an array but it checks and tells even their type.
 
That said,  in order to make your code work you have to force the array element relative to exercise_time to be an integer:

foreach ($result as $r){
  if (!empty($r) && $r != ''){
    $firstDate = explode(' ', $r["exercise_timestamp"]);
    $result2[] = array($firstDate[0],  (integer)$r["exercise_time"]);
  }
}

Add to your foreach loop the (integer) forcing and it will work like a charm!

Cheers
0
 

Author Comment

by:TLN_CANADA
ID: 38813092
Thank you so much, it’s working perfectly now! I really appreciate your help with this.

There are a couple of other additions I would like to make to this chart but I feel it would be best if I opened a new question about it. It is just arranging and grouping the data output in a slightly different way. Could I open a new question and send you a link to it?

Thank you so much for your help with this again!
0
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 38813103
Yes, you can, as Obama would say :-) But I'm not experienced about using of google chart (this was the first time I had to do with) so I don't know if I'll be helpful...

Cheers
0
 

Author Comment

by:TLN_CANADA
ID: 38813115
Thank you so much! It will just be PHP stuff with it so no worries there :)

Here is the question

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28006725.html
0
 

Author Comment

by:TLN_CANADA
ID: 38813228
I hope I made sense in the new question Marco ? :)
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now