Log in

View Full Version : Need help with foreach please... Urgent...



vipmerv
03-10-2009, 05:10 AM
Hello guys...

I have something that make me stucked in 2 days because I have deal with some integration with 3rd party API.. The code are as below..

$title1 = array(
$title2=> array(
'1' => 9,
'2' => 25,
'3' => 2,
'4' => 22,
'5' => 16,
'6' => 18,
),
);

Assume that left value is $count and right value is is $value.. I use foreach to call the multiple array of $title2 like the code below.


foreach ($measurement_data as $count => $value)
{
$count = $count+1;
if ($count < 20 )
{
echo "$count => $value <br>";
}
}

I got the result for this on $count and $value.... But how do I return the value for $title1 and $title2 just as below...??

$title1 = array( $title2=> array( $count => $value ), );

Please help.....

vipmerv
03-10-2009, 05:43 AM
What I mean here is I would like to get a result like the code below, and not only result for $title1 & $title2

$title1 = array( $title2=> array( $count => $value ), );

JasonDFR
03-10-2009, 07:31 AM
I am having trouble understanding what you would like your result to look like.

Post an example of the OUTPUT you would like to see.

vipmerv
03-10-2009, 08:27 AM
Ok... I want the output to be like this


My Test <----- where $title2 = 'My Test'

1 => 9 <----- where 1 = $count // 9 = $value (from database)
2 => 25
3 => 2
4 => 22
5 => 16
6 => 18

$title1 is a call function to another array that generate the output..

vipmerv
03-10-2009, 08:32 AM
I want the $title2 to be appear as well when the result of $count and $value been generated.

All this will be related to the google API function that I use.. XY-axis and Legend value.
the $title2 will represent value for legend

vipmerv
03-10-2009, 08:51 AM
Ok.... Full code are as below.

This is to generate the graph on the site.

goofunctions.php (The function is all correct with NO error...) So its return all value when I put dummy result at the display page.


class MyGoogleChart
{
const URL = 'http://chart.apis.google.com/chart?';
protected $ChartTypes = array(
'pie' => 'p',
'pie3' => 'p3',
'line' => 'lc',
'sparkline' => 'ls',
'bar-horizontal' => 'bhg',
'bar-vertical' => 'bvg',
);
protected $get_mychart;
protected $title;
protected $data = array();
protected $size = array();
protected $color = array();
protected $fill = array();
protected $labelsXY = true;
protected $legend;
protected $useLegend = true;
protected $background;
protected $max;
protected $min;

protected $query = array();

public function __toString()
{
return $this->display();
}

protected function display()
{

$this->query = array(
'cht' => $this->ChartTypes[strtolower($this->get_mychart)], // get_mychart
'chtt' => $this->title, // Title
'chd' => 't:'.$this->data['values'], // Data
'chl' => $this->data['names'], // Data labels
'chdl' => ( ($this->useLegend) && (is_array($this->legend)) ) ? implode('|',$this->legend) : null, // Data legend
'chds' => $this->min.','.$this->max, // Data scale
'chs' => $this->size[0].'x'.$this->size[1], // Size
'chco' => preg_replace( '/[#]+/', '', implode(',',$this->color)), // Color ( Remove # from string )
'chm' => preg_replace( '/[#]+/', '', implode('|',$this->fill)), // Fill ( Remove # from string )
'chxt' => ( $this->labelsXY == true) ? 'y' : null, // X & Y axis labels
'chxr' => '0,1,'.$this->max, // Data range
'chf' => preg_replace( '/[#]+/', '', $this->background), // Background color ( Remove # from string )
);
// Return chart
return $this->img(
MyGoogleChart::URL.http_build_query($this->query),
$this->title
);
}
/* Set attributes */
public function setChartAttrs( $attrs )
{

$this->debug[] = $attrs;
foreach( $attrs as $key => $value )
{
$this->{"set$key"}($value);
}
}

protected function setGet_MyChart( $get_mychart )
{
$this->get_mychart = $get_mychart;
}

protected function setTitle( $title )
{
$this->title = $title;
}



protected function setData( $data )
{
// Clear any previous data
unset( $this->data );

// Check if multiple data
if( is_array(reset($data)) )
{
/* Multiple sets of data */
foreach( $data as $key => $value )
{
// Set highest & lowest
foreach( $value as $valueS ) {
if( empty($this->min) || $valueS < $this->min )
$this->min = $valueS;
if( empty($this->max) || $valueS > $this->max )
$this->max = $valueS;
}
// Add data values
$this->data['values'][] = implode( ',', $value );

// Add data names
$this->data['names'] = implode( '|', array_keys( $value ) );
}

/* Implode data correctly */
$this->data['values'] = implode('|', $this->data['values']);

/* Create legend */
$this->legend = array_keys( $data );
}
else
{
// Set highest & lowest value for X & Y-axis
foreach( $data as $value ) {
if( empty($this->min) || $value < $this->min )
$this->min = $value;
if( empty($this->max) || $value > $this->max )
$this->max = $value;
}

// Add data values
$this->data['values'] = implode( ',', $data );
// Add data names
$this->data['names'] = implode( '|', array_keys( $data ) );
}

}


protected function setLegend( $legend )
{
$this->useLegend = $legend;
}


protected function setSize( $width, $height = null )
{

if(is_array( $width ) )
{
$this->size = $width;
}
else
{

$this->size[] = $width;
$this->size[] = $height;
}
}

protected function setColor( $color )
{
$this->color = $color;
}

protected function setLabelsXY( $labels )
{
$this->labelsXY = $labels;
}

protected function setFill( $fill )
{
if( count( $fill ) < 4 )
{
// Add remaining params
$count = count( $fill );
for( $i = 0; $i < $count; ++$i )
$fill[$i] = 'b,'.$fill[$i].','.$i.','.($i+1).',0';
}
$this->fill = $fill;
}

protected function setBackground( $background )
{
$this->background = 'bg,s,000000';
}

protected function img( $url, $alt = null )
{
return sprintf('<img src="%s" alt="%s" style="width:%spx;height:%spx;" />', $url, $alt, $this->size[0], $this->size[1]);
}

}

display.php (This page is to display the result of Google Graph I generate thru OO PHP... )


$getchart = new MyGoogleChart();
$color = array('#FF6600');
$gettitle = 'Total Time';

//I want to get the dynamic value from database for the result and attempt value. I just stucked here because the value not return to my graph.

/* foreach ($measurement_data as $count => $totalTime)
{
$count = $count+1;
if ($count < 20 )
{
echo "$count => $totalTime <br>";
}
} */

// This is dummy value... You can see the value.
$dataTimeline = array(
$gettitle => array(
'1' => 9,
'2' => 25,
'3' => 2,
'4' => 22,
'5' => 16,
'6' => 18,
'7' => 13,
'Aug' => 10,
'Sep' => 18,
'Okt' => 5,
'Nov' => 1,
'Dec' => 3,
),
);

$getchart->setChartAttrs( array(
'get_mychart' => 'line',
'title' => My testing result,
'data' => $dataTimeline,
'size' => array( 640, 320 ),
'color' => $color,
'labelsXY' => true,
'background' => $background,
'fill' => array( '#222222', '#000000' ),
));

echo $getchart;

JasonDFR
03-10-2009, 09:09 AM
I just want an example of what you would see on the screen in a browser once everything is working like you want.

Based on this data:


Title1 = array(
Title2 => array(
'1' => 9,
'2' => 25,
'3' => 2,
'4' => 22,
'5' => 16,
'6' => 18
)
);

vipmerv
03-10-2009, 09:38 AM
See the attachment and refer to the dummy code that I provide in my last post. Hope this will help.

JasonDFR
03-10-2009, 09:42 AM
I'm sorry vipmerv, I'm just struggling to understand what you need. Maybe someone else will take a look.

Good luck.

vipmerv
03-10-2009, 10:09 AM
Its ok Jason.... Thanks for your quick response...