Jump to content

JPGraph: MySQL looping with a GET variable.


Smell

Recommended Posts

Hy there!  :)

 

Fellows i need a big help here!  :(

 

I have to create a jpgraph-based line graph where every single plot is a MySQL result. Visitors will get a form containing all cities registered in the db so they can select as many as they want from the checkbox. All chosen ones are gonna be sent to graph.php through a $_GET variable called "City". Ok so far...

 

Graph.php should show the visitor a line graphic with all sales from those cities during a previously specified period. Theoretically, it must be done by looping "$_GET", right?

 

So let's do that thing:

 


if ($_GET["city"]){

foreach($_GET["city"] as $city){
$query="SELECT sales.".$_GET["unit"]." AS unit 
FROM register 
INNER JOIN sales ON register.IntCod = sales.IntCod 
WHERE City = '".$city."' && Time BETWEEN '".$_GET["since"]."' AND '".$_GET["to"]."'";
$result=mysql_query($query) or die(mysql_error());
$datax=array();
while ( $row = mysql_fetch_array($result) ){
array_push($datax,$row["unit"]);


$p1 = new LinePlot($datax);
}

$p1->SetColor('navy');
$p1->SetLegend($city);
$graph->Add($p1);
}
}

 

Two cities (ALTINOPOLIS and MONTE STO DE MINAS) have their sales registered in the database so we can test it.

 

Therefore, if they (and nothing but them) are chosen from the form, the graph is shown fine: (ALTINOPOLIS does have just four sales registered. This graph is OK).

 

er1.JPG

URL Called: graph.php?city%5B%5D=ALTINOPOLIS&city%5B%5D=MONTE+STO+DE+MINAS&unit=a1&since=01-02-2007&to=16-02-2007

 

But if some other city is filled out, something goes wrong... Look at this example where cities are ALTINOPOLIS, JARDINOPOLIS and MONTE STO DE MINAS respectively:

 

er2.JPG

URL Called: graph.php?city%5B%5D=ALTINOPOLIS&city%5B%5D=JARDINOPOLIS&city%5B%5D=MONTE+STO+DE+MINAS&unit=a1&since=01-02-2007&to=16-02-2007

Can you see that? ALTINOPOLIS' legend disappears although its line is still being shown... Weird, huh?  ???

 

PS: Yes. As you can see, i didn't get to change line colors by using array of colors... if you may help me out on that as well, i'll be pretty thankful!  ;D

 

Whole code is right up here, if you need so:

 


<?php
include ("../jpgraph/src/jpgraph.php");
include ("../jpgraph/src/jpgraph_line.php");
require("config.php");
mysql_connect($host,$user,$pass);
@mysql_select_db($bd) or die( "We have a problem with our database now, we'll be back shortly. Sorry for the upset.'");

// Setup the graph
$graph = new Graph(300,200);
$graph->SetMarginColor('white');
$graph->SetScale("textlin");
$graph->SetFrame(false);
$graph->SetMargin(30,50,30,30);

$graph->title->Set('Sales');


$graph->yaxis->HideZeroLabel();
$graph->ygrid->SetFill(true,'#[email protected]','#[email protected]');
$graph->xgrid->Show();

$graph->xaxis->SetTickLabels($gDateLocale->GetShortMonth());

if ($_GET["city"]){

foreach($_GET["city"] as $city){
$query="SELECT sales.".$_GET["unit"]." AS unit FROM register INNER JOIN sales ON register.IntCod = sales.IntCod WHERE City = '".$city."' && Time BETWEEN '".$_GET["since"]."' AND '".$_GET["to"]."'";
$result=mysql_query($query) or die(mysql_error());
$datax=array();
while ( $row = mysql_fetch_array($result) ){
array_push($datax,$row["unit"]);


$p1 = new LinePlot($datax);
}

$p1->SetColor('navy');
$p1->SetLegend($city);
$graph->Add($p1);
}
}

$graph->legend->SetShadow('[email protected]',5);
$graph->legend->SetPos(0.1,0.1,'right','top');
// Output line

$graph->Stroke();

?>

 

Txs a lot for any help, brows!  8)

 

Take care!  ;)

I believe that what is happening is your get variable is being overwritten when you send the second city.

 

It is therefore not creating an array of city, but simply a string.

 

Your best method would be to use POST and make the checkboxes an array:

 

<input type="checkbox" name="city[]" value="My City Name">

I believe that what is happening is your get variable is being overwritten when you send the second city.

 

It is therefore not creating an array of city, but simply a string.

 

Your best method would be to use POST and make the checkboxes an array:

 

<input type="checkbox" name="city[]" value="My City Name">

 

Txs a lot for your php-related advice, Tom! :D

 

I'm so sorry that didn't work though... i had already been using arrays in the form:

 

<input type=\"checkbox\" name=\"city[]\" value=\"$row[city]\">".$row["city"]."<br />"

 

I replaced GET with POST as you suggested, but that didn't change anything...

 

It's so strange, man...  :(  What else should i try?

 

Btw txs so much for your attention, brov!  ;)

Try outputting the $_POST array and your $datax array (after you have finished creating it) to ensure that the data is actually there:

 

print_r($_POST);

print_r($datax);

 

Ok, let's try:  ;)

 


if ($_POST["city"]){

foreach($_POST["city"] as $city){
$query="SELECT sales.".$_POST["unit"]." AS unit FROM register INNER JOIN sales ON register.IntCod = sales.IntCod WHERE City = '".$city."' && Time BETWEEN '".$_POST["since"]."' AND '".$_POST["to"]."'";
$result=mysql_query($query) or die(mysql_error());
$datax=array();
while ( $row = mysql_fetch_array($result) ){
array_push($datax,$row["unit"]);


// Create the first line
print_r($datax);
}

print_r($_POST[city]);

}
}

 

This outputs:

 

--------------

Array ( [0] => 310 ) Array ( [0] => 310 [1] => 870 ) Array ( [0] => 310 [1] => 870 [2] => 324 ) Array ( [0] => 310 [1] => 870 [2] => 324 [3] => 321 ) Array ( [0] => 310 [1] => 870 [2] => 324 [3] => 321 [4] => 545 ) Array ( [0] => 310 [1] => 870 [2] => 324 [3] => 321 [4] => 545 [5] => 241 ) Array ( [0] => 310 [1] => 870 [2] => 324 [3] => 321 [4] => 545 [5] => 241 [6] => 129 ) Array ( [0] => 310 [1] => 870 [2] => 324 [3] => 321 [4] => 545 [5] => 241 [6] => 129 [7] => 415 ) Array ( [0] => ALTINOPOLIS [1] => JARDINOPOLIS [2] => MONTE STO DE MINAS ) Array ( [0] => ALTINOPOLIS [1] => JARDINOPOLIS [2] => MONTE STO DE MINAS ) Array ( [0] => 583 ) Array ( [0] => 583 [1] => 315 ) Array ( [0] => 583 [1] => 315 [2] => 166 ) Array ( [0] => 583 [1] => 315 [2] => 166 [3] => 565 ) Array ( [0] => ALTINOPOLIS [1] => JARDINOPOLIS [2] => MONTE STO DE MINAS )

--------------

 

Well... that prints a $datax array on every loop (because i put "print_r($datax);" into the WHILE statement right after "array_push")... so that seems to be ok, right?

 

The following code doesn't display $datax, but it shows up every field that is being put on that array through "array_push".

 

As you can see, $_POST["city"] is set and outputted as $city. This code is just to make sure that all variables are working fine:

 

 

foreach($_POST["city"] as $city){
$query="SELECT sales.".$_POST["unit"]." AS unit FROM register INNER JOIN sales ON register.IntCod = sales.IntCod WHERE City = '".$city."' && Time BETWEEN '".$_POST["since"]."' AND '".$_POST["to"]."'";
$result=mysql_query($query) or die(mysql_error());
echo "$city: ";

while ( $row = mysql_fetch_array($result) ){
array_push($datax,$row["unit"]);
    echo "<TR>\n"
        ."<TD>".$row["unit"]."</TD></TR>\n";

    }
        echo "<br>";
}
};

 

Outputting:

 

ALTINOPOLIS: 310 870 324 321 545 241 129 415
JARDINOPOLIS:
MONTE STO DE MINAS: 583 315 166 565 

 

So, it sounds to be all right with our variables i guess...  :(

 

Something else?

 

Txs!  :)

Quick idea: just for the hell of it because I'm to lazy right now to look to in depth into your code. I might look more in depth tommorrow, but image functions always give me headaches.

 

Make your first array value an empty one. Just to try it, so instead of your array listing out:

---

ALTINOPOLIS: 310 870 324 321 545 241 129 415

JARDINOPOLIS:

MONTE STO DE MINAS: 583 315 166 565

---

It would list

---

 

ALTINOPOLIS: 310 870 324 321 545 241 129 415

JARDINOPOLIS:

MONTE STO DE MINAS: 583 315 166 565

---

 

Otherwise I'll look at it tommorrow if no one else figures it out.

 

ALTINOPOLIS: 310 870 324 321 545 241 129 415

JARDINOPOLIS:

MONTE STO DE MINAS: 583 315 166 565

---

It would list

---

 

ALTINOPOLIS: 310 870 324 321 545 241 129 415

JARDINOPOLIS:

MONTE STO DE MINAS: 583 315 166 565

---

 

I think you posted the same result twice...  :D you must be really tired! hehehe  ;D

 

You meant, i should list JARDINOPOLIS before ALTINOPOLIS, didn't you?

 

If so, this is the result:

 

JARDINOPOLIS:
ALTINOPOLIS: 310 870 324 321 545 241 129 415
MONTE STO DE MINAS: 583 315 166 565 

 

When i tried generating a graph with JARDINOPOLIS before ALTINOPOLIS, it produced a fatal error: "Fatal error: Call to a member function SetColor() on a non-object in C:\www\full\graph.php on line 36". I got to solve that by adding an "if (!empty($datax))", but that made the script ignore JARDINOPOLIS:

 

$p1 = new LinePlot($datax);
}

if (!empty($datax)){
$p1->SetColor('navy');
$p1->SetLegend($city);
$graph->Add($p1);
}


}
}

 

Outputs:

 

er3.JPG

URL Called: graph.php?city%5B%5D=JARDINOPOLIS&city%5B%5D=ALTINOPOLIS&city%5B%5D=MONTE+STO+DE+MINAS&unit=a1&since=01-02-2007&to=16-02-2007

 

That's enough for today. Let's go bed...  :)

 

Hope to see you tomorrow, man! ;D

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.