?
Solved

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

Posted on 2005-03-02
13
Medium Priority
?
236 Views
Last Modified: 2008-03-17
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
Comment
Question by:chocobogo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 2
13 Comments
 
LVL 25

Expert Comment

by:Marcus Bointon
ID: 13441512
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
 

Author Comment

by:chocobogo
ID: 13441578
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
 
LVL 25

Expert Comment

by:Marcus Bointon
ID: 13441997
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
Tutorial: Introduction to Managing a Linux Server

In this tutorial on systemd, we will explore:
-OS/Distro Adoption
-chkconfig and Other Legacy Commands
-Summary and Key Commands

 
LVL 2

Expert Comment

by:TheWebMonster
ID: 13442505

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
 

Author Comment

by:chocobogo
ID: 13442928
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
 
LVL 2

Expert Comment

by:TheWebMonster
ID: 13443196
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
 

Author Comment

by:chocobogo
ID: 13443478
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
 
LVL 2

Expert Comment

by:TheWebMonster
ID: 13444962
replace  $lineplot->SetLegend($key);

with   $lineplot->SetLegend($k);

sorry about that, happens when you cut 'n' paste
0
 

Author Comment

by:chocobogo
ID: 13445548
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
 
LVL 2

Accepted Solution

by:
TheWebMonster earned 400 total points
ID: 13445585
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
 
LVL 2

Expert Comment

by:TheWebMonster
ID: 13445597
Starting from // line colour in graph
supposed to be in your line_plot.php file
0
 

Author Comment

by:chocobogo
ID: 13447893
TheWebMonster - excellent... Thanks for all your help... the plot looks great.. thanks for the colours part too..
0
 
LVL 2

Expert Comment

by:TheWebMonster
ID: 13452112
You are welcome , glad I was able to help.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question