Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 241
  • Last Modified:

creating/processing 2d Array of values to be added to a lineplot

This is some data I have stored in my database
 
 Period         Reason         Searched
1 Apr-Jun 2003       Drugs       2433
1 Apr-Jun 2003       Firearms       144
1 Apr-Jun 2003       Going equipped       1147
1 Apr-Jun 2003       Offensive weapons       729
1 Apr-Jun 2003       Other       1630
1 Apr-Jun 2003       Stolen Property       5576
2 Jul-Sep 2003       Drugs       2149
2 Jul-Sep 2003       Firearms       152
2 Jul-Sep 2003       Going equipped       913
2 Jul-Sep 2003       Offensive weapons       584
2 Jul-Sep 2003       Other       1415
2 Jul-Sep 2003       Stolen Property       4612
3 Oct-Dec 2003       Drugs       2737
3 Oct-Dec 2003       Firearms       108
3 Oct-Dec 2003       Going equipped       1214
3 Oct-Dec 2003       Offensive weapons       583
3 Oct-Dec 2003       Other       2169
3 Oct-Dec 2003       Stolen Property       5414
4 Jan-Mar 2004       Drugs       2501
4 Jan-Mar 2004       Firearms       162
4 Jan-Mar 2004       Going equipped       1219
4 Jan-Mar 2004       Offensive weapons       807
4 Jan-Mar 2004       Other       2905
4 Jan-Mar 2004       Stolen Property       6434


I'm trying to write some code in PHP that stores a 2d array in the session holing the results for each of the 'Reasons' above.


Then when I pass the array to my line_plot function I need it to extract each of the 'Reasons' arrays and add a plot to the graph for each 'Reason'.


Here is my pseudo/code so far





file evidence.php


$reasons = 'SELECT DISTINCT Reason FROM 28_evidence';
$results = mysql_query($reasons) or
      die(mysql_error());
      while ($rows=mysql_fetch_assoc($results)) {
      
      //fill the legend array
      $legend_array[] = $rows['Reason'];
      
      //obtain results for each reason
      $sql = "SELECT Searched FROM 28_evidence WHERE Reason=\"".$rows['Reason']."\"";

                //Insert data into array
                  $results1 = mysql_query($sql) or
                        die(mysql_error());
                        while ($rows2=mysql_fetch_assoc($results1)) {
                            //store the results for each period
                              $result_array[] = $rows2['Searched'];
                        } //while
                        
                        //add to main data array in session
                        $_SESSION['data'][$rows['Reason']][] = $result_array;
                        unset($result_array);
      }//while
      





file line_plot.php


for each Reason in $_SESSION['data']{
$lineplot =new LinePlot($result_array for Reason);
$lineplot->SetLegend('Reason');
// Add the plot to the graph
$graph->Add( $lineplot);
}//for each reason



I am pretty new to PHP so any help would be greaty appreciated with my code
0
chocobogo
Asked:
chocobogo
  • 6
  • 5
  • 2
1 Solution
 
Marcus BointonCommented:
This looks like you're using jpgraph. It all looks fine - do you have a particular problem? A couple of comments - To ease searching and sorting, it's probably a good idea to have separate start and end date fields that are real date fields and not just strings. Are you really storing results data like this? Do you not derive it from the underlying individual cases, or doesn't your data go that deep?
0
 
chocobogoAuthor Commented:
The data doesnt go in that deep... But I am storing the years as strings as they are mostly in the format 2003/04 etc

My problem is in the line_plot.php
I cant seem to get any php code to do what I have set out above.
0
 
Marcus BointonCommented:
The main thing is to take a look at the contents of the array that you've built and make sure it looks right; try adding a var_dump($_SESSION['data']) after building it. Also, test your graph drawing code by passing in a known-good array manually.

Failing that, post your real code.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
TheWebMonsterCommented:

If I understand it , you are trying to build a multi-dimentional array called 'data' with element 'reason' as an array.

To do that, you need to change the line:

 $_SESSION['data'][$rows['Reason']][] = $result_array;

 TO :    

 $_SESSION['data'][$rows['Reason']] = $result_array;

In your code you are not building 'reason' as an array, you are building it as an element containing an array.

I think you are going one level deeper in the multi-dimentional array, then what is required.

Try to delete the [] , and see if it works.





0
 
chocobogoAuthor Commented:
When I print the data array it seems to look ok but I cant get the code to work in the line_plot.php

for each Reason in $_SESSION['data']{
$lineplot =new LinePlot($result_array for Reason);
$lineplot->SetLegend('Reason');
// Add the plot to the graph
$graph->Add( $lineplot);
}//for each reason

im not sure of the code needed to create a for loop to extract each of the reason arrays

The line_plot.php works fine when I pass it a single array of data, its just I cant get it to work when I want to plot a number of lines on the graph...

Its just the code to grab the individual arrays out of the multidimensional array that I am finding tricky
0
 
TheWebMonsterCommented:
did you try nested foreach loops?

foreach($_SESSION['data'] as $key => $value)
      {
            foreach($value as $k => $v)
                  {
                        // $v is $result_array for Reason
                        $lineplot = new LinePlot($v);
                        $lineplot->SetLegend('Reason');
                        // Add the plot to the graph
                        $graph->Add( $lineplot);
                  }
      }
0
 
chocobogoAuthor Commented:
TheWebMonster  - thanks that code produces the following when I do a print_R

Array ( [0] => 2433 [1] => 2149 [2] => 2737 [3] => 2501 ) Drugs
Array ( [0] => 144 [1] => 152 [2] => 108 [3] => 162 ) Firearms
Array ( [0] => 1147 [1] => 913 [2] => 1214 [3] => 1219 ) Going equipped
Array ( [0] => 729 [1] => 584 [2] => 583 [3] => 807 ) Offensive weapons
Array ( [0] => 1630 [1] => 1415 [2] => 2169 [3] => 2905 ) Other
Array ( [0] => 5576 [1] => 4612 [2] => 5414 [3] => 6434 ) Stolen Property

Which is great... but now in my line_plot code it seems to only plot the last set.. maybe it is overwriting the previous lineplots as they have the same name....

line_plot.php

foreach($_SESSION['data'] as $key => $value)
     {
          foreach($value as $k => $v)
               {
                    // $v is $result_array for Reason
                    $lineplot = new LinePlot($v);
                $lineplot->SetLegend($key);
                    // Add the plot to the graph
                    $graph->Add($lineplot);
               }
     }
unset($_SESSION['data']);

This produces a graph with 1 plot and 5 legends all saying "Stolen Property"

The data array  looks like this when I print it if that helps
 [data] => Array ( [Drugs] => Array ( [0] => Array ( [0] => 2433 [1] => 2149 [2] => 2737 [3] => 2501 ) ) [Firearms] => Array ( [0] => Array ( [0] => 144 [1] => 152 [2] => 108 [3] => 162 ) ) [Going equipped] => Array ( [0] => Array ( [0] => 1147 [1] => 913 [2] => 1214 [3] => 1219 ) ) [Offensive weapons] => Array ( [0] => Array ( [0] => 729 [1] => 584 [2] => 583 [3] => 807 ) ) [Other] => Array ( [0] => Array ( [0] => 1630 [1] => 1415 [2] => 2169 [3] => 2905 ) ) [Stolen Property] => Array ( [0] => Array ( [0] => 5576 [1] => 4612 [2] => 5414 [3] => 6434 ) ) )

Cheers for  the help so far..:)
0
 
TheWebMonsterCommented:
replace  $lineplot->SetLegend($key);

with   $lineplot->SetLegend($k);

sorry about that, happens when you cut 'n' paste
0
 
chocobogoAuthor Commented:
TheWebMonster $lineplot->SetLegend($key); is correct...
The problem is that I am only getting one plot on the graph instead of 6

Here is a screenshot.. hopefully it displays my problem more clearly

http://www.lemoncubed.co.uk/multi_plot.jpg

Thanks again
0
 
TheWebMonsterCommented:
Here is the code tested and working:

$reasons = 'SELECT DISTINCT Reason FROM 28_evidence';
$results = mysql_query($reasons) or die(mysql_error());
     
   while ($rows=mysql_fetch_assoc($results))
           {
     
           //fill the legend array
           $legend_array[] = $rows['Reason'];
          
           //obtain results for each reason
           $sql = "SELECT Searched FROM 28_evidence WHERE Reason=\"".$rows['Reason']."\"";
      
           //Insert data into array
           $results1 = mysql_query($sql) or die(mysql_error());
             
             while ($rows2=mysql_fetch_assoc($results1))
                   {
                        //store the results for each period
                         $result_array[] = $rows2['Searched'];
                } //while
                   
             //add to main data array in session
             $_SESSION['data'][$rows['Reason']] = $result_array;
             unset($result_array);
       }//while

// line colours in graph
$colours = array('red','blue','green','yellow','pink','navy');

// counter
$y = 0;

$graph = new Graph(350, 250,"auto");    
$graph->SetScale( "textlin");
     
foreach($_SESSION['data'] as $key => $value)
     {
           // ${'lineplot'.$y} is a 'variable variable' created so we can add multiple lines to graph
           // $value is $result_array for Reason
        ${'lineplot'.$y} = new LinePlot($value);
        ${'lineplot'.$y}->SetColor($colours[$y]);
        ${'lineplot'.$y}->SetLegend($key);
        // Add the plot to the graph
        $graph->Add(${'lineplot'.$y});
        $y++; // increase counter
     }

$graph->Stroke();
0
 
TheWebMonsterCommented:
Starting from // line colour in graph
supposed to be in your line_plot.php file
0
 
chocobogoAuthor Commented:
TheWebMonster - excellent... Thanks for all your help... the plot looks great.. thanks for the colours part too..
0
 
TheWebMonsterCommented:
You are welcome , glad I was able to help.
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

  • 6
  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now