Help wrapping my head around this loop within a loop

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
zachalbert
Forum Newbie
Posts: 13
Joined: Sun Jan 04, 2009 11:32 pm

Help wrapping my head around this loop within a loop

Post by zachalbert »

Ultimately, I need the following line in my code where the array values and the Series number are dynamically generated.

Code: Select all

 
$DataSet->AddPoint(array(1,2,3,4,5,6,7),"Serie1");
 
The array values represent 31 fields from a single row in a database. The series1 represents the row number, 1-65 (though a new row is added daily--hence the need for it to be dynamic).

The following code perfectly echoes everything. The only way I could make it work though was by breaking it up into several chunks. The issue is that I don't need it to echo, but to show up in the code in the form of the line above.

Code: Select all

 
<?php
//connect to DB stuff
 
$k = 1;
    
    while($row = mysql_fetch_array( $result )) {
    
        $string1 = "$"."DataSet->AddPoint(array(";
        echo $string1;
    
        for ( $i = 0; $i < 31; $i++ ) {
            $data = $row[$i].",";
            echo $data;
        }
    
        $string2 = "),\"Serie".$k;
        echo $string2;
    
        $string3 = "\");";
        echo $string3."<br>";
        $k = $k + 1;
    }
}
?>
 
...which echoes:

$DataSet->AddPoint(array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,),"Serie1");
$DataSet->AddPoint(array(-1,-1,-1,-1,-1,-1,-1,-1,1,1,-1,0,-1,1,1,1,1,1,-1,-1,-1,1,1,-1,-1,0,1,0,1,0,0,),"Serie2");
$DataSet->AddPoint(array(0,1,1,1,0,-1,-1,1,1,1,0,0,1,0,1,1,1,1,0,1,1,0,1,1,1,1,1,0,1,0,0,),"Serie3");
$DataSet->AddPoint(array(-1,0,0,-1,0,-1,1,0,0,1,0,0,-1,0,0,0,0,0,1,0,1,1,0,1,0,0,0,0,1,0,0,),"Serie4");

...and so on. Thoughts?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Help wrapping my head around this loop within a loop

Post by requinix »

Code: Select all

for ( $i = 0; $i < 31; $i++ ) {
    $data = $row[$i].",";
    echo $data;
}
This part generates an "array". But isn't $row an array already, with the data you want?
   

Code: Select all

$string2 = "),\"Serie".$k;
So "Serie".$k is the series number.

Think about it for a minute. The solution is a bit more obvious than you might expect.
zachalbert
Forum Newbie
Posts: 13
Joined: Sun Jan 04, 2009 11:32 pm

Re: Help wrapping my head around this loop within a loop

Post by zachalbert »

Forgive me if I don't quite follow--I'm a designer and I'm pretty new to php & programming in general.

So I think I see what you're saying... I should be able to set up the variable like this:

Code: Select all

 
$var = "$DataSet->AddPoint(array(".$row[$i]."),\"Serie".$k."\");";
 
But the reason I need that line is because I'm trying to use a charting library that requires that code further down, outside the loops. When I set it up as above and echo it outside the loops, I get the following

Code: Select all

 
$DataSet->AddPoint(array(),"Serie65");
 
The Series number only grabs the last number and nothing even comes up from the array.

It echoes everything as it should, but the only way I could figure that out is if it echoes it in 3 separate parts. I need it all in one line, and $var = $string1.$string2.$string3 doesn't work either, because at least one of those strings is outside the loop and turns up either blank or as the last number.

Sorry if I'm not getting it. Trust me though... I spent a lots of hours on this. If the answer is obvious, I'm not seeing it.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Help wrapping my head around this loop within a loop

Post by requinix »

You're trying to execute that $var code, right? I gathered that much.
But you say pchart "requires that code further down". How exactly does it need it?
zachalbert
Forum Newbie
Posts: 13
Joined: Sun Jan 04, 2009 11:32 pm

Re: Help wrapping my head around this loop within a loop

Post by zachalbert »

Here's what it looks like (lines 12-15). I just need the actual values and the Series numbers pulled from the database. I'm not sure how to get all the stuff from the loops into those lines.

Code: Select all

 
<?php
// Standard inclusions   
include("pChart/pData.class");
include("pChart/pChart.class");
 
// Dataset definition 
$DataSet = new pData;
 
// These following lines I need pulled from the database.
$DataSet->AddPoint(array(1,2,3,4,5,6,7),"Serie1");
$DataSet->AddPoint(array(1,2,3,4,5,6,7,8,9,10),"Serie2");
$DataSet->AddPoint(array(1,2,3,2,3,4,3,2,1,0),"Serie3");
 
$DataSet->AddAllSeries();
$DataSet->SetAbsciseLabelSerie("Serie1"); //sets which 'serie' should define the X axis
 
// Initialise the graph
$Test = new pChart(700,230); //width & height of image
$Test->setFontProperties("Fonts/tahoma.ttf",10);
$Test->setGraphArea(40,30,680,200); //graph width & height, in coordinates from top left of image
$Test->drawGraphArea(252,252,252); //rgb value of odd rows (background)
$Test->drawScale($DataSet->GetData(),$DataSet->GetDataDescription(),SCALE_NORMAL,150,150,150,TRUE,0,2); // the 150's are the color of the numbers on the axis
$Test->drawGrid(4,TRUE,230,230,230,255); // (dotted line size, color every other)
 
// Draw the line graph
$Test->drawLineGraph($DataSet->GetData(),$DataSet->GetDataDescription());
$Test->drawPlotGraph($DataSet->GetData(),$DataSet->GetDataDescription(),3,2,255,255,255); //the points
 
// Finish the graph
$Test->setFontProperties("Fonts/tahoma.ttf",8);
$Test->drawLegend(45,35,$DataSet->GetDataDescription(),255,255,255);
$Test->setFontProperties("Fonts/tahoma.ttf",10);
$Test->drawTitle(60,22,"My pretty graph",50,50,50,585);
$Test->Render("pr.png");*/
 
?>
 
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Help wrapping my head around this loop within a loop

Post by requinix »

Code: Select all

for ( $i = 0; $i < 31; $i++ ) {
    $data = $row[$i].",";
    echo $data;
}
$row is the first argument to pass to addPoint. It's already an array with the numbers needed.

Code: Select all

"Serie".$k;
"Serie".$k is the second argument.

Unless I still misunderstand what you want to do then all you need is

Code: Select all

$k = 1;
 
while($row = mysql_fetch_array( $result )) {
    $DataSet->AddPoint($row, "Serie" . $k);
    $k++;
}
Post Reply