Rails: printing mysql query - php

I cant figure out how to display MySql information using Ruby on Rails.
This is the query i want to run
SELECT name, description FROM projects where status > 1
If someone can translate to Rails the code below it would be greatly apreciatted
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Proyects</title>
</head>
<?php
$dsn = "mysql:dbname=redmine_default";
$username = "root";
$password = "";
try {
$conn = new PDO( $dsn, $username, $password );
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
} catch ( PDOException $e ) {
echo "Error: " . $e->getMessage();
}
function mostrarProyectos($conn) {
$sql = 'SELECT * FROM projects';
foreach ($conn->query($sql) as $row) {
print $row['name'] . "\t";
print $row['id'] . "\t";
print $row['description'] . "\n";
}
}
//creating a table with 4 columns
?>
<table border="1" cellspacing=1 cellpadding=2 style="font-size: 8pt"><tr>
<td><font face="verdana"><b>ID</b></font></td>
<td><font face="verdana"><b>Name</b></font></td>
<td><font face="verdana"><b>Status</b></font></td>
<td><font face="verdana"><b>Desc</b></font></td>
</tr>
<?php
//here comes the SQL query
$query = "SELECT name, description FROM projects where status > 1";
$resultado = $conn->query($query);
$numero = 0;
foreach($resultado as $row)
{
echo "<tr><td width=\"25%\"><font face=\"verdana\">" .
$row["id"] . "</font></td>";
echo "<td width=\"25%\"><font face=\"verdana\">" .
$row["name"] . "</font></td>";
echo "<td width=\"25%\"><font face=\"verdana\">" .
$row["status"] . "</font></td>";
echo "<td width=\"25%\"><font face=\"verdana\">" .
$row["description"]. "</font></td></tr>";
$numero++;
}
echo "<tr><td colspan=\"15\"><font face=\"verdana\"><b>N&uacutemero: " . $numero .
"</b></font></td></tr>";
mysql_free_result($result);
mysql_close($link);
?>
</table
</body>
</html>

The RoR code would be something like this:
Project.where("status > ?", 10)
http://guides.rubyonrails.org/active_record_querying.html
The link will give you a large list of mysql queries and their RoR counterpart.

Related

when i search my database instead of getting results i get error 500

i have a website that is supposed to search the database for a specific id, but i get an error 500 when the submit button is clicked
i am running php7 and the latest mysql, ive tried removing un-needed code, ive tried changing the method i use to get information from the database, no luck
"dbh.inc.php"
<?php
$servername = "localhost";
$dBUsername = "root";
$dBPassword = "";
$dBName = "chromebook";
$conn = mysqli_connect($servername, $dBUsername, $dBPassword, $dBName);
if (!$conn) {
die("Connection Failed: ".mysqli_connect_error());
}
"search.inc.php(destination page, shows results)"
<!DOCTYPE html>
<?php
require "dbh.inc.php";
?>
<html>
<head>
<title>Searching For Data</title>
</head>
<body>
<?php
echo "<h2>Search Results</h2>
if(isset($_POST['search'])) {
$search = $_POST['search'];
if ($search == "") {
echo "<p>You Didn't Submit Data! Try Again!</p><a
href="myurlusuallyisherebutitsforaimportantthingandidontwantusersgoingtoit"</a>";
exit;
}
$search = strtoupper($search);
$search = strip_tags($search);
$search = trim($search);
$result = mysqli_query("SELECT * FROM chrome WHERE Teacher LIKE '%search%'");
echo "<table class="table" border='1'>
<tr>
<th>Room Number</th>
<th>Teacher</th>
<th>Front Barcode</th>
<th>Back Barcode</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['RoomNum'] . "</td>";
echo "<td>" . $row['Teacher'] . "</td>";
echo "<td>" . $row['FrontId'] . "</td>";
echo "<td>" . $row['Barcode'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($conn);
?>
</body>
</html>
i expect the page to load with the results asked from the database, instead i get an "http error 500" message from my browser
thanks for any help in advance
First parameter of the mysqli_query should be $conn
search.inc.php
<!DOCTYPE html>
<?php
require "dbh.inc.php";
?>
<html>
<head>
<title>Searching For Data</title>
</head>
<body>
<?php
echo "<h2>Search Results</h2>";
if(isset($_POST['search'])) {
$search = $_POST['search'];
if ($search == "") {
echo "<p>You Didn't Submit Data! Try Again!</p><a
href="myurlusuallyisherebutitsforaimportantthingandidontwantusersgoingtoit"</a>";
exit;
}
$search = strtoupper($search);
$search = strip_tags($search);
$search = trim($search);
$result = mysqli_query($conn, "SELECT * FROM chrome WHERE Teacher LIKE '%search%'");
echo "<table class='table' border='1'>
<tr>
<th>Room Number</th>
<th>Teacher</th>
<th>Front Barcode</th>
<th>Back Barcode</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['RoomNum'] . "</td>";
echo "<td>" . $row['Teacher'] . "</td>";
echo "<td>" . $row['FrontId'] . "</td>";
echo "<td>" . $row['Barcode'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($conn);
?>
</body>
</html>
echo "<h2>Search Results</h2> is incomplete; add the closing ";
Unclosed if statement at if(isset($_POST['search'])) {
Unescaped double quotes on echo "<p>You Didn't Submit Data! Try Again!</p><a
href="myurlusuallyisherebutitsforaimportantthingandidontwantusersgoingtoit"</a>"; and echo "<table class="table" border='1'>
Also, I would highly recommend adding some error checking to your code so these 500 errors aren't a total mystery.
This works great for on page error reporting:
error_reporting(E_ALL);
ini_set('display_errors', 1);
So in total, replace your existing code with this:
<html>
<head>
<title>Searching For Data</title>
</head>
<body>
<?php
// added error reporting
error_reporting(E_ALL);
ini_set('display_errors', 1);
echo "<h2>Search Results</h2>"; //fixed
if(isset($_POST['search'])) {
$search = $_POST['search'];
if ($search == "") {
echo "<p>You Didn't Submit Data! Try Again!</p><a href=\"myurlusuallyisherebutitsforaimportantthingandidontwantusersgoingtoit\"</a>"; // escape quotes
exit;
}
$search = strtoupper($search);
$search = strip_tags($search);
$search = trim($search);
$result = mysqli_query($conn, "SELECT * FROM chrome WHERE Teacher LIKE '%search%'"); // connection param added
echo "<table class=\"table\" border='1'> // escape quotes
<tr>
<th>Room Number</th>
<th>Teacher</th>
<th>Front Barcode</th>
<th>Back Barcode</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['RoomNum'] . "</td>";
echo "<td>" . $row['Teacher'] . "</td>";
echo "<td>" . $row['FrontId'] . "</td>";
echo "<td>" . $row['Barcode'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($conn);
} // closing bracket from main 'if' statement
?>

converting php code to html

I have been having hard time trying to convert my php code to html for 5 hours and at this point, I'm really burned out :X.
Here's my Php code
<?php
$con=mysqli_connect("localhost","dbuser","pw","dbname");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM tablea");
echo "<table border='1' >
<tr>
<th>id</th>
<th>Subject_Code</th>
<th>date</th>
<th>name</th>
<th>description</th>
<th>Other_Details</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['Subject_Code'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td width='600' class='table_width'>" . $row['description'] . "</td>";
echo "<td width='600' class='table_width' align='center'>" . $row['Other_Details'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
and here's what i have done so far for HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>database connections</title>
</head>
<body>
<?php
$username = "dbuser";
$password = "pw";
$host = "localhost";
$database= "dbname";
$connector = mysql_connect($localhost,$dbuser,$pw,dbname)
or die("Unable to connect");
echo "Connections are made successfully::";
$selected = mysql_select_db("test_db", $connector)
or die("Unable to connect");
//execute the SQL query and return records
$result = mysql_query("SELECT * FROM tablea ");
?>
<table border="2">
<thead>
<tr>
<th>id</th>
<th>Subject_Code</th>
<th>date</th>
<th>name</th>
<th>description</th>
<td>Other_Details</td>
</tr>
</thead>
<tbody>
<?php
while ($row = mysql_fetch_array($result)) {
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['Subject_Code']; ?></td>
<td><?php echo $row['date']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['description']; ?></td>
<td><?php echo $row['Other_Details']; ?></td>
</tr>
<?php mysql_close($connector); ?>
</body>
</html>
little Help here please, Thanks !!
EDIT: seems like some people are not understanding my question. My php is working fine so i want to convert the php code into html. Basically, i want my table from database to show up using HTML table.
you not close while;
so change code to :
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>database connections</title>
</head>
<body>
<?php
/////////////////////////////////// change \\\
$username = "dbuser";
$password = "pw";
$host = "localhost";
$database= "dbname";
$connector = mysql_connect($localhost,$username,$password)
or die("Unable to connect");
echo "Connections are made successfully::";
$selected = mysql_select_db($database, $connector)
or die("Unable to connect");
/////////////////////////////////// end change \\\
//execute the SQL query and return records
$result = mysql_query("SELECT * FROM tablea ");
?>
<table border="2">
<thead>
<tr>
<th>id</th>
<th>Subject_Code</th>
<th>date</th>
<th>name</th>
<th>description</th>
<td>Other_Details</td>
</tr>
</thead>
<tbody>
<?php
while ($row = mysql_fetch_array($result)) :
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['Subject_Code']; ?></td>
<td><?php echo $row['date']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['description']; ?></td>
<td><?php echo $row['Other_Details']; ?></td>
</tr>
<?php endwhile;?>
<?php mysql_close($connector); ?>
</body>
</html>
Actually your problem is as you said like html version there is no variable like $localhost but your passing the parameter to connect mysql etc. below error code shows the variable mismatch of your code.
Error code :
<?php
$username = "dbuser";
$password = "pw";
$host = "localhost";
$database= "dbname";
$connector = mysql_connect($localhost,$dbuser,$pw,dbname);
^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^
//here there is no variable like what you passing to connect mysql that's problem here
?>
solution :
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>database connections</title>
</head>
<body>
<?php
$con=mysqli_connect("localhost","dbuser","pw","dbname");
// Check connection
if(mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM tablea");
echo "<table border='1' >
<tr>
<th>id</th>
<th>Subject_Code</th>
<th>date</th>
<th>name</th>
<th>description</th>
<th>Other_Details</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['Subject_Code'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td width='600' class='table_width'>" . $row['description'] . "</td>";
echo "<td width='600' class='table_width' align='center'>" . $row['Other_Details'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</body>
</html>
Here's another version that makes use of PDO - by far the superior of php connectors in terms of maintainability and versatility. I have the same code working under MySQL, SQLite and SQLServer - the only thing that changes is the connection string.
You'll note I pull all of the results at once. I also make use of the fact that we get back an array. Each element of that array is a row. Each row is an array of cells. This greatly reduces the code needed to output a result-set. We just need two forEach loops and that's it.
<?php
$host = 'localhost';
$userName = 'dbuser';
$password = 'pw';
$nameOfDb = 'dbname';
$nameOfTable = 'tablea';
$pdo = new PDO('mysql:host='.$host, $userName, $password);
$pdo->query("use " . $nameOfDb);
// desired results requested explicitly, so when we get an array for the row's data, the elements will be in the same order
// - not required if the order of the columns in the database matches the desired display order. (we could just use 'select *' then)
//$query = $pdo->prepare('select * from '.$nameOfTable);
$query = $pdo->prepare('select id, Subject_Code, date, name, description, Other_Details from '. $nameOfTable);
$query->execute();
$query->setFetchMode(PDO::FETCH_ASSOC);
$rows = $query->fetchAll();
?><!doctype html>
<html>
<head>
</head>
<body>
<table>
<thead>
<tr>
<th>id</th><th>Subject_Code</th><th>date</th><th>name</th><th>description</th><th>Other_Details</th>
</tr>
</thead>
<tbody><?php
forEach($rows as $curRow)
{
echo '<tr>';
// use this one if you want to display the columns in the same order they are returned in the query results
// AND you'd like to display all columns in the result
forEach($curRow as $curCell)
echo '<td>'.$curCell.'</td>';
// otherwise, you can request the desired elements by name
//
// echo '<td>' . $curCell['id'] . '</td>'
// echo '<td>' . $curCell['Subject_Code'] . '</td>'
echo '</tr>';
}
?></tbody>
</table>
</body>
Download HTTrack Website Copier and install and run the soft. Run your script and paste your URL in web copier software. You will get HTML output in your desire folder

implode in where class

I am having a problem with array while using it in "select" statement
that array contains the following strings
Array
(
[0] => M.A.JINNA
[1] => K.DHANA RAJU
[2] => B.EPHRIM
)
array data had came from the following data
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<style>
table{
border : 1px solid black;
}
tr{
border : 1px solid black;
}
td{
border : 1px solid black;
}
</style
<head>
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Update Deployement</title>
<style type="text/css">
body
{
background-color: #00FF00;
background-image: url(images/gail-india.jpg);
color: #000000;
scrollbar-face-color: #0B0B0B;
scrollbar-arrow-color: #C8C8C8;
scrollbar-3dlight-color: #0B0B0B;
scrollbar-darkshadow-color: #000000;
scrollbar-highlight-color: #141414;
scrollbar-shadow-color: #060606;
scrollbar-track-color: #0B0B0B;
}
</style>
</head>
<body>
<form method="post" action="edit_data.php">
<div id="wb_Image3" style="margin:0;padding:0;position:absolute;left:7px;top:4px;width:208px;height:129px;text-align:left;z-index:0;">
<img src="images/image_thumb3.png" id="Image2" alt="" border="0" style="width:208px;height:129px;"></div>
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("gail", $con);
$installation_1 = trim($_POST['installation']);
$area_1 = trim($_POST['area']);
$district_1 = trim($_POST['district']);
if(empty($area_1) AND empty($district_1))
{
$sql = "SELECT * FROM deployment WHERE installation ='" . $installation_1 . "'";
}
else if(empty($installation_1) AND empty($district_1))
{
$sql = "SELECT * FROM deployment WHERE area ='" . $area_1 . "'";
}
else if(empty($installation_1) AND empty($area_1))
{
$sql = "SELECT * FROM deployment WHERE district ='" . $district_1 . "'";
}
else if(empty($district_1))
{
$sql = "SELECT * FROM deployment WHERE installation ='" . $installation_1 . "' AND area ='" . $area_1 . "'";
}
else if(empty($area_1))
{
$sql = "SELECT * FROM deployment WHERE installation ='" . $installation_1 . "' AND district ='" . $district_1 . "'";
}
else if(empty($installation_1))
{
$sql = "SELECT * FROM deployment WHERE area ='" . $area_1 . "' AND district ='" . $district_1 . "'";
}
else
{
$sql = "SELECT * FROM deployment WHERE installation ='" . $installation_1 . "' AND area ='" . $area_1 . "' AND district ='" . $district_1 . "'";
}
$result = mysql_query($sql);
echo "<table id='table1' width = '500' align = 'center' style= 'border:1px'>";
echo "<tr><b>";
echo "<td>Installation</td>";
echo "<td>Area</td>";
echo "<td>District</td>";
echo "<td>Employee Name</td>";
echo "<td>Reference</td>";
echo "</b></tr>";
$employee = array();
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo ("<td>$row[installation]</td>");
echo ("<td>$row[area]</td>");
echo ("<td>$row[district]</td>");
echo ("<td>$row[employeename]</td>");
echo ("<td>$row[ref]</td>");
echo"</tr>";
$employee[] = $row['employeename'];
$arrlength = count($employee);
}
echo"</table>";
echo '<pre>'; print_r(array_filter($employee)); echo '</pre>';
?>
</body>
</html>
now I am getting error like this: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''M.A.JINNA,K.DHANA RAJU,B.EPHRIM'' at line 1
please give me any suggestions, thanks in advance
NOTE: I cannot use employee id instead of employee name due to client request
the code is
<?
$emp = implode( ',', $employee );
echo '<pre>'; print_r($emp); echo '</pre>';
$sql = "SELECT * FROM securitystaffdetails WHERE employeename IN '" . $emp . "'";
$result = mysql_query($sql) or die(mysql_error());
echo "<table id='table1' width='1500' style= 'border:1px'>";
echo "<tr><b>";
echo "<td>Employee Name</td>";
echo "<td>Address</td>";
echo "<td>DOB</td>";
echo "<td>Age</td>";
echo "<td>SEx</td>";
echo "<td>Mobile Number</td>";
echo "<td>Blood Group</td>";
echo "<td>ID Card</td>";
echo "<td>Ex Army Idcard</td>";
echo "<td>Police Clearence</td>";
echo "<td>ESI Card</td>";
echo "<td>PF Account</td>";
echo "<td>PAN Card</td>";
echo "<td>Voter ID</td>";
echo "<td>Ration/Family</td>";
echo "</b></tr>";
$employee = array();
while($record = mysql_fetch_object($result))
{
echo "<tr>";
echo ("<td>$record[employeename]</td>");
echo ("<td>$record[address]</td>");
echo ("<td>$record[dob]</td>");
echo ("<td>$record[age]</td>");
echo ("<td>$record[sex]</td>");
echo ("<td>$record[mobn]</td>");
echo ("<td>$record[bg]</td>");
echo ("<td>$record[icard]</td>");
echo ("<td>$record[exarmycard]</td>");
echo ("<td>$record[policeclearence]</td>");
echo ("<td>$record[esicard]</td>");
echo ("<td>$record[pfa]</td>");
echo ("<td>$record[pancard]</td>");
echo ("<td>$record[acard]</td>");
echo ("<td>$record[vcard]</td>");
echo ("<td>$record[rcard]</td>");
echo"</tr>";
}
echo"</table>";
?>
Change your implode line to be:
$emp = implode( "','", $employee );
and your query to be:
"SELECT * FROM securitystaffdetails WHERE employeename IN ('" . $emp . "')";
you missed ().
$sql = "SELECT * FROM securitystaffdetails WHERE employeename IN ('" . $emp . "')";

Mysql table not displayed as html table in cloud 9

hi I am trying to execute the following code on cloud 9
:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Test | Products</title>
</head>
<body>
<?php
$con=mysqli_connect(0.0.0.0,"ritikasahay","","trydb");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM veg");
echo "<table border='1'>
<tr>
<th>Name</th>
<th>Price</th>
<th>Image</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['price'] . "</td>";
echo "<td>" . $row['image'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</body>
</html>
when i am executing this code, i am getting just the following php code as result instead of the table contents:
Name Price Image "; while($row = mysqli_fetch_array($result)) { echo ""; echo "" . $row['name'] . ""; echo "" . $row['price'] . ""; echo "" . $row['image'] . ""; echo ""; } echo ""; mysqli_close($con); ?>
can someone tell me whre i am going wrong?
chang this command:
$con=mysqli_connect(0.0.0.0,"ritikasahay","","trydb");
To like this:
$con=mysqli_connect("0.0.0.0","ritikasahay","","trydb");
Table display once instead of multiple times
<!DOCTYPE html>
<html lang="en">
<head>
<title>Test | Products</title>
</head>
<body>
<?php
$con=mysqli_connect("0.0.0.0","ritikasahay","","trydb");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM veg");
//create table
$table = "<table border='1'>
<tr>
<th>Name</th>
<th>Price</th>
<th>Image</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
//bind rows
$table .= "<tr>";
$table .= "<td>" . $row['name'] . "</td>";
$table .= "<td>" . $row['price'] . "</td>";
$table .= "<td>" . $row['image'] . "</td>";
$table .= "</tr>";
}
//close table
$table .= "</table>";
//display table
echo $table;
mysqli_close($con);
?>
</body>
</html>

Failed to open error in libchart library in php

I'm using Libchart library in my project to generate graphs. I'm trying to execute example in libchart but I'm getting following error.
imagepng(generated/demo1.png): failed to open stream: No such file or
directory in
C:\xampp\htdocs\test\fileupload\libchart\classes\view\plot\Plot.php on
line 284
this is my code
include "libchart/classes/libchart.php";
$chart = new VerticalBarChart(500, 250);
$dataSet = new XYDataSet();
$dataSet->addPoint(new Point("Jan 2005", 273));
$dataSet->addPoint(new Point("Feb 2005", 321));
$dataSet->addPoint(new Point("March 2005", 442));
$dataSet->addPoint(new Point("April 2005", 711));
$chart->setDataSet($dataSet);
$chart->setTitle("Monthly usage for www.example.com");
$chart->render("generated/demo1.png");
when I change the last line of code to $chart->render(); then it opens binary format in my browser.
Could any one please help me to solve this error?
this is my complete code
<?php
include "libchart/classes/libchart.php";
class fileupload
{
var $htmlID_type = "type";
var $type_uploadfile = 0;
var $type_generategraphs = 1;
var $content_html = "";
function __construct()
{
$this->__switch();
$this->__print();
}
function __switch()
{
enter code here
$this->param_type = $this->getParamType();
switch ($this->param_type)
{`enter code here`
case $this->type_uploadfile :
$this->selectFileUplaod ();
break;
case $this->type_generategraphs :
$this->generateGraphs();
break;
}
}
function __print()
{
print <<<HEREDOC
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style>
body{
margin-left:20%;
margin-right:20%;
margin-top:50px;
}
table, td, th
{
border:1px solid black;
}
th
{
background-color:#00BFFF;
color:white;
}
</style>
</head>
<body>
{$this->content_html}
</body>
</html>
HEREDOC;
}
function getParamType()
{
if (isset ( $_GET [$this->htmlID_type] ))
{
return $_GET [$this->htmlID_type];
}
else
return $this->type_uploadfile;
}
function selectFileUplaod()
{
$con=mysqli_connect("localhost","root","","test");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT
income_id,
month,
paint_cost,
parts_cost,
repair_cost,
excess_Suspense,
sublet,
remove_refit_cost
FROM income");
echo "<h3>Bundamba Income</h3>";
echo "<table>
<tr>
<th>Paint</th>
<th>Parts</th>
<th>Remove Refit Cost</th>
<th>Repair</th>
<th>Excess Suspense</th>
<th>Sublet</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td width=100>" . $row['paint_cost'] . "</td>";
echo "<td width=100>" . $row['parts_cost'] . "</td>";
echo "<td width=100>" . $row['remove_refit_cost'] . "</td>";
echo "<td width=100>" . $row['repair_cost'] . "</td>";
echo "<td width=100>" . $row['excess_Suspense'] . "</td>";
echo "<td width=100>" . $row['sublet'] . "</td>";
echo "</tr>";
}
echo "</table>";
echo "<br>";
$result1 = mysqli_query($con,"SELECT
cost_of_saleID , paint_liquid ,
paint_consumables,
workshop_consumables,
parts_purchased,
wages_paintshop,
wages_workshop,
suplier_discount,
sublet_cost
FROM cost_of_sale
LIMIT 0 , 30");
echo "<h3>Bundamba cost of sale</h3>";
echo "<table>
<tr>
<th>Paint Consumables</th>
<th>Paint Liquid</th>
<th>Parts Purchased</th>
<th>Workshop Consumables</th>
<th>Wages - Paintshop</th>
<th>Wages - Workshop</th>
<th>Supplier Discounts</th>
<th>Sublets Costs</th>
</tr>";
while($row = mysqli_fetch_array($result1))
{
echo "<tr>";
echo "<td>" . $row['paint_consumables'] . "</td>";
echo "<td>" . $row['paint_liquid'] . "</td>";
echo "<td>" . $row['parts_purchased'] . "</td>";
echo "<td>" . $row['workshop_consumables'] . "</td>";
echo "<td>" . $row['wages_paintshop'] . "</td>";
echo "<td>" . $row['wages_workshop'] . "</td>";
echo "<td>" . $row['suplier_discount'] . "</td>";
echo "<td>" . $row['sublet_cost'] . "</td>";
echo "</tr>";
}
echo "</table>";
$this->content_html = <<<End_Of_String
<FORM action="uploadFile.php?{$this->htmlID_type}={$this->type_generategraphs}" method="POST" />
<input type="submit" value="Income vs Cost" style="margin-top:20px">
<input type="submit" value="Income vs Cost Ratio" style="margin-top:20px">
</FORM>
End_Of_String;
// mysqli_close($con);
}
function generateGraphs()
{
echo getcwd();
print "rrrrr";
header("Content-type:image/png");
$chart = new VerticalBarChart(500, 250);
$dataSet = new XYDataSet();
$dataSet->addPoint(new Point("Jan 2005", 273));
$dataSet->addPoint(new Point("Feb 2005", 321));
$dataSet->addPoint(new Point("March 2005", 442));
$dataSet->addPoint(new Point("April 2005", 711));
$chart->setDataSet($dataSet);
$chart->setTitle("Monthly usage for www.example.com");
$chart->render("demo/generated/demo1.png");
readfile("demo/generated/demo1.png");
// $im = imagecreatefrompng("demo/generated/demo1.png");
// header('Content-Type: image/png');
//imagepng($im);
//imagedestroy($im);
}
}
$obj = new fileupload();
?>
I got the same error now when trying to plot the piechart using LibChart library. The problem is that you would not have created a folder named 'generated' in your current folder.. but the name given for the image is generated/demo1.png which says that generated is a folder containing the file "demo1.png". So, just give 'demo1.png' instead of 'generated/demo1.png' and try running the snippet.. I got the output just now!!

Categories