In this below code i want to generate pie chart.and i got this error Undefined variable: mysqli, Call to a member function query() on a non-object at line 29.Please any one help me to rectify the problem to get solution.
<!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>
<title>Pie Chart Demo (LibChart)- http://codeofaninja.com/</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-15" />
</head>
<body>
<?php
//include the library
include "libchart/libchart/classes/libchart.php";
//new pie chart instance
$chart = new PieChart( 500, 300 );
//data set instance
$dataSet = new XYDataSet();
//actual data
//get data from the database
//include database connection
include 'db_connect.php';
//query all records from the database
$query = "select * from programming_languages";
//execute the query
$result = $mysqli->query( $query );
//get number of rows returned
$num_results = $result->num_rows;
if( $num_results > 0){
while( $row = $result->fetch_assoc() ){
extract($row);
$dataSet->addPoint(new Point("{$name} {$ratings})", $ratings));
}
//finalize dataset
$chart->setDataSet($dataSet);
//set chart title
$chart->setTitle("Tiobe Top Programming Languages for June 2012");
//render as an image and store under "generated" folder
$chart->render("generated/1.png");
//pull the generated chart where it was stored
echo "<img alt='Pie chart' src='generated/1.png' style='border: 1px solid gray;'/>";
}else{
echo "No programming languages found in the database.";
}
?>
</body>
</html>
you should not end the <form> until all fields has been printed.
you have a
</form>
<select>....</select>
please output the select first an then close the form. Otherwise the value of your select field will not be submited.
Related
I have this PHP code that fetches data (an image link and some strings) from my database. The goal is for me to be able to add products (its for an online store) to the database and have that PHP file automatically fetch the data required to display it on the site. I've managed to pull the data I needed but I can't seem to find a way to add some CSS to the divs it creates for each product.
I have already tried linking a CSS file and I don't want to go adding it inline (defeats the purpose of it being automatic)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>testdoc</title>
<style>
.prodimg
{
border: 20px, red, solid;
}
</style>
</head>
<body>
</body>
</html>
<?php
include 'db_connection.php';
$conn = OpenCon();
echo "Connected Successfully";
//query voor var
$sql = "SELECT prod_id, prod_name, stock, prod_price,images FROM PRODUCTS WHERE prod_name = 'red shirt' OR prod_type = 'tshirt'";
$result = $conn->query($sql); // kijken of er results zijn
$numberofRows=0;
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
makeDiv($numberofRows,$row);
}
}
else {
echo "0 results";
}
$conn->close();
function makeDiv($numberofRows,$row)
{
$doc = new DOMDocument;
$div = $doc->createElement('div');//Create new <div> tag
$div->setAttribute('class',"prodimg"); // give it a class
$imglink = $doc->createElement('img'); //create new image
$doc->appendChild($div);//Add the <div> tag to document
$imglink->setAttribute('src',$row['images']); //add the source from the database
$imglink->setAttribute('width',"200px");//give it the right size
$imglink->setAttribute('margin',"50px"); // setattribute seems to only work once for some reason
$div->appendChild($imglink); //add the image to the div
echo $doc->saveHTML(); // save everything
}
?>
You could add style block to the page
<style>.prodimg { background: #FF6600 }</style>
I am new to PHP and currently I am using php to create four different conference tables that have some hard-coded data in for now and I am trying to make any of the rows within the four tables selectable and eventually I will save the rows selected into a database, but for now I just can't figure out a way to make the rows selectable using PHP. Below is the current code that displays the four tables on my localhost. If this is not possible with PHP how would I incorporate another language within a php file to make the rows selectable. Thank you all for your help in advance.
Conference Class:
<?php
class Conference
{
protected $teams;
function loadTeams($teams)
{
$this->teams = $teams;
}
function getTeams()
{
return $this->teams;
}
}
?>
Main code:
<?php print( '<?xml version = "1.0" encoding = "utf-8"?>') ?>
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>User selection page</title>
<link rel="stylesheet" href="gameViewStyleSheet.css" type="text/css" />
</head>
<?php
require_once('Conference.php');
for ($j = 0; $j<4; $j++)
{
$loadGameClass = new Conference();
$loadGameClass->loadTeams(array("(1)Gonzaga vs (16)Southern U", "(8)Pittsburgh vs (9)Wichita St", "(5)Wisconsin vs (12)Ole Miss", "(4)Kansas st vs (13)Boise St", "(6)Arizona vs (11)Belmont", "(3)New Mexico vs (14) Harvard", "(7)Notre Dame vs (10)Iowa St", "(2)Ohio St vs (15) Iona"));
$teams = $loadGameClass->getTeams();
echo '<table border="1" align="center">';
switch ($j) {
case 0:
echo "Midwest";
break;
case 1:
echo "West";
break;
case 2:
echo "South";
break;
case 3:
echo "East";
break;
}
for ($i = 0; $i < 8; $i++)
{
$games = $teams[$i];
echo '<tr><td>'.$games.'</td><tr>';
}
echo '</table>';
echo "<br>" . "<br>";
}
?>
<body>
</body>
</html>
If you're talking "selectable" as in front-end interactivity in your browser, that cannot be done with PHP. You need to use JavaScript.
If you just want a link that would tag something as "selected" with a page load and a session value, you could do small form submits for each select/deselect action, and highlight the appropriate rows.
side note: <br> is invalid in XHTML - you need to use <br />
This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 9 years ago.
I'm trying to create a social bookmarking site using php and mysql.
When I save a website's URL, I want to be able to save the site's title, favicon and description in a table in my database, then print them on my page using ajax.
How can I extract those elements from a website?
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
$myServer = "localhost";
$myUser = "root";
$myPass = "'100pushups'";
$myDB = "social_bookmarking";
//connection to the database
$connect = mysqli_connect($myServer,$myUser, $myPass)
or die("Couldn't connect to SQLServer on $myServer");
//select a database to work with
$selected = mysqli_select_db($connect, $myDB)
or die("Couldn't open database $myDB");
var_dump($_POST);
//declare the SQL statement that will query the database
$url = "INSERT INTO url (url ) VALUES ('$_POST[url]')";
if (isset($_POST['value']))
{
// Instructions if $_POST['value'] exist
echo 'Your url is ' .$url;
}
$data = get_meta_tags($url);
print_r($data);
if (!mysqli_query($connect, $url)) {
die('Error: ' . mysql_error());
}
else
{
echo "Your information was added to the database";
}
mysqli_close($connect);
?>
</body>
</html>
I know I'm doing something wrong with my url there, but I don't know how to use a variable as an argument in get_meta_tags, since the function only accepts filenames or strings.
You can get the title by using: (courtesy of https://stackoverflow.com/users/54680/jonathan-sampson)
<?php
if ( $_POST["url"] ) {
$doc = new DOMDocument();
#$doc->loadHTML( file_get_contents( $_POST["url"] ) );
$xpt = new DOMXPath( $doc );
$output = $xpt->query("//title")->item(0)->nodeValue;
} else {
$output = "URL not provided";
}
echo $output;
?>
You can get the favicon using:
<?php
$url = $_POST['url'];
$doc = new DOMDocument();
$doc->strictErrorChecking = FALSE;
$doc->loadHTML(file_get_contents($url));
$xml = simplexml_import_dom($doc);
$arr = $xml->xpath('//link[#rel="shortcut icon"]');
echo $arr[0]['href'];
?>
Finally for the description you can use:
<?php
$tags = get_meta_tags($_POST['url']);
$description = $tags['description'];
echo $description;
?>
There are very smart scripts/classes out there that help getting content from the dom. For instance using smart selectors. I recommend using one of those.
This is a nice example:
http://simplehtmldom.sourceforge.net/
To get the content of the page, use file_get_contents or equal function.
You can use file_get_contents() function to get the favicon for a site(unless it thwarts you for https). Example:
$icon = file_get_contents("http://stackoverflow.com/favicon.ico");
// now save it
Another option is using curl. It's an awesome php extension if you know how to use it.
Using these methods, you can fetch the html content from the sites too. And then can parse them any HTML parser library of PHP. Or can use REGEX(which experts doesn't recommend often).
I am having an issue with my TCPDF output error and I'm not sure how to solve it. It said
"Warning: Cannot modify header information - headers already sent by
(output started at C:\xampp\htdocs\Printing\listing_p.php:14) in
C:\xampp\htdocs\tcpdf\tcpdf.php on line 9043 TCPDF ERROR: Some data
has already been output to browser, can't send PDF file"
I'm quite new to TCPDF and it doesn't have proper tutorial to guide me through. Examples dont help much though...
Here is my codes:
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>
<!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">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>insert page</title></head>
<body>
<?php
ob_start();
require_once("../tcpdf/tcpdf.php");
require_once("../tcpdf/config/lang/eng.php");
$pdf = & new TCPDF("P","mm","A4",true,"UTF-8",false);
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->SetAutoPageBreak(false);
$link = mysql_connect("localhost","root");
if(!$link){
die('Could not connect: '.mysql_error());
}
if(mysql_select_db("New_People",$link)){
mysql_query("SET NAMES utf8");
$result = mysql_query("SELECT * FROM People1");
$pdf->SetMargins(15,20,15);
$pdf->AddPage();
$pdf->SetFont('FreeSerif','B',12);
$pdf->SetFillColor(255,255,255);
$i = 0;
$max = 30;
$row_height = 5;
$backup_group = "";
$pdf->Cell(60,$row_height,'Id',1,0,'C',1);
$pdf->Cell(60,$row_height,'Code',1,0,'C',1);
$pdf->Cell(60,$row_height,'Name',1,0,'C',1);
while($row = mysql_fetch_array($result))
{
$Id = $row['P_Id'];
$Code = $row['P_Code'];
$Name = $row['P_Name'];
if($backup_group != $Id)
{
$pdf->SetFont('FreeSerif','B',12);
$pdf->Cell(120,$row_height,$Id,1,1,'C',1);
}
if($i >$max){
$pdf->AddPage();
$pdf->SetFont('FreeSerif','B',12);
$pdf->SetFillColor(255,255,255);
$pdf->Cell(60,$row_height,'Id',1,0,'C',1);
$pdf->Cell(60,$row_height,'Code',1,0,'C',1);
$pdf->Cell(60,$row_height,'Name',1,0,'C',1);
}
if(!empty($group)){
$pdf->SetFont('FreeSerif','',12);
$pdf->Cell(60,$row_height,$Id,1,0,'C',1);
$pdf->Cell(60,$row_height,$Code,1,0,'C',1);
$pdf->Cell(60,$row_height,$Name,1,0,'C',1);
}
$backup_group = $Id;
$i++;
}
mysql_close($link);
ob_end_clean();
$pdf->Output('mypdf.pdf','I');
}
else{
die("Error: ".mysql_error());
}
?>
</body>
</html>
Any suggestion or help is appreciated. Thanks!
Problem Solved, just removed
require_once("../tcpdf/config/lang/eng.php");
from the top of the codes then everything is going well.
I am trying to capture the contents of my php page using output buffering:
<?php
function connect() {
$dbh = mysql_connect ("localhost", "user", "password") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db("PDS", $dbh);
return $dbh;
}
session_start();
if(isset($_SESSION['username'])){
if(isset($_POST['entryId'])){
//do something
$dbh = connect();
$ide = $_POST['entryId'];
$usertab = $_POST['usertable'];
$answertable = $usertab . "Answers";
$entrytable = $usertab . "Entries";
$query = mysql_query("SELECT e.date, q.questionNumber, q.question, q.sectionId, a.answer FROM $answertable a, Questions q, $entrytable e WHERE a.entryId = '$ide' AND a.questionId = q.questionId AND e.entryId = '$ide' ORDER BY q.questionNumber ASC;") or die("Error: " . mysql_error());
if($query){
//set variables
$sectionOne = array();
while($row=mysql_fetch_assoc($query)){
$date = $row['date'];
$sectionOne[] = $row;
}
}else{
//error - sql failed
}
}
?>
<?php
ob_start();
?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script src = "jQuery.js"></script>
<script>
$(document).ready(function(){
$("#export").click(function(e){
//post to html2pdfconverter.php
$("#link").val("<?php echo(ob_get_contents()); ?>"); //THIS DOESN'T WORK
$("#nm").val("Entry Report.pdf");
$("form#sendanswers").submit();
});
});
</script>
<title>Personal Diary System - Entry Report - <?php echo($date); ?></title>
</head>
<body>
<h1>Entry Report - <?php echo($date); ?></h1>
<div id = "buttons">
<form id = "sendanswers" name = "sendanswers" action="html2pdfconverter.php" method="post">
<input type = "hidden" name = "link" id = "link" value = "">
<input type = "hidden" name = "nm" id = "nm" value = "">
<input type = "button" name = "export" id = "export" value = "Export As PDF"/>
</form>
</div>
<h3>Biological Information</h3>
<?php
echo('<p>');
$i = 0;
foreach($sectionOne as &$value){
if($i == 1 || $i == 3){
$image = "assets/urine".$i.".png";
echo("<br/>");
echo($value['question']." <br/> "."<img src = \"$image\"/>");
echo("<br/>");
}else{
echo($value['question'].' : '.$value['answer']);
}
echo("<br/>");
$i++;
}
echo('</p>');
?>
</body>
</html>
<?php
}
$contents = ob_get_contents(); //THIS WORKS
ob_end();
?>
I assign the contents of ob to $contents using ob_get_contents(); This works, and echoing $contents duplicates the html page.
However, in my jQuery, I am trying to assign this to a hidden text field ('link') using:
$("#link").val("<?php echo($contents); ?>");
This doesn't work however..And I have a feeling its because I am accessing $contents too eraly but not too sure...any ideas?
$("#link").val("<?php echo(ob_get_contents()); ?>"); //THIS DOESN'T WORK
at the point you do that ob_get_contents call, you've only output about 10 lines of javascript and html. PHP will NOT reach back in time and magically fill in the rest of the document where you do this ob_get_contents().
You're basically ripping the page out of the laser printer the moment the page starts emerging, while the printer is still printing the bottom half of the page.
I fail to see why you want to embed the contents of your page into an input field. If you want to somehow cache the page's content in an input field, you can just use JS to grab the .innerHTML of $('body').
Well, you have two problems.
The first is what you suspect. You can't access that stuff until later. The second problem which you may not realize is that you will have quoting issues in JavaScript even if you manage to find a way to reorder this and make it work. It's recursive, in a bad way.
What you should do instead is change your $('#export').click handler to do an Ajax call, render the HTML you need to appear in the link on the server in a separate PHP script (no output buffering necessary) and then have your code inject the result of that call into the page the way you're trying to do in your click handler now.