everisk Posted May 28, 2007 Share Posted May 28, 2007 Hi I try to populate an array variable by pulling data from data using while. However, whatever I insert only stays in the while loop. I need the values to be available outside of the while loop. Below is the code... $chart['chart_data'][0][0]=""; $chart['chart_data'][1][0]="Opens"; $sql = "SELECT count(*) as open, sdate FROM table a where nl=111 and lid=863 group by sdate order by sdate ASC"; $table = mysql_query($sql) or die("cannot select"); while($row=mysql_fetch_array($table)) { static $i=1; $chart['chart_data'][0]['$i'] = $row['sdate']; $chart['chart_data'][1]['$i'] = $row['open']; $i++; } SendChartData ( $chart ); Thanks a lot! Quote Link to comment Share on other sites More sharing options...
MadTechie Posted May 28, 2007 Share Posted May 28, 2007 move the $i = 1; ie $i=1; while($row=mysql_fetch_array($table)) { $chart['chart_data'][0]['$i'] = $row['sdate']; $chart['chart_data'][1]['$i'] = $row['open']; $i++; } Quote Link to comment Share on other sites More sharing options...
everisk Posted May 28, 2007 Author Share Posted May 28, 2007 Thanks alot MadTechie but that doesn't seem to work. When execute SendChartData($chart) .. it seems to get only value I initialized in the first 2 lines and not what what I populate within the while block. $chart['chart_data'][0][0]=""; $chart['chart_data'][1][0]="Opens"; Let me paste the full code in the correct manner again $chart['chart_data'][0][0]=""; $chart['chart_data'][1][0]="Opens"; $sql = "SELECT count(*) as open, sdate FROM table a where nl=111 and lid=863 group by sdate order by sdate ASC"; $table = mysql_query($sql) or die("cannot select"); while($row=mysql_fetch_array($table)) { static $i=1; $chart['chart_data'][0]['$i'] = $row['sdate']; $chart['chart_data'][1]['$i'] = $row['open']; $i++; } SendChartData ( $chart ); Quote Link to comment Share on other sites More sharing options...
yzerman Posted May 28, 2007 Share Posted May 28, 2007 check your sql statement to see if its working. should be: $sql = "SELECT count(*) as open, sdate FROM table a where nl='111' and lid='863' group by sdate order by sdate ASC"; Quote Link to comment Share on other sites More sharing options...
MadTechie Posted May 28, 2007 Share Posted May 28, 2007 Try this quick test (untested my end) <?php $chart['chart_data'][0][0]=""; $chart['chart_data'][1][0]="open"; //<--changed $tmp = array("open" => array(), "sdate" => array()); $sql = "SELECT count(*) as open, sdate FROM table a where nl=111 and lid=863 group by sdate order by sdate ASC"; $table = mysql_query($sql) or die("cannot select"); $i=1; while($row=mysql_fetch_array($table)) { $tmp["sdate"][$i] = $row['sdate']; $tmp["open"][$i] = $row['open']; $i++; } print_r($tmp); die; $chart['chart_data'][0] = $tmp['sdata']; $chart['chart_data'][1] = $tmp['open']; SendChartData ( $chart ); ?> Quote Link to comment Share on other sites More sharing options...
everisk Posted May 28, 2007 Author Share Posted May 28, 2007 Thanks a lot MadTechie, your code seems to working very well. I'm not very good with array so I need help again. Instead of printing $tmp I tried printing $chart but then I got unwanted strings in red. How do I get rid of it? Array ( [chart_data] => Array ( [0] => Array ( [1] => 2007-05-10 [2] => 2007-05-11 [3] => 2007-05-12 [4] => 2007-05-13 [5] => 2007-05-14 [6] => 2007-05-15 [7] => 2007-05-16 [8] => 2007-05-17 [9] => 2007-05-18 [10] => 2007-05-19 [11] => 2007-05-20 [12] => 2007-05-21 [13] => 2007-05-22 [14] => 2007-05-23 [15] => 2007-05-24 [16] => 2007-05-25 [17] => 2007-05-26 [18] => 2007-05-28 ) [1] => Array ( [1] => 250 [2] => 112 [3] => 34 [4] => 21 [5] => 32 [6] => 27 [7] => 14 [8] => 8 [9] => 4 [10] => 2 [11] => 2 [12] => 4 [13] => 4 [14] => 3 [15] => 2 [16] => 1 [17] => 1 [18] => 1 ) ) ) 2007-05-10 2007-05-11 2007-05-12 2007-05-13 2007-05-14 2007-05-15 2007-05-16 2007-05-17 2007-05-18 2007-05-19 2007-05-20 2007-05-21 2007-05-22 2007-05-23 2007-05-24 2007-05-25 2007-05-26 2007-05-28 250 112 34 21 32 27 14 8 4 2 2 4 4 3 2 1 1 1 $chart['chart_data'][0][0]=""; $chart['chart_data'][1][0]="open"; //<--changed $tmp = array("open" => array(), "sdate" => array()); $sql = "SELECT count(*) as open, sdate FROM table a where nl=111 and lid=863 group by sdate order by sdate ASC"; $table = mysql_query($sql) or die("cannot select"); $i=1; while($row=mysql_fetch_array($table)) { $tmp["sdate"][$i] = $row['sdate']; $tmp["open"][$i] = $row['open']; $i++; } $chart['chart_data'][0] = $tmp['sdate']; $chart['chart_data'][1] = $tmp['open']; print_r($chart); SendChartData ( $chart ); Quote Link to comment Share on other sites More sharing options...
MadTechie Posted May 28, 2007 Share Posted May 28, 2007 on a quick review are you sure thats from chart_data ? try $chart['chart_data'][0] = $tmp['sdate']; $chart['chart_data'][1] = $tmp['open']; echo "<pre>"; print_r($chart); die; SendChartData ( $chart ); Quote Link to comment Share on other sites More sharing options...
everisk Posted May 28, 2007 Author Share Posted May 28, 2007 MadTechie, you are a genius! Your modified code your correctly, it looks like the extra data were from SendChartData(). Thanks a million! Quote Link to comment Share on other sites More sharing options...
MadTechie Posted May 28, 2007 Share Posted May 28, 2007 cool your welcome Quote Link to comment Share on other sites More sharing options...
everisk Posted May 28, 2007 Author Share Posted May 28, 2007 oh .. sorry .. one more thing .. it seems like when I first initialize the array the value for both [0][0] and [1][0] are not stored. Array ( [chart_data] => Array ( [0] => Array ( [0] => null /////missing [1] => 2007-05-10 [2] => 2007-05-11 [3] => 2007-05-12 [4] => 2007-05-13 [5] => 2007-05-14 [6] => 2007-05-15 [7] => 2007-05-16 [8] => 2007-05-17 [9] => 2007-05-18 [10] => 2007-05-19 [11] => 2007-05-20 [12] => 2007-05-21 [13] => 2007-05-22 [14] => 2007-05-23 [15] => 2007-05-24 [16] => 2007-05-25 [17] => 2007-05-26 [18] => 2007-05-28 ) [1] => Array ( [0] => open /////missing [1] => 250 [2] => 112 [3] => 34 [4] => 21 [5] => 32 [6] => 27 [7] => 14 [8] => 8 [9] => 4 [10] => 2 [11] => 2 [12] => 4 [13] => 4 [14] => 3 [15] => 2 [16] => 1 [17] => 1 [18] => 1 ) ) ) Quote Link to comment Share on other sites More sharing options...
everisk Posted May 28, 2007 Author Share Posted May 28, 2007 oh .. Got it! eieii .. Quote Link to comment Share on other sites More sharing options...
redarrow Posted May 28, 2007 Share Posted May 28, 2007 set the array to [0][0] ok Quote Link to comment Share on other sites More sharing options...
MadTechie Posted May 28, 2007 Share Posted May 28, 2007 Coughts array starts at $i=1; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.