When I try to print the table in the file_html.php it prints the static table with some error(that's a different question). However, when the php tags for the posted data are included in the file with the post variables, the pdf generated displays nothing at all except for the anchor tag which is at the bottom of the page.
Here is the index.php :-
require_once("dompdf/dompdf_config.inc.php");
require_once("dompdf/dompdf_config.custom.inc.php");
spl_autoload_register('DOMPDF_autoload');
function pdf_create($html, $filename, $paper, $orientation, $stream=TRUE)
{
$dompdf = new DOMPDF();
$dompdf->set_paper($paper,$orientation);
$dompdf->load_html_file('http://localhost/pdf/file_html.php');
$dompdf->render();
$dompdf->stream($filename.".pdf");
}
$filename = 'billofsale';
$dompdf = new DOMPDF();
$html = file_get_contents('http://localhost/pdf/file_html.php');
pdf_create($html,$filename,'A4','portrait');
And here is the file_html.php which consists the form with the post variable. Note : when the php with the post is removed the table prints out in the pdf.
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
table, caption, tbody, tfoot, thead, tr, th, td {
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
font: inherit;
}
table {
border-collapse: collapse;
border-spacing: 0;
width:100%;
}
body{
font: normal medium/1.4 sans-serif;
}
th{
text-align: center;
border: 3px solid #ccd;
}
td{
padding: 0.25rem;
text-align: left;
border: 2px solid #ccc;
}
tbody tr:nth-child(odd){
background: #eee;
}
tbody:before, thead:after { display: none; }
</style>
</head>
<body>
<?php
if(isset($_POST['submit'])){
?>
<p>
<table>
<thead><th colspan="2">Purchaser's Information</th></thead>
<tbody>
<tr>
<td colspan="2">Purchaser's Name : <?php echo $_POST['pname']; ?></td>
</tr>
<tr>
<td colspan="2">Purchaser's Address : <?php echo $_POST['padd']; ?></td>
</tr>
<tr>
<td>City/Town : <?php echo $_POST['pcity']; ?> </td>
<td>Province : <?php echo $_POST['ppro']; ?></td>
</tr>
<tr>
<td>Postal Code :<?php echo $_POST['ppcode']; ?></td>
<td>Home Tel No :<?php echo $_POST['ptelno']; ?></td>
</tr>
<tr>
<td>Business Tel :<?php echo $_POST['pbtel']; ?></td>
<td>Email :<?php echo $_POST['pemail']; ?></td>
</tr>
<tr>
<td>Driver License : <?php echo $_POST['pdriverlic']; ?></td>
<td>Expiry Date :<?php echo $_POST['pdriverexp']; ?></td>
</tr>
</tbody>
</table>
</p>
<?php
}
?>
Print
</body>
</html>
You're loading file_html.php using $dompdf->load_html_file('http://localhost/pdf/file_html.php');. This method is like starting a new browser request using the GET method. Any variables set during the current PHP process are not passed through. This means the $_POST array is empty.
There are a few ways of addressing the issue, but since your table relies on data from the $_POST array I'd suggest using output buffering. You can render the content within index.php, capture the output, and feed it to dompdf.
Using your original sample as a starting point ...
<?php
ob_start();
require 'file_html.php'
$html = ob_get_contents();
ob_end_clean();
require_once('dompdf/dompdf_config.inc.php');
$dompdf = new DOMPDF();
$dompdf->set_paper('a4','portrait');
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream('billofsale.pdf');
?>
Now when you POST to index.php the $_POST array will be available to file_html.php.
Related
I have view file on my app there is code array I want to get first object of that array without going in loop.
<?php
$result = array_chunk($products->result_array(), 3);
foreach($result as $products){ ?>
<table style="width:100% style="page-break-after:always;" >
<tr>
<?php
foreach($products as $productArray){
$product = (object) $productArray;
echo '<td>';
?>
<div style="width: 100%; height: 210px; border: 1px solid #dddddd; margin: auto 5px 5px 0; padding: 5px;">
<div class="box-header">
<p class="box-title"><FONT SIZE=12><?php echo $product->product_name; ?></FONT></p>
</div>
<div style="height: 100px; text-align: center;">
<?php echo '<img src="'.'uploads/'. $product->photo.'" class="img-responsive" style="height:100px !important; width: 150px !important" />'; ?>
</div>
<div style="clear: both"></div>
<table class="table table-responsive">
<tr>
<th><FONT SIZE=12>ID</FONT></th>
<td><FONT SIZE=14><?php echo $product->product_id; ?></FONT></td>
</tr>
$result is array of object I'm getting from a form. In below you can clearly see I'm chunking it to 3 more array and looping though individual objects and getting their details to html for example.
<tr>
<th><FONT SIZE=12>ID</FONT></th>
<td><FONT SIZE=14><?php echo $product->product_id; ?></FONT></td>
</tr>
I want to get the first object details let's say want get $product->product_name of first object of result array without going in loop how to achieve that.
here is complete view file code.
<!DOCTYPE html>
<html class="bg-black">
<head>
<meta charset="UTF-8">
<title><?php if(isset($title)) echo $title.' | '; ?> Sales agent management software (SAMS) </title>
<style>
body{
font-size: 9px;
margin: 20px;
}
th,td,p,div,table,h3{margin:0;padding:0}
#page { margin: 20px; }
.header{
border-bottom: 0px solid #dddddd;
text-align: center;
position: fixed; top: 0;
}
.footer { position: fixed; bottom: 0px; text-align: center }
.pagenum:before { content: counter(page); }
</style>
</head>
<body>
<?php
$usd = get_option('lkr_per_usd', 134);
?>
<div class="footer">
Page: <span class="pagenum"></span>, creation time : <?php echo date('l jS \of F Y h:i:s A') ?>, create by: <?php echo user_full_name(singleDbTableRow(loggedInUserData()['user_id'])); ?>, $ Rate : Rs. <?php echo $usd; ?> </div>
<br />
<div class="box-body">
<?php
$usd = get_option('lkr_per_usd', 134);
?>
<?php
$result = array_chunk($products->result_array(), 3);
foreach($result as $products){ ?>
<table style="width:100% style="page-break-after:always;" >
<tr>
<?php
foreach($products as $productArray){
$product = (object) $productArray;
echo '<td>';
?>
<div style="width: 100%; height: 210px; border: 1px solid #dddddd; margin: auto 5px 5px 0; padding: 5px;">
<div class="box-header">
<p class="box-title"><FONT SIZE=12><?php echo $product->product_name; ?></FONT></p>
</div>
<div style="height: 100px; text-align: center;">
<?php echo '<img src="'.'uploads/'. $product->photo.'" class="img-responsive" style="height:100px !important; width: 150px !important" />'; ?>
</div>
<div style="clear: both"></div>
<table class="table table-responsive">
<tr>
<th><FONT SIZE=12>ID</FONT></th>
<td><FONT SIZE=14><?php echo $product->product_id; ?></FONT></td>
</tr>
<tr>
<th><FONT SIZE=12>LKR</FONT></th>
<td><FONT SIZE=14><?php $lkr = get_selling_price($product);
echo number_format(round($lkr, get_option('round_precision')) ); ?></FONT>
</td>
</tr>
<tr>
<th> <FONT SIZE=12>US $</FONT></th>
<td><FONT SIZE=14><?php echo number_format(round(lkr_to_usd($lkr), get_option('round_precision')) ); ?></FONT></td>
</tr>
</table>
<?php $GLOBALS['a']= $product->product_id; ?>
</div>
</td>
<?php } ?>
</tr>
<?php } ?>
</table>
</div><!-- /.box-body -->
</body>
</body>
</html>
ok man as i said u need to change your way of writing codes but about your specific question use this:
$result = array('a', 'b', 'c', 'd', 'e');
reset($array);
$first = current($array);
you can check this:
http://php.net/manual/en/function.reset.php
http://php.net/manual/en/function.current.php
But still about your way of coding. u should soon go to MVC or such ways of programming so u should separate your view and coding logics
like u may have a page like view_profile.php which is going to show user's information. in regular coding u have this:
view_profile.php:
<?php session_start();
// you check sessions to see if the user is logged in and has the right to view this page.
// like:
if ($_SESSIONS['is_user_logged_in']){
$username=$_SESSIONS['username'];
$name=$_SESSIONS['name'];
// ....
}else{
header('location: ./login.php');// if user is not authenticated u redirect to login page
exit();// prevents the rest of codes to be shown and executed
}
?>
<html>
<head>
<title>View <?php echo $name; ?>'s profile</title>
</head>
<body>
<div>Name: <span><?echo $name; ?></span></div>
......
</body>
</html>
But in a better way u can do it like this:
you have files like
'view_profile.htm' // holds the HTML
'view_profile.php // holds the PHP logic
'inc.php' // holds some useful functions that help you in writing PHP logic
view_profile.htm
<html>
<head>
<title>View <?php echo $name; ?>'s profile</title>
</head>
<body>
<div>Name: <span><?echo $name; ?></span></div>
......
</body>
</html>
inc.php
<?php
function ses_start(){
if(!session_id()){
session_start();
}
}
function ses_get($key){
ses_start();
if(!empty($_SESSION[$key])){
return $_SESSION[$key];
}
return NULL;
}
function ses_set($key,$val){
$_SESSION[$key]=$val;
}
function is_user_loggedin(){
ses_start();
if(!empty(ses_get('is_user_loggedin')){
return true;
}
return false;
}
function go($to){
header('location: '.$to);
exit();
}
//and lots of useful functions that help u connect and work with data base.
view_profile.php
<?php
include('inc.php');
if(is_user_loggedin(){
$username=ses_get('username');
$name=ses_get('name');
//...
}else{
go('login.php);
}
include('view_profile.html); // and u call the .htm file that holds the HTML the view file)
?>
this was a simple sample of separating codes logic(php codes) from views(html tags)
and also u may search about MVC Model-View-Controller and try working with simple MVC frameworks.
I'm working on setting up a page to report all past transactions from my organization's Authorize.net account and having some trouble. I've done a bunch of reading of documentation and in the forums and it seems that the XML approach is the encouraged method.
I have little to no experience with XML documents and I'm afraid it's hindering me at the moment.
Out of the documentation I found the following:
<?xml version="1.0" encoding="utf-8"?>
<getTransactionListRequest xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd">
<merchantAuthentication>
<name>1234</name>
<transactionKey>1234abcd</transactionKey>
</merchantAuthentication>
<batchId>1234123</batchId>
</getTransactionListRequest>
I plugged in my id, transaction key, and batch id but I'm really not sure where to go from here. How do I use this to fetch a list of transactions?
Thank you.
If you use the AuthnetXML PHP library I wrote for working with Authorize.Net's APIs this is pretty straight forward. Here is the sample code for the getTransactionListRequest APi call:
<?php
/*************************************************************************************************
Use the Transaction Details XML API to get a list of transaction in a batch
SAMPLE XML FOR API CALL
--------------------------------------------------------------------------------------------------
<?xml version="1.0"?>
<getTransactionListRequest xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd">
<merchantAuthentication>
<name>yourloginid</name>
<transactionKey>yourtransactionkey</transactionKey>
</merchantAuthentication>
<batchId>1221577</batchId>
</getTransactionListRequest>
SAMPLE XML RESPONSE
--------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<getTransactionListResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd">
<messages>
<resultCode>Ok</resultCode>
<message>
<code>I00001</code>
<text>Successful.</text>
</message>
</messages>
<transactions>
<transaction>
<transId>2162566217</transId>
<submitTimeUTC>2011-09-01T16:30:49Z</submitTimeUTC>
<submitTimeLocal>2011-09-01T10:30:49</submitTimeLocal>
<transactionStatus>settledSuccessfully</transactionStatus>
<invoiceNumber>60</invoiceNumber>
<firstName>Matteo</firstName>
<lastName>Bignotti</lastName>
<accountType>MasterCard</accountType>
<accountNumber>XXXX4444</accountNumber>
<settleAmount>1018.88</settleAmount>
</transaction>
</transactions>
</getTransactionListResponse>
*************************************************************************************************/
require('../../config.inc.php');
require('../../AuthnetXML.class.php');
try
{
$xml = new AuthnetXML(AUTHNET_LOGIN, AUTHNET_TRANSKEY, AuthnetXML::USE_DEVELOPMENT_SERVER);
$xml->getTransactionListRequest(array(
'batchId' => '1221577'
));
}
catch (AuthnetXMLException $e)
{
echo $e;
exit;
}
?>
<!DOCTYPE html>
<html>
<html lang="en">
<head>
<title></title>
<style type="text/css">
table
{
border: 1px solid #cccccc;
margin: auto;
border-collapse: collapse;
max-width: 90%;
}
table td
{
padding: 3px 5px;
vertical-align: top;
border-top: 1px solid #cccccc;
}
pre
{
overflow-x: auto; /* Use horizontal scroller if needed; for Firefox 2, not needed in Firefox 3 */
white-space: pre-wrap; /* css-3 */
white-space: -moz-pre-wrap !important; /* Mozilla, since 1999 */
white-space: -pre-wrap; /* Opera 4-6 */
white-space: -o-pre-wrap; /* Opera 7 */ /*
width: 99%; */
word-wrap: break-word; /* Internet Explorer 5.5+ */
}
table th
{
background: #e5e5e5;
color: #666666;
}
h1, h2
{
text-align: center;
}
</style>
</head>
<body>
<h1>
Transaction Detail :: Get Transactions List
</h1>
<h2>
Results
</h2>
<table>
<tr>
<th>Response</th>
<td><?php echo $xml->messages->resultCode; ?></td>
</tr>
<tr>
<th>code</th>
<td><?php echo $xml->messages->message->code; ?></td>
</tr>
<tr>
<th>Successful?</th>
<td><?php echo ($xml->isSuccessful()) ? 'yes' : 'no'; ?></td>
</tr>
<tr>
<th>Error?</th>
<td><?php echo ($xml->isError()) ? 'yes' : 'no'; ?></td>
</tr>
<?php
foreach ($xml->transactions->transaction as $transaction)
{
?>
<tr>
<th>Transaction</th>
<td>
transId: <?php echo $transaction->transId; ?><br>
submitTimeUTC: <?php echo $transaction->submitTimeUTC; ?><br>
submitTimeLocal: <?php echo $transaction->submitTimeLocal; ?><br>
transactionStatus: <?php echo $transaction->transactionStatus; ?><br>
invoiceNumber: <?php echo $transaction->invoiceNumber; ?><br>
firstName: <?php echo $transaction->firstName; ?><br>
accountType: <?php echo $transaction->accountType; ?><br>
accountNumber: <?php echo $transaction->accountNumber; ?><br>
settleAmount: <?php echo $transaction->settleAmount; ?><br>
</td>
</tr>
<?php
}
?>
</table>
<h2>
Raw Input/Output
</h2>
<?php
echo $xml;
?>
</body>
</html>
I'm using CentOS 6.4 with apache installed. I have a single php file called cmsSearch.php. At the top of the file I have PHP that executes fine (queries a Sphinx Search index). But, any php I try to run in the HTML that is below (trying to run a foreach and populate a table with the results of the Sphinx Search) just gets commented out when I view the page in the console (Chrome). Here is the whole cmsSearch.php file:
<?php
require("sphinxapi.php");
if($_POST['keyword'])
{
$keyword = $_POST['keyword'];
$client = new SphinxClient();
$client->SetMatchMode(SPH_MATCH_ANY);
$result = $client->Query($keyword, "staffDirectoryMember");
if(!$result)
{
print "ERROR: " . $client->GetLastError();
}
else
{
var_dump($result["matches"]);
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Sphinx Test</title>
<style>
.container
{
border: solid 1px black;
height: 350px;
width: 700px;
margin: auto;
padding: 5px;
}
.output
{
margin-top:20px;
border: solid 1px red;
height: 200px;
}
</style>
</head>
<body>
<?echo "TEST"; ?>
<div class="container">
<div>
<form method="post" action="cmsSearch.php">
<input type="text" name="keyword">
<input type="submit" value="Search">
</form>
<div class="output">
<? echo "test2"; ?>
<table>
<thead>
<tr>
<th>ID</th>
<th>Weight</th>
<th>ClientId</th>
<th>DomainId</th>
<th>ContentTypeId</th>
</tr>
</thead>
<tbody>
<?
echo "Above for loop";
foreach($result["matches"] as $match)
{
echo "Print from for loop:";
var_dump($match);
?>
<!-- <tr>
<td><?=$match[id]?></td>
<td><?=$match[weight]?></td>
<td><?=$match[attrs][clientid]?></td>
<td><?=$match[attrs][domainid]?></td>
<td><?=$match[attrs][contenttypeid]?></td>
</tr> -->
<?}
echo "After for loop";
?>
</tbody>
</table>
</div>
</div>
</div>
Not sure why the php executes at the top fine (i can echo out and the var dump works), but then any php put in the HTML just shows as comments and doesn't do anything. Anyone have an idea?
Your PHP contained inside the HTML is using short tags which can be turned off in your php.ini file. Take a look at this directive and make sure it is set to true if you want to use them:
short_open_tag true
http://www.php.net/manual/en/ini.core.php#ini.short-open-tag
Try using <?php to start your PHP blocks, not <?... It's the difference between the blocks of code.
Overview
I have some data stored in MySql database related to Products. I am trying to retrieve this data and display it on a page using HTML table.
The PHP and MySql has gone well and all the data is retrieved but it is displayed in a very messy manner.
Here is what I have as a layout:
What I am aiming to achieve is to further divide the results table add more columns rows to make the data more readable
Something like this;
The code: PHP, MySQL & HTML:
<?php
session_start();
include('connect_mysql.php');
$product_name = 'product_name';
$product_qua = 'product_qua';
$product_price = 'product_price';
$product_image = 'product_image';
$product_des = 'product_des';
$sql = mysql_query("SELECT * FROM products");
echo "<table id='display'>";
while($rows = mysql_fetch_array($sql))
{
echo"<br>";
echo"<tr><td>";
echo"$rows[$product_name]<br></td>";
echo"<td><img src=$rows[$product_image] height='200px' width='200px'><br></td>";
echo"<td>Avalible: $rows[$product_qua]<br></td>";
echo"<td>Price: $rows[$product_price]<br></td>";
echo"<td>Description: $rows[$product_des]<br></td>";
echo"</tr>";
}
echo "</table>";
?>
CSS responsible for this part:
#display{
float:left;
border: 5px solid black;
margin-left:100px;
}
just add some padding or a border to the table cells:
table#display td{
border: 1px solid black;
padding:0 8px;
}
Edit: What you could do as well:
<table id='display'>
<?php while($rows = mysql_fetch_array($sql)): ?>
<!-- <br> <- why a break? it's not in the correct spot anyway -->
<tr><td>
<?php echo $rows[$product_name]; ?><br>
</td>
<td> - </td>
<td><img src="<?php echo $rows[$product_image]; ?>" height='200px' width='200px'><br></td>
<td> - </td>
<td>Avalible: <?php echo $rows[$product_qua]; ?><br></td>
<td> - </td>
<td>Price: <?php echo $rows[$product_price]; ?><br></td>
<td> - </td>
<td>Description: <?php echo $rows[$product_des]; ?><br></td>
</tr>
<?php endwhile; ?>
</table>
Tip: I prefer to use the while/endwhile; approach rather than using brackets when displaying data to the user.
First of all don't echo so much HTML using PHP, instead do it like this
<?php
session_start();
include('connect_mysql.php');
$product_name = 'product_name';
$product_qua = 'product_qua';
$product_price = 'product_price';
$product_image = 'product_image';
$product_des = 'product_des';
$sql = mysql_query("SELECT * FROM products"); ?>
<table id='display'>
<?php
while($rows = mysql_fetch_array($sql)) {
?>
<tr><td><?php echo $rows[$product_name]; ?></td>
<!-- And so on-->
<?php
}
?>
</table>
Secondly by seeing your inmage, it seems like you need a border for your table so use
table, td {
border: 1px solid #000000;
}
add the following css to apply border on your table cells:
#display td{ border: 2px solid red; }
and optionally add to your #display { :
border-collapse: collapse;
I need some Help. I'm new to PHP and the last couple of days trying to solve this issue. I am trying to parse data from my database to a styled HTML Table and I am not able to find any tutorials on this. I did do the tutorial for parsing to a table that is created with PHP. I would like to use the tables I did include in this file. If anyone would be so kind to show me how to do this and also explain it I would be very happy.
This is the PHP file I did try to work with. Only one close I could find from tutorials.
<?php
// Make a MySQL Connection
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("tegneserier") or die(mysql_error());
// Get all the data from the "årgang" table
$result = mysql_query("SELECT * FROM årgang")
or die(mysql_error());
echo "<table border='1'>";
echo "<tr> <th>Navn</th> <th>Årgang</th> <th>NR</th> <th>Navn</th> <th>Navn</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr><td>";
echo $row['name'];
echo "</td><td>";
echo $row['age'];
echo "</td><td>";
echo $row['issue'];
echo "</td><td>";
echo $row['Description'];
echo "</td><td>";
echo $row['quality'];
echo "</td></tr>";
}
echo "</table>";
?>
This is the stylesheet I would like to use:
/* ------------------
styling for the tables
------------------ */
body
{
line-height: 1.6em;
}
#hor-zebra
{
font-family: "Lucida Sans Unicode", "Lucida Grande", Sans-Serif;
font-size: 12px;
margin: 60px;
width: 480px;
text-align: left;
border-collapse: collapse;
}
#hor-zebra th
{
font-size: 14px;
font-weight: normal;
padding: 10px 8px;
color: #039;
}
#hor-zebra td
{
padding: 8px;
color: #669;
}
#hor-zebra .odd
{
background: #e8edff;
And this is the HTML file where I would like the data from my database to show:
<!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>DataTable Output</title>
<style type="text/css">
<!--
#import url("style.css");
-->
</style>
</head>
<body>
<?php include("datamodtagelse.php"); ?>
<table id="hor-zebra" summary="Datapass">
<thead>
<tr>
<th scope="col">name</th> //Name off table in DB
<th scope="col">age</th> //Name off table in DB
<th scope="col">issue</th> //Name off table in DB
<th scope="col">Description</th> //Name off table in DB
<th scope="col">quality</th> //Name off table in DB
</tr>
</thead>
<tbody>
<tr class="odd">
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr class="odd">
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr class="odd">
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
</tbody>
</table>
</body>
</html>
After adding the code my PHP do return the result. The only problem is it it not showing my styled tables from my style.css and I also get the error "
Notice: Undefined variable: i in C:\Program Files (x86)\EasyPHP-5.3.9\www\Tables\Datamodtagelse.php on line 25
And under that it returns my output: (This is the php page.)
name age issue Description quality
Anders And & Co. 1949 1 Dette er en beskrivelse af en tegneserie. Very Fine.
When I open my html file it doesn't display anything at all.
I will add my file :
Datamodtagelse.php
<?php
// Make a MySQL Connection
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("tegneserier") or die(mysql_error());
// Get all the data from the "årgang" table
$result = mysql_query("SELECT * FROM årgang")
or die(mysql_error());
echo '<table id="hor-zebra" summary="Datapass">
<thead>
<tr>
<th scope="col">name</th>
<th scope="col">age</th>
<th scope="col">issue</th>
<th scope="col">Description</th>
<th scope="col">quality</th>
</tr>
</thead>
<tbody>';
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
if( $i++ % 2 == 0 ) {
$class = " class='odd'";
} else {
$class = "";
}
// Print out the contents of each row into a table
echo "<tr" . $class . "><td>";
echo $row['name'];
echo "</td><td>";
echo $row['age'];
echo "</td><td>";
echo $row['issue'];
echo "</td><td>";
echo $row['Description'];
echo "</td><td>";
echo $row['quality'];
echo "</td></tr>";
}
echo "</tbody></table>";
?>
Showcomic.html:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>DataTable Output</title>
<style type="text/css">
<!--
#import url("style.css");
-->
</style>
</head>
<body>
<?php include("datamodtagelse.php"); ?>
</body>
</html>
Style.css
body
{
line-height: 1.6em;
}
#hor-zebra
{
font-family: "Lucida Sans Unicode", "Lucida Grande", Sans-Serif;
font-size: 12px;
margin: 60px;
width: 480px;
text-align: left;
border-collapse: collapse;
}
#hor-zebra th
{
font-size: 14px;
font-weight: normal;
padding: 10px 8px;
color: #039;
}
#hor-zebra td
{
padding: 8px;
color: #669;
}
#hor-zebra .odd
{
background: #e8edff;
}
My database name is: tegneserier
My table in the database is: årgang
My attributes in the table is:
id int(11) AUTO_INCREMENT
name varchar(255) utf8_danish_ci
age int(11)
issue int(11)
Description text utf8_danish_ci
When looking at the code I think the problem is the HTML file nor importing the stylesheet and the data from the .php file?
The .php file and the .css file and the .html file is located in the same folder.
Any help is welcome.
And sorry this is probably just a easy beginner mistake. (We all need to start somewhere.)
Try something like this:
<?php
// Make a MySQL Connection
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("tegneserier") or die(mysql_error());
// Get all the data from the "årgang" table
$result = mysql_query("SELECT * FROM årgang")
or die(mysql_error());
echo '<table id="hor-zebra" summary="Datapass">
<thead>
<tr>
<th scope="col">name</th> //Name off table in DB
<th scope="col">age</th> //Name off table in DB
<th scope="col">issue</th> //Name off table in DB
<th scope="col">Description</th> //Name off table in DB
<th scope="col">quality</th> //Name off table in DB
</tr>
</thead>
<tbody>';
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
if( $i % 2 == 0 ) {
$class = " class='odd'";
} else {
$class = "";
}
// Print out the contents of each row into a table
echo "<tr" . $class . "><td>";
echo $row['name'];
echo "</td><td>";
echo $row['age'];
echo "</td><td>";
echo $row['issue'];
echo "</td><td>";
echo $row['Description'];
echo "</td><td>";
echo $row['quality'];
echo "</td></tr>";
}
echo "</tbody></table>";
?>
And in your HTML file:
<!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>DataTable Output</title>
<style type="text/css">
<!--
#import url("style.css");
-->
</style>
</head>
<body>
<?php include("datamodtagelse.php"); ?>
</body>
</html>
you are almost there (if the code you showed us runs).
in your php file, you need to give your table the id "hor-zebra", so that the style will be applied to it and the th's and td's within it.
you also want to add a counter to get the .odd thing right:
var $i = 0;
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr";
if( $i++ % 2 == 0 ) echo(" class='odd'");
echo "><td>";
...
you also might want to set the scope="col" in your th's to match the original table as closely as possible
AND last but not least: Don't forget the elements thead and tbody in your echo-output.
if you do all this, you should see the same table twice in your final html (check the source, ctrl+U)
Remember you can echo your data in div tags too, use
<li></li>
to allow row after row of sql data to get displayed. I mention this because I find the div elements easier to work with than tables.