I'm generating a list (some sort of appointment card for students) with PHP. I want to be able to save this as a PDF.
I've successfully tried some basic layout with PHP and DOMPDF.
When I try to save the following as a PDF it does not work.
<?php
session_start();
// User session check
if(!$_SESSION['email'])
{
header("Location: login.php");//redirect to login page to secure the welcome page without login access.
}
include("php/getCustomerEvents.php");
// get row id = student id
$id = $_GET[id];
// check user language
$lang = $_SESSION['lang'];
switch ($lang)
{
case 'en':
$lang_file = 'lang.en.php';
break;
//case 'fr':
//$lang_file = 'lang.fr.php';
//break;
case 'nl':
$lang_file = 'lang.nl.php';
break;
default:
$lang_file = 'lang.nl.php';
}
include_once 'lang/'.$lang_file;
// Debug code
//print_r($_SESSION);
//echo "----------------------------------------------------------------------->".$_SESSION['lang']." = taal <br>";
//echo "----------------------------------------------------------------------->".$_SESSION['name']." = naam <br>";
//echo "----------------------------------------------------------------------->".$_SESSION['resource']." = id <br>";
//Echo "----------------------------------------------------------------------->".$_SESSION['admin']." = admin <br>";
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<meta content='width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no' name='viewport'>
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="css/bootstrap-select.css">
<style type="text/css">
.bs-example{
margin: 20px;
}
/* Fix alignment issue of label on extra small devices in Bootstrap 3.2 */
.form-horizontal .control-label{
padding-top: 7px;
}
</style>
</head>
<body >
<!-- Main content -->
<section class="content" >
<!-- Your Page Content Here -->
<div class="row">
<div class="col-md-12">
<div class="box">
<div class="box-header">
<h3 class="box-title"><?php echo $lang['LBL_LIST_EVENTS']; ?></h3>
</div><!-- /.box-header -->
<div class="box-body">
<table id="instructors" class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th><?php echo $lang['LBL_LESSON']; ?></th>
<th><?php echo $lang['LBL_LOCATION']; ?></th>
<th><?php echo $lang['LBL_TITLE']; ?></th>
<th><?php echo $lang['LBL_EVENT_START']; ?></th>
<th><?php echo $lang['LBL_EVENT_END']; ?></th>
</tr>
</thead>
<tbody>
<?php customerevents($id); ?><!-- function from getCustomerEvents.php -->
</tbody>
</table>
</div><!-- /.box-body -->
<div class="box-footer">
<button type="submit" class="btn btn-success" ><span class="glyphicon glyphicon-print" aria-hidden="true"></span> PDF </button>
<button type="submit" class="btn btn-success" ><span class="glyphicon glyphicon-envelope" aria-hidden="true"></span> MAIL </button>
</div><!-- /.box-footer -->
</div><!-- /.box -->
</div><!-- /.col -->
</div><!-- /.row -->
</section><!-- /.content -->
<!-- Main Footer -->
<footer class="main-footer">
<!-- To the right -->
<div class="pull-right hidden-xs">
</div>
<!-- Default to the left -->
<strong>Copyright © 2015 biqs it.</strong> All rights reserved.
</footer>
</body>
</html>
The code I use to generate the PDF:
<?php
require_once("../dompdf/dompdf_config.inc.php");
//$content = "../" . $_GET["content"];
//$file_name = $_GET["file_name"];
$dompdf = new DOMPDF();
//$html = file_get_contents($content);
$html = file_get_contents('../pdf_customer_events.php');
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("sample.pdf");
?>
What am i doing wrong? Or is DOMPDF not the right solution?
Printing raw file
To convert a file that does not contain any dependancy, you can get the content and give it to the convertion library.
$html = file_get_contents('pregenerated.html');
This method does not allow to pass variables that can be needed (session/POST...).
Printing advanced raw file
By bufferizing the content, you avoid to send it to the browser, it can then be given to the library.
ob_start();
include('content.php');
$html = ob_get_clean();
This way you can transmit session/post/get like you would normally do, and you can even handle errors correctly. You can also use templating engine for advanced output.
The main problem of ressources
By including the php script, all sub files (scripts, css, iframes, ...) are not included. So for simple pdf that does not include image, you need to get everything in one page.
Quite not maintenable ... but feasable.
Handling ressources easily
So, fortunatly there is some php tools that do interpret the page and can fetch any sub files. The most common is wkhtmltopdf, which is a binary that include Chrome's rendering engine webkit to interpret the page and print the pdf.
And for that, you can give him the content like the previous example, or give an url. You can control the printing (margins, colors ...).
It have many php wrappers to make it easy to use in your code.
Going further
But (there is always a but...) wkhtmltopdf does not use a recent engine of webkit, which can be annoying if like in my company you enjoy slick css3 and javascript canvas features.
What we made was to use headless browser, to render the page as a browser and print it in pdf (which is already what does wkhtmltopdf with great optimisations and features).
The most advanced headless browser is probably PhantomJS. Based on Webkit, it can print in many formats easily.
And it have a PHP wrapper: http://jonnnnyw.github.io/php-phantomjs/
var webPage = require('webpage');
var page = webPage.create();
page.viewportSize = { width: 1920, height: 1080 };
page.open("http://www.google.com", function start(status) {
page.render('google_home.jpeg', {format: 'jpeg', quality: '100'});
phantom.exit();
});
Side note:
In our company we use SlimerJS because it offers better output back in the days we build the service, this is only a concern because our pages were not made to be consistent accross browsers and the conversion messed up some part...
When we developed the service, SlimerJS did not provide full PDF printing support until 0.10 release that was in developement. So we had to compile it ourself with latest xulrunner.
Thanks to #Cyrbil I came to the working code below. Still having a problem with the CSS which causes the PDF to fail. From what I've found it has something to do with the bootstrap.css. Already tried to remove *before and after but still no PDF render. But I probably should post this as a new question.
<?php
session_start();
require_once("dompdf/dompdf_config.inc.php");
// User session check
// if(!$_SESSION['email'])
// {
// header("Location: login.php");//redirect to login page to secure the welcome page without login access.
// }
include("php/getCustomerEvents.php");
// get row id = student id
$id = $_GET["id"];
//check user language
$lang = $_SESSION['lang'];
switch ($lang)
{
case 'en':
$lang_file = 'lang.en.php';
break;
case 'fr':
$lang_file = 'lang.fr.php';
break;
case 'nl':
$lang_file = 'lang.nl.php';
break;
default:
$lang_file = 'lang.nl.php';
}
include_once 'lang/'.$lang_file;
ob_start(); // start output buffer
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<meta content='width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no' name='viewport'>
<!--link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css" /-->
<link href="css/biqs_print_bootstrap.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="css/bootstrap-select.css">
<style type="text/css">
.bs-example{
margin: 20px;
}
/* Fix alignment issue of label on extra small devices in Bootstrap 3.2 */
.form-horizontal .control-label{
padding-top: 7px;
}
tbody:before, tbody:after { display: none; }
</style>
</head>
<body >
<!-- Main content -->
<section class="content" >
<!-- Your Page Content Here -->
<div class="row">
<div class="col-md-12">
<div class="box">
<div class="box-header">
<h3 class="box-title"><?php echo $lang['LBL_LIST_EVENTS']; ?></h3>
</div><!-- /.box-header -->
<div class="box-body">
<table id="instructors" class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th><?php echo $lang['LBL_LESSON']; ?></th>
<th><?php echo $lang['LBL_LOCATION']; ?></th>
<th><?php echo $lang['LBL_TITLE']; ?></th>
<th><?php echo $lang['LBL_EVENT_START']; ?></th>
<th><?php echo $lang['LBL_EVENT_END']; ?></th>
</tr>
</thead>
<tbody>
<?php customerevents($id); ?><!-- function from getCustomerEvents.php -->
</tbody>
</table>
</div><!-- /.box-body -->
<div class="box-footer">
</div><!-- /.box-footer -->
</div><!-- /.box -->
</div><!-- /.col -->
</div><!-- /.row -->
</section><!-- /.content -->
<!-- Main Footer -->
<footer class="main-footer">
<!-- To the right -->
<div class="pull-right hidden-xs">
</div>
<!-- Default to the left -->
<strong>Copyright © 2015 biqs it.</strong> All rights reserved.
</footer>
</body>
</html>
<?php
$html = ob_get_contents();
ob_end_clean();
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("sample.pdf");
echo $html;
?>
Related
My error or success message is not in the right place, so I want to know how to get my message in the right place.
I have been searching the internet. I cannot find the right solution.
Here a photo what I mean.
<!DOCTYPE html>
<?php include('import_excel.php') ?>
<?php include('functions.php') ?>
<html lang="en" class=""><head>
<meta name="ac:base" content="/C:/xampp/htdocs/Angular-421-N-WORK-APP">
<meta charset="utf-8">
<title>N-WORK-USER | opslag-Sorting</title>
<meta name="description" content="app, web app, responsive, responsive layout, admin, admin panel, admin dashboard, flat, flat ui, ui kit, AngularJS, ui route, charts, widgets, components">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<link rel="stylesheet" href="../libs/assets/animate.css/animate.css" type="text/css">
<link rel="stylesheet" href="../libs/assets/font-awesome/css/font-awesome.min.css" type="text/css">
<link rel="stylesheet" href="../libs/assets/simple-line-icons/css/simple-line-icons.css" type="text/css">
<link rel="stylesheet" href="../libs/jquery/bootstrap/dist/css/bootstrap.css" type="text/css">
<link rel="stylesheet" href="css/app.min.css" type="text/css">
<link rel="stylesheet" type="text/css" href="css/component.css">
<script>
(function(e,t,n){
var r=e.querySelectorAll("html")[0];r.className=r.className.replace(/(^|\s)no-js(\s|$)/,"$1js$2");
}
(document,window,0)
);
</script>
</head>
<body>
<!-- content -->
<div id="content" class="app-content" role="main">
<div class="app-content-body ">
<div ng-controller="FullcalendarCtrl">
<!-- main header -->
<div class="wrapper-md bg-yello b-b">
<h1 class="m-n font-thin h3">Lijst werkkracht TL GL Database</h1>
</div>
<!-- / main header -->
<div class="wrapper-md" ng-controller="FormDemoCtrl">
<div class="panel panel-defaultgrd">
<div class="panel-headinggrd font-bold text-blue">
Uitslag
</div>
<div class="panel-body">
<div class="hbox hbox-auto-xs hbox-auto-sm">
<div class="container">
<div class="row">
<div class="col-md-12 control-label">
<button class="btn btn-info">Database invoegen</button>
<a class="btn btnx btn-orange" id="download-btn" target="_new"><i class="fa fa-download"></i> Export to Excel</a>
<form method="post" action="import_excel.php" enctype="multipart/form-data">
<div class="form-groupp" style="">
<input type="file" name="file-7" id="file-7" class="inputfile inputfile-6" data-multiple-caption="{count} files selected" multiple=""><label for="file-7"><span></span> <strong><svg xmlns="http://www.w3.org/2000/svg" width="20" height="17" viewBox="0 0 20 17"><path d="M10 0l-5.2 4.9h3.3v5.1h3.8v-5.1h3.3l-5.2-4.9zm9.3 11.5l-3.2-2.1h-2l3.4 2.6h-3.5c-.1 0-.2.1-.2.1l-.8 2.3h-6l-.8-2.2c-.1-.1-.1-.2-.2-.2h-3.6l3.4-2.6h-2l-3.2 2.1c-.4.3-.7 1-.6 1.5l.6 3.1c.1.5.7.9 1.2.9h16.3c.6 0 1.1-.4 1.3-.9l.6-3.1c.1-.5-.2-1.2-.7-1.5z"></path></svg> Import Excel..</strong></label>
</div>
<a class="btnl"><button class="btn btn-info" style="">Upload</button></a>
</form>
<div class="table-responsive">
<table id="mytable" class="table table-bordred table-striped">
<thead>
<tr>
<th>#</th>
<th>MBU</th>
<th>Zone</th>
<th>Omschrijving</th>
<th>TL</th>
<th>GL</th>
<th>Stand in lijn</th>
<th>Station</th>
<th>MBU nr</th>
<th>WO stap</th>
<th>LOG stap</th>
<th>Q stap</th>
<th>RA stand</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<?php
$fetchdata=new DB_con();
$sql=$fetchdata->fetchdata();
$cnt=1;
while($row=mysqli_fetch_array($sql))
{
?>
<tr>
<td><?php echo htmlentities($cnt);?></td>
<td><?php echo htmlentities($row['MBU']);?></td>
<td><?php echo htmlentities($row['zone']);?></td>
<td><?php echo htmlentities($row['omschr']);?></td>
<td><?php echo htmlentities($row['tl']);?></td>
<td><?php echo htmlentities($row['gl']);?></td>
<td><?php echo htmlentities($row['stand_in_lijn']);?></td>
<td><?php echo htmlentities($row['station']);?></td>
<td><?php echo htmlentities($row['MBU_nr']);?></td>
<td><?php echo htmlentities($row['WO_stap']);?></td>
<td><?php echo htmlentities($row['LOG_stap']);?></td>
<td><?php echo htmlentities($row['Q_stap']);?></td>
<td><?php echo htmlentities($row['RA_stand']);?></td>
<td><button class="btn btn-info btn-xs"><span class="glyphicon glyphicon-pencil"></span></button></td>
<td><button class="btn btn-danger btn-xs" onclick="return confirm('Wil je bestand echt verwijderen?');"><span class="glyphicon glyphicon-trash"></span></button></td>
</tr>
<?php
// for serial number increment
$cnt++;
}
?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- / main -->
</div>
</div>
<!-- /content -->
<!-- footer -->
<footer id="footer" class="app-footer" role="footer">
<div class="wrapper b-t bg-light">
<span class="pull-right">2.3.0 <i class="fa fa-long-arrow-up"></i></span>
© 2018 Copyright.
</div>
</footer>
<!-- / footer -->
<script src="js/app.min.js"></script>
</body>
</html>
my other code import_excel.php
<?php
/**
* RaeCreated by Homensdigiworld.
* Admin: Stefaan
* Date: 26-03-2019
*/
include_once "app_opslag_database_tl_gl.php";
use Box\Spout\Reader\ReaderFactory;
use Box\Spout\Common\Type;
require_once ('connect.php');
require_once ('Spout/Autoloader/autoload.php');
if(!empty($_FILES['file-7']['name']))
{
// Get File extension eg. 'xlsx' to check file is excel sheet
$pathinfo = pathinfo($_FILES['file-7']['name']);
// check file has extension xlsx, xls and also check
// file is not empty
if (($pathinfo['extension'] == 'xlsx' || $pathinfo['extension'] == 'xls')
&& $_FILES['file-7']['size'] > 0 )
{
$file = $_FILES['file-7']['tmp_name'];
// Read excel file by using ReadFactory object.
$reader = ReaderFactory::create(Type::XLSX);
// Open file
$reader->open($file);
$count = 0;
// Number of sheet in excel file
foreach ($reader->getSheetIterator() as $sheet)
{
// Number of Rows in Excel sheet
foreach ($sheet->getRowIterator() as $row)
{
// It reads data after header. In the my excel sheet,
// header is in the first row.
if ($count > 0) {
// Data of excel sheet
$MBU = $row[1];
$zone = $row[2];
$omschr = $row[3];
$tl = $row[4];
$gl = $row[5];
$stand_in_lijn = $row[6];
$station = $row[7];
$MBU_nr = $row[8];
$WO_stap = $row[9];
$LOG_stap = $row[10];
$Q_stap = $row[11];
$RA_stand = $row[12];
//Here, You can insert data into database.
$qry = "INSERT INTO `users`(`MBU`, `zone`, `omschr`, `tl`, `gl`, `stand_in_lijn`, `station`, `MBU_nr`, `WO_stap`, `LOG_stap`, `Q_stap`, `RA_stand`) VALUES ('$MBU','$zone','$omschr','$tl','$gl','$stand_in_lijn','$station','$MBU_nr','$WO_stap','$LOG_stap','$Q_stap','$RA_stand')";
$res = mysqli_query($con,$qry);
}
$count++;
}
}
if($res)
{
echo "Your file Uploaded Successfull";
}
else
{
echo "Your file Uploaded Failed";
}
// Close excel file
$reader->close();
}
else
{
echo "Please Choose only Excel file";
}
}
else
{
echo "File is Empty"."<br>";
echo "Please Choose Excel file";
}
?>
I have that message : Undefined variable: _SESSION in C:\xampp\htdocs\import-excel_08\index.php on line 29
You are including the upload file at the top of your main page file.
<?php include('import_excel.php') ?>
It doesn't need to be included as it does nothing on that page, your form submits to that file.
Remove it from the top of that page and the message should go away.
I have been working on this for so long now, I feel this is my last option, I have a slideshow on a html web page and they can essentially flick through all the way through to the end by scrolling. Essentially I want users to be able to go back to their dashboard and when they click to go back into the slideshow, to re-load the page they were on...
At the top of the page I have this:
<?php
if (!isset($_COOKIE['PageNo'])){
setcookie("PageNo", 1, time() + (86400 * 30), "/"); // 86400 = 1 day, so set the cookie for a month long period
}
?>
I am essentially saying set the cookie at 1 to begin with (the first page in the slideshow = 1, then above the next section I have the below:
<?php
if($_COOKIE['PageNo'] >= 2)
{
?>
<script>
window.location.replace("<?php echo "istudy_university.php#slide=".$_COOKIE['PageNo']; ?> ");
</script>
<?php
}
else
{
?>
<script>
window.location.replace("istudy_university.php#slide=1");
</script>
<?php
}
?>
Above each slide I have the below and just changing the slide=number:
<?php $_COOKIE['PageNo'] = 3; ?>
So I'm saying, if the cookie is more than or equal to 2, then go to the page no'x' else go to page 1. However all it keeps doing is bringing me back constantly to Page 1. Please help!! Am I setting the cookie wrong?
UPDATE: After sliding through some of the slides, the cookie should have changed to 5, however it's still 1?
UPDATED code showing the HTML for page:
<?php
session_start();
require "includes/dbh.inc.php";
?>
<?php
echo $_COOKIE['PageNo'];
//$_COOKIE['PageNo'] = 5; //Commented out, for testing
if (!isset($_COOKIE['PageNo'])){
setcookie("PageNo", 1, time() + (86400 * 30), "/"); // 86400 = 1 day, so set the cookie for a month long period
}
?>
<!doctype html>
<html lang="en" prefix="og: http://ogp.me/ns#">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>iStudy University | Stress & Anxiety</title>
<link rel="stylesheet" type='text/css' media='all' href="webslides.css" rel="stylesheet">
<body>
<script src="static/js/webslides.js"></script>
<!-- BEGINNING OF SLIDES -->
<?php
if($_COOKIE['PageNo'] >= 2)
{
?>
<script>
window.location.replace("<?php echo "istudy_university.php#slide=".$_COOKIE['PageNo']; ?> ");
</script>
<?php
}
else
{
?>
<script>
window.location.replace("istudy_university.php#slide=1");
</script>
<?php
}
?>
<main role="main">
<article id="webslides">
<!-- SLIDE 1 -->
<section class="bg-apple aligncenter">
<span class="background dark" style="background-image: url('istudy_slides_images/abstract.jpg')"/></span>
<div class="wrap" id="slide=1">
<h1 class="text-landing">Stress & Anxiety</h1>
<br>
<br>
<br>
<hr id="hor-rule">
<h1 class="slides-logo">iStudy University <i class="fas fa-graduation-cap"></i></h1>
<h2 class="slogan">Designed by Students <br><span class="iv">IV</span> <br>Students</h2><br><br>
</div>
</section>
<!-- SLIDE 2 -->
<?php $_COOKIE['PageNo'] = 2; ?>
<section class="aligncenter">
<span class="background light" style="background-image: url('istudy_slides_images/mountain.jpg')"/></span>
<div class="wrap" id="slide=2">
<blockquote class="quote">
<p class>"No one can create negativity or stress within you. Only you can do that by virtue of how you process your world"</p>
<p><cite>Wayne Dyer</cite></p>
</blockquote>
</div>
</section>
<!-- SLIDE 3 -->
<?php $_COOKIE['PageNo'] = 3; ?>
<section class="bg-slide3">
<div class="wrap size-80" id="slide=3">
<h3 class="title stessAnx"><strong> Stress & Anxiety</strong></h3><br>
<p>Stress and anxiety are common experiences of students in higher education.<br>This module will introduce you to evidence based techniques for managing stress and anxiety based upon cognitive behavioural therapy (CBT).</p>
</section>
</div>
Include jQuery library in both dashboard and slides page.
Include Scrollify library in the slides page
In the dashboard page, add an id to the slide page navigation link, like:
<a id="home" href="#">Slides</a>
Modify the sections in your slides page as follows:
Add a unique ID and a common class name for all the sections.
Example:
<section class="slides aligncenter" id="b">
<span class="background light" style="background-image: url('istudy_slides_images/mountain.jpg')" /></span>
<div class="wrap" id="slide=2">
<blockquote class="quote">
<p class>"No one can create negativity or stress within you. Only you can do that by virtue of how you process your world"</p>
<p><cite>Wayne Dyer</cite></p>
</blockquote>
</div>
</section>
<!-- SLIDE 3 -->
<section class="slides bg-slide3" id="c">
<div class="wrap size-80" id="slide=3">
<h3 class="title stessAnx"><strong> Stress & Anxiety</strong></h3><br>
<p>Stress and anxiety are common experiences of students in higher education.<br>This module will introduce you to evidence based techniques for managing stress and anxiety based upon cognitive behavioural therapy (CBT).</p>
</section>
** The id for each section is given as 'b', 'c' etc.
** both section contains a common class name - 'slides'.
In the slides page, add the following JavaScript code in the footer.
$.scrollify({
section: ".slides", //Rename the class name with the common class name that you gave for the sections
after: function() {
localStorage.setItem('currentPage', $.scrollify.current()[0].id)
}
});
In the dashboard page, add the below JavaScript code to the footer at the end:
<script>
if(localStorage.getItem('currentPage') != ''){
var newUrl = 'scroll.html#'+localStorage.getItem('currentPage');
$("#home").attr("href", newUrl);
}
</script>
I was following this tutorial to use multiple languages on my site, but i'm trying to switch language after POST, but actually the language switch happens when clicked on POST button twice.
This code is called out in the beginning of page.
<?php
if (!isset($_SESSION['lang'])){
$_SESSION['lang'] = 'ENG';
require "lang/ENG.php";
}
else {
$language = (isset($_SESSION['lang'])) ? $_SESSION['lang'] : "ENG";
switch($language) {
case "EST":
require "lang/EST.php";
break;
case "ENG":
require "lang/ENG.php";
break;
default:
require "lang/ENG.php";
}
}
?>
This code is called out when form is submitted:
if(isset($_POST['selectlang'])){
$lang = $_POST['country'];
//Change language??
}
If i click POST button twice, the language is switched, but not on the first try. What do i need to change?
Edit #2: Adding modified HTML now based on answers:
<?php
include('connection.php');
?>
<?php
if (!isset($_SESSION['lang'])){
$_SESSION['lang'] = 'ENG';
require "lang/ENG.php";
}
else {
$language = (isset($_SESSION['lang'])) ? $_SESSION['lang'] : "ENG";
switch($language) {
case "EST":
require "lang/EST.php";
break;
case "ENG":
require "lang/ENG.php";
break;
default:
require "lang/ENG.php";
}
}
?>
<!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">
<meta name="description" content="">
<meta name="author" content="">
<title><?php echo $lang['reklamatsioon_title'];?></title>
<!-- Bootstrap Core CSS -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- Custom CSS -->
<link href="css/logo-nav.css" rel="stylesheet">
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<!-- Navigation -->
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="reklamatsioon.php">
<img src="logo.png" alt="">
</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li>
<?php echo $lang['link1'];?>
</li>
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
<!-- /.container -->
</nav>
<!-- Page Content -->
<div class="container">
<div class="row">
<form action="" method="post" class="form-horizontal" role="form">
<div class="form-group">
<label for="selector" class="col-sm-3 col-lg-3 control-label">Pick language</label>
<div class="col-sm-9 col-lg-9">
<span id="selector" class="input-group-btn">
<select id="country" name="riik" class="form-control">
<option <?php if($_POST['riik'] == "EST"){ echo "selected='selected'"; }?> value="EST">Eesti</option>
<option <?php if($_POST['riik'] == "ENG"){ echo "selected='selected'"; }?> value="ENG">English</option>
<option <?php if($_POST['riik'] == "FIN"){ echo "selected='selected'"; }?> value="FIN">Suomi</option>
<option <?php if($_POST['riik'] == "SWE"){ echo "selected='selected'"; }?> value="SWE">Rootsi</option>
<option <?php if($_POST['riik'] == "NOR"){ echo "selected='selected'"; }?> value="NOR">Norra</option>
</select>
</span>
<span class="input-group-btn">
<button class="btn btn-primary" name="selectlang" type="submit">Choose language!</button>
</span>
</div>
</div>
</form>
</div>
<?php
if(isset($_POST['selectlang'])){
$lang = $_POST['riik'];
$_SESSION['lang'] = $lang;
//Show code form and terms and conditions url!
echo "<div class='row'>";
echo "<form action='' method='post' class='form-horizontal' role='form'>";
echo "<div class='form-group'>";
echo "<label for='code' class='col-sm-3 control-label'>"."Code"."</label>";
echo "<div class='col-sm-6'>";
echo "<input type='number' id='code' name='code' placeholder='Code' class='form-control' autofocus>";
echo "<span class='help-block'></span>";
echo "</div>";
echo "</div>";
echo "<div class='form-group'>";
echo "<div class='col-sm-6 col-sm-offset-3'>";
echo "<button type='submit' class='btn btn-primary btn-block' name='gobutton'>Continue to form</button>";
echo "</div>";
echo "</div>";
echo "</form>";
echo "</div>";
}
if(isset($_POST["gobutton"])){
$codeVal = $_POST['code'];
$brokenCode = false;
if(strlen($codeVal) < 9){
$codeQuery = mysql_query("SELECT clientNumber FROM client WHERE clientNumber = '$codeVal'",$connection) or die(mysql_error());
if (mysql_num_rows($codeQuery) == 1){
header('location: ankeet.php?code='.$codeVal);
}
else {
$brokenCode = true;
}
}
else {
$brokenCode = true;
}
if ($brokenCode){
echo "<div class='row'>";
echo "<form action='' method='post' class='form-horizontal' role='form'>";
echo "<div class='form-group'>";
echo "<label for='code' class='col-sm-3 control-label'>"."Code"."</label>";
echo "<div class='col-sm-6'>";
echo "<input type='number' id='code' name='code' placeholder='Code' class='form-control' autofocus value='".$codeVal."'>";
echo "<span class='help-block'></span>";
echo "</div>";
echo "</div>";
echo "<div class='alert alert-danger col-sm-6 col-sm-offset-3'>Such code does not exist!</div>";
echo "<div class='form-group'>";
echo "<div class='col-sm-6 col-sm-offset-3'>";
echo "<button type='submit' class='btn btn-primary btn-block' name='gobutton'>Continue to form</button>";
echo "</div>";
echo "</div>";
echo "</form>";
echo "</div>";
}
}
?>
</div>
<!-- /.container -->
<!-- jQuery -->
<script src="js/jquery.js"></script>
<!-- Bootstrap Core JavaScript -->
<script src="js/bootstrap.min.js"></script>
</body>
</html>
First you need:
if (isset($_POST['country'])) {
$_SESSION['lang'] = $_POST['country'];
}
So the language can be switched.
Then you need to load your settings.
$language = (isset($_SESSION['lang'])) ? $_SESSION['lang'] : "ENG";
switch($language) {
case "DE":
require "lang/de.php";
break;
case "ENG":
default:
require "lang/eng.php";
}
That way nothing, that you dont want can happen
There are several other things you could optimize but for now that should solve your problem.
You should NEVER require or include a file based on a variable that can be set by the website user!
Edit:
I have seen your posted code now: It can not work because you first include your file and switch the session value later on. At that time the file has already been loaded. You need to do things in the correct order, just think of it like that:
1) initialize request
2) make configurations
3) output html code
Edit again:
There are so many bad practices in your code, that i actually dont know where to start.
1) Dont include user input unfiltered in SQL queries, that way you can be attacked by anyone with minimal knowledge. And people will try - believe me. You have to escape values, that are used in sql queries. It would be much better to use parameter binding, please google "php mysql pdo parameter binding"
2) When rendering your options, you should consider using a loop to avoid repeating yourself, please google "DRY dont repeat yourself"
3) you can not send a http header after you printed output already. That does not work due to the nature of the http protocol. First the headers are sent to the browser, followed by the content, basically it looks like:
HeaderLineOne: HeaderValue
HeaderLineTwo: HeaderValue
Content
3) You should really handle the request first and output your code later on. So put your php logic on top of the file (handle input, configure etc) and output your response at the end. That way you can react properly and dont mix up your view with your logic. Basically like:
<?php
// bootstrap application
require_once "connection.php";
require_once "other_include.php";
// configure
$language = "ENG";
if (isset($_SESSION['language'])) {
$language = $_SESSION['language'];
}
// allow user override for language
if (isset($_POST['country'])) {
$language = $_POST['country'];
// make change persistent
$_SESSION['language'] = $language;
}
// include translation labels
switch($language) {
case "DE":
require "lang/de.php";
break;
case "ENG":
default:
require "lang/eng.php";
}
// handle user request
if (isset($_POST['someValue'])) {
// handle user input, make database calls etc
// this is where your logic goes
}
// finally render response
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title><?= $lang['title'] ?></title>
</head>
<body>
<h1><?= $lang['title'] ?>
</body>
</html>
so this is my code
<body>
<section id="container" >
<?php require 'inc/header.php';?>
<section id="main-content">
<section class="wrapper">
<div class="row">
<div class="row mt">
<div class="col-md-12">
<div class="content-panel">
<!-- 1ere ETAPE -->
<h1 style="text-align:center ;"> Rénitialisation </h1>
<div class="etape1">
<?php
require('inc/reni/fpdf.php');
$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',10);
$pdf->Ln();
$pdf->Ln();
$pdf->SetFont('times','B',10);
$pdf->Cell(25,7,"nom");
$pdf->Cell(30,7,"prenom");
$pdf->Ln();
$pdf->Cell(450,7,"----------------------------------------------------------------------------------------------------------------------------------------------------------------------");
$pdf->Ln();
include ('inc/reni/db.php');
$sql="SELECT * FROM table1";
$result = $bdd->query($sql);
while($rows=$result->fetch())
{
$nom = $rows[0];
$prenom = $rows[1];
$pdf->Cell(25,7,$nom);
$pdf->Cell(30,7,$prenom);
$pdf->Ln();
}
$pdf->Output();?>
</div>
</div>
</div>
</div><!-- /col-md-12 -->
</div><!-- /row -->
</section><! --/wrapper -->
</section><!-- /MAIN CONTENT -->
</section>
<?php require 'inc/script.php';?>
</body>
The error is : Fatal error: Uncaught Exception: FPDF error: Some data has already been output, can't send PDF file.
i try everything like add ob_end_clean(); ob_start ();ob_end_flush();
and when i do this my page is empty the Pdf does not display
What you try to use the result it's an octet stream which sends the PDF file to your Browser to download the file. In this case you can't have an output before. Create a single file and output that PDF file without writing anything before. Then you can download the file.
That is what the error message tells you. Don't output anything before the output of the PDF file.
You're already outputting something.
Usually there's an echo somewhere, or you have php code like this:
<HTML>
<?php // do my pdf thing ?>
If anything is in front of your pdf output, wether it be a space, some html, an echo, a print, you will get this error.
Even a space after a php closing tag of a pdf that was included can cause this.
<?php
class PDFWriter() {
// some code
}
?> <-- space here
<?php include('pdfwriter.php');
That's why you should omit the closing tags on pure php files so that doesn't happen.
In your case you're adding all these divs
<body>
<section id="container" >
<?php require 'inc/header.php';?>
<section id="main-content">
<section class="wrapper">
<div class="row">
<div class="row mt">
<div class="col-md-12">
<div class="content-panel">
<!-- 1ere ETAPE -->
<h1 style="text-align:center ;"> Rénitialisation </h1>
<div class="etape1">
And that violates the no output sending requirement.
I am using Bootstrap but under the roots.io wordpress template using a 'wrapperless theme'
i am trying to achieve this http://roots.io/ - where there are sections of colour but the page content itself isn't full width.
the answer I have been given is to make the container class 100% - but this just makes all the content full width.
Im really stuck and ive been trying to figure this out for hours. - I know that sounds noobish and it is, but I can't get past this point.
all the page templates take their its style from base.php, code here
<?php get_template_part('templates/head'); ?>
<body <?php body_class(); ?>>
<!--[if lt IE 8]><div class="alert alert-warning"><?php _e('You are using an <strong>outdated</strong> browser. Please upgrade your browser to improve your experience.', 'roots'); ?></div><![endif]-->
<?php
do_action('get_header');
// Use Bootstrap's navbar if enabled in config.php
if (current_theme_supports('bootstrap-top-navbar')) {
get_template_part('templates/header-top-navbar');
} else {
get_template_part('templates/header');
}
?>
<div class="wrap container" role="document">
<div class="content row">
<div class="main <?php echo roots_main_class(); ?>" role="main">
<?php include roots_template_path(); ?>
</div><!-- /.main -->
<?php if (roots_display_sidebar()) : ?>
<aside class="sidebar <?php echo roots_sidebar_class(); ?>" role="complementary">
<?php include roots_sidebar_path(); ?>
</aside><!-- /.sidebar -->
<?php endif; ?>
</div><!-- /.content -->
</div><!-- /.wrap -->
<?php get_template_part('templates/footer'); ?>
</body>
</html>
so Im just not sure how to get past it in any of the page templates
About the first part of your question: "100% width colored parts".
Cause Bootstrap use the box-sizing: border-box model every html-element gets 100% width of its parent by default. So wrap your .container div's in other element (div, header, etc). This wrapper is a direct child of the body and gets 100% width. See: http://bootply.com/87196
<header role="banner" class="banner">
<div class="container" style="background-color:red;">
Header example
</div>
</header>
<div style="background-color:blue;">Elements get 100% width by default</div>
The second part about your page templates. The code you show use the get_template_part(). This function is a core function of WordPress. See http://codex.wordpress.org/Function_Reference/get_template_part#Using_loop.php_in_child_themes. You will find where your templates should be located.
But i think read http://roots.io/roots-101/#theme-templates first.
Thank you, I had a few solutions offered to me yesterday also, in case anyone else looks at this.
my own was simply to remove the .container class from the base.php file. - this just stopped the content of every page being constrained in a container by default. - this way I was able to add sections to the page at full browser width, and add .container inside them to constrain the actual content.
lines 16 and 17 of original base.php
<div class="wrap <?php if ( ! is_front_page() ): ?>container<?php endif; ?>" role="document">
<div class="content <?php if ( ! is_front_page() ): ?>row<?php endif; ?>">
In app.less
.home .main {
padding: 0;
}
Then add your sections like so:
<section class="semantic-section-class">
<div class="container">
<!-- your content -->
</div>
</section>