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>
Related
I'm trying to get a box around 2 different fields of input the script pings a game server and brings back information but pings 2 servers i want them to be put inside a box instead of just laying on the website messy
<?php
// Include the main class file
require '../GameQ.php';
// Define your servers,
// see list.php for all supported games and identifiers.
$servers = array(
array(
'id' => 'BF4',
'type' => 'bf4',
'host' => '192.223.29.138:47200',
),
array(
'id' => 'GMOD',
'type' => 'gmod',
'host' => '31.186.251.28:27015',
)
);
// Call the class, and add your servers.
$gq = new GameQ();
$gq->addServers($servers);
// You can optionally specify some settings
$gq->setOption('timeout', 4); // Seconds
// You can optionally specify some output filters,
// these will be applied to the results obtained.
$gq->setFilter('normalise');
// Send requests, and parse the data
$results = $gq->requestData();
// Some functions to print the results
function print_results($results) {
foreach ($results as $id => $data) {
printf("<h2>%s</h2>\n", $id);
print_table($data);
}
}
function print_table($data) {
$gqs = array('gq_online', 'gq_address', 'gq_port', 'gq_prot', 'gq_type');
if (!$data['gq_online']) {
printf("<p>The server did not respond within the specified time.</p>\n");
return;
}
print("<table><thead><tr><td>Variable</td><td>Value</td></tr></thead><tbody>\n");
foreach ($data as $key => $val) {
if (is_array($val)) continue;
$cls = empty($cls) ? ' class="uneven"' : '';
if (substr($key, 0, 3) == 'gq_') {
$kcls = (in_array($key, $gqs)) ? 'always' : 'normalise';
$key = sprintf("<span class=\"key-%s\">%s</span>", $kcls, $key);
}
printf("<tr%s><td>%s</td><td>%s</td></tr>\n", $cls, $key, $val);
}
print("</tbody></table>\n");
}
?>
<!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>Game List</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" href="style.css" />
</style>
</head>
<body>
<h1>Game List</h1>
<div id="chat">
Server List. <br/>
Click here for a list of supported games.
</div>
<?php
print_results($results);
?>
</body>
</html>
the css
body {
background-image: url("bg.jpg");
}
table td {
color: #cccccc; // OR WHATEVER
}
h1 {
color: #00ff00;
}
#chat {
color: #00ff00;
}
Add some CSS to change the required font color. Text doesn't come in as black, that's just the default font color for browsers.
table td {
color: #123456; // OR WHATEVER
}
I am trying to read and display the content of the title (contained in a h1 tag) from many HTML files. These files are all in the same folder.
This is what the html files look like :
<!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01//EN'>
<html>
<head>
<title>A title</title>
<style type='text/css'>
... Styles here ...
</style>
</head>
<body>
<h1>Être aidant</h1>
<p>En général, les aidants doivent équilibrer...</p>
... more tags ...
</body>
I have tried to display the content from the H1 tag with this PHP script :
<?php
foreach (glob("test/*.html") as $file) {
$file_handle = fopen($file, "r");
$doc = new DOMDocument();
$doc->loadHTMLfile($file);
$title = $doc->getElementsByTagName('h1');
if ( $title && 0<$title->length ) {
$title = $title->item(0);
$content = $doc->savehtml($title);
echo $content;
}
fclose($file_handle);
}
?>
But the output contains wrong characters. For the example file, the output is :
Être aidant
How can I achieve this output?
Être aidant
You should state a charset in the <head> of your HTML document.
<meta charset="utf-8">
you need to use utf-8 encoding
change echo $content to echo utf8_encode($content);
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 />
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.
I'm trying to redirect this PHP page to another page as soon as I get successful update in the database .. but I get a warning from PHP and nothing happens .. Below is the code .. Where did I go wrong ?
<html> <head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<link rel="stylesheet" type="text/css" href="css/madscore.css">
</head>
<body>
<?php
require('../database/connect.php');
?>
<?php
$id = $_POST["id"];
$value = $_POST["score"];
database_connect();
$query = "update people set Score= Score +".$value." WHERE ID ='".$id."'";
$result = $connection->query($query);
if($result)
{
?>
<?php
#header("Location: http://www.europe-zone.com/");
exit();
}
?>
</body>
</html>
You should send headers before your HTML page. Put your redirect code just before
<html> <head>
Regarding to this:
Remember that header() must be called before any actual output is
sent, either by normal HTML tags, blank lines in a file, or from PHP.
It is a very common error to read code with include, or require,
functions, or another file access function, and have spaces or empty
lines that are output before header() is called. The same problem
exists when using a single PHP/HTML file.
<?php
require('../database/connect.php');
$id = $_POST["id"];
$value = $_POST["score"];
database_connect();
$query = "update people set Score= Score +".$value." WHERE ID ='".$id."'";
$result = $connection->query($query);
if($result)
{
?>
<?php
#header("Location: http://www.europe-zone.com/");
exit();
}
?>
<html> <head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<link rel="stylesheet" type="text/css" href="css/madscore.css">
</head>
<body>
</body>
</html>