Jump to content

[SOLVED] Arrays and variables help


Gazz1982

Recommended Posts

I have created a graph using the below code.

 

<?include("phpgraphlib.php");

$graph=new PHPGraphLib(400,300);

$data=array("1"=>20, "2"=>198, "3"=>70, "4"=>90);

$graph->addData($data);

$graph->setTitle("Definitions");

$graph->setGradient("red", "maroon");

$graph->setBarOutlineColor("black");

$graph->setTextColor("blue");

$graph->createGraph();

// Closing connection

mysql_close($link);

?>

 

This is called from a html page using

 

<img src="bar_graph.php" />

 

I have a database using mysql, how would i link this into the graph?

ie from $data=array("1"=>20, "2"=>198, "3"=>70, "4"=>90); to $data=array("1"=$a, "2"=$b, "3"=>$c, "4"=>$d);

 

in this case the $a/b/c and d would be called from a query

 

I tried the above $data=array... but it didn't work any ideas?

 

Gary

Link to comment
https://forums.phpfreaks.com/topic/61019-solved-arrays-and-variables-help/
Share on other sites

I'm connecting to a mysql database and conducting a query using:

 

<?php

// Connecting, selecting database

$link = mysql_connect('127.0.0.1', 'root', '')

    or die('Could not connect: ' . mysql_error());

//echo 'Connected successfully';

mysql_select_db('gary') or die('Could not select database');

// Performing SQL query

// Retrieve all the data from the "example" table

$result = mysql_query("SELECT count(*) AS def1 FROM answer where def=1")

 

or die(mysql_error()); 

while ($row = mysql_fetch_array ($result)) {

echo"<table><tr><td>Definition 1 count=".$row["def1"]."</td></tr>\n";

 

}

mysql_free_result ($result);

 

 

this echos the result of the query then I need $a=".$row["def1"]." followed by the graph code

try

<?php
$sql = "SELECT def, count(*) AS def1 
        FROM answer
        GROUP BY def";
        
$result = mysql_query($sql);

$data = array();

while (list($def, $def1) = mysql_fetch_row($result))
{
    $data[$def] = $def1;
}
?>

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.