|
php include connection string |
Posted by Jorge Eduardo on Oct-26-2012 15:01 |
|
HI Peter I'd rather post a new thread because my previous thread maybe not clear. The
problem is that need to make a dashboard composed by many charts, all in the same
web page. So for each chart I have to query a database and fetch different data. But I
found that for each chart I must paste the same connection string, what I find not
efficient and maybe error generator due to same variables repeated. Haw can I use just
one connection string, maybe atop of the web page for every chart. Also I always
include a connection string rather than paste on each script, but when I iclude the
connection string on the chart script, the chart doesn't output. Instead if I paste the
connection string the chart renders Ok. Here is an example:
This render the chart perfect but have to paste the conn string:
<?php
$dsn ='XXXXXXXXXXXXXXXXXXXXXXX';
$username='XXXXXXX';
$password='XXXXXXXXXX';
$dbh = new PDO($dsn,$username,$password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$surv_decision=$dbh->query("SELECT case when mr_answer=1 then 'SI' when
mr_answer=0 then 'NO' END as mr_answer, COUNT( DISTINCT id_mr_oltp_01 ) FROM
datellig_mr.mr_oltp_02 WHERE id_mr_question =0 GROUP BY mr_answer;");
foreach($surv_decision as $li)
{
$labels[]=$li[0];
$data[]=$li[1];
}
require("../chd/lib/phpchartdir.php");
# Create a PieChart object of size 360 x 300 pixels
$c = new PieChart(360, 300);
# Set the center of the pie at (180, 140) and the radius to 100 pixels
$c->setPieSize(180, 140, 100);
# Set the pie data and the pie labels
$c->setData($data, $labels);
# Output the chart
header("Content-type: image/png");
print($c->makeChart2(PNG));
?>
But the way I want is this, but this script doesn't show any chart.
<?php
include("../obj/fnc_dbcon_mysql_local.php"); //db connexion
$surv_decision=$dbh->query("SELECT case when mr_answer=1 then 'SI' when
mr_answer=0 then 'NO' END as mr_answer, COUNT( DISTINCT id_mr_oltp_01 ) FROM
datellig_mr.mr_oltp_02 WHERE id_mr_question =0 GROUP BY mr_answer;");
foreach($surv_decision as $li)
{
$labels[]=$li[0];
$data[]=$li[1];
}
require("../chd/lib/phpchartdir.php");
# Create a PieChart object of size 360 x 300 pixels
$c = new PieChart(360, 300);
# Set the center of the pie at (180, 140) and the radius to 100 pixels
$c->setPieSize(180, 140, 100);
# Set the pie data and the pie labels
$c->setData($data, $labels);
# Output the chart
header("Content-type: image/png");
print($c->makeChart2(PNG));
?>
Thanks, for the advisory. Jorge |
Re: php include connection string |
Posted by Peter Kwan on Oct-26-2012 17:49 |
|
Hi Jorge,
I do not see this thread before I responded to your other thread. According to your information, I suspect "../obj/fnc_dbcon_mysql_local.php" outputs HTML. As mentioned in PHP, everything not inside the <?php ... ?> block is HTML. Even an empty space or empty line is HTML. So the fnc_dbcon_mysql_local.php should only contain the following code, with no leading empty lines or trailing empty lines.
<?php
$dsn ='XXXXXXXXXXXXXXXXXXXXXXX';
$username='XXXXXXX';
$password='XXXXXXXXXX';
$dbh = new PDO($dsn,$username,$password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
?>
Also, you may try to access the fnc_dbcon_mysql_local.php directly (by entering its URL in the browser address bar) to check if it really does not output anything. (In the browser, use "View Source" to verfiy that it really does not output anything.)
Hope this can help.
Regards
Peter Kwan |
Re: php include connection string |
Posted by Jorge Eduardo on Oct-26-2012 21:56 |
|
Hi Peter it was solved quickly. Thanks, as always support is excellent. Many, many,
many thanks. Muchas gracias amigo!
Jorge |
Re: php include connection string |
Posted by Jorge Eduardo on Oct-26-2012 23:02 |
|
Hi Peter, I forgot to ask you a very important question:
I need to create a dashboard like the draft seen in the attached image. That means that
need to insert many acripts, one for each chart, each with it's call to connection string. Is
there a way to, generate dynamically, whith a for loop, int html part each script? Cause
each dashboard is dynamically generated by the web user.
Thanks
Jorge
|
Re: php include connection string |
Posted by Peter Kwan on Oct-26-2012 23:21 |
|
Hi Jorge,
If I were you, I will use something like:
<html><body>
<?php
$NOdata = array(..............);
$SIdata = array(..............);
for ($i = 0; $i < 10; ++$i)
echo "<img src='makeMeter.php?aaa=$NOdata[$i],$SIdata[$i]'>";
?>
....
In makeMeter.php, you can create a script that draws a bar chart by using data from the query parameter "aaa".
Hope this can help.
Regards
Peter Kwan |
Re: php include connection string |
Posted by Jorge Eduardo on Oct-27-2012 00:47 |
|
Ok Peter I'm gonna check. Thanks, I'll tell you how it gone.
Thanks, Jorge |
Re: php include connection string |
Posted by Jorge Eduardo on Oct-27-2012 04:00 |
|
Hi Peter I sent a wrong illustration. Each chart is different. Please check this one.
THnaks, Jorge
|
Re: php include connection string |
Posted by Peter Kwan on Oct-28-2012 01:36 |
|
Hi Jorge,
The code is essentially the same. Basically, you can use a loop to generate the URL necessary for the charts. For example:
#an array of text strings, representing the URLs for the charts
$myURLs = array(..............);
for ($i = 0; $i < count($myURLs); ++$i)
echo "<img src='$myURLs[$i]'>";
Hope this can help.
Regards
Peter Kwan |
|