Jump to content

[SOLVED] passing variables/values out of while block


everisk

Recommended Posts

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!

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 );

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 );

?>

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 );

:o 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
                )

        )

)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.