I have finish parsing json to html table, and now I am going to get my html table generate to pdf using TCPDF. Everything is working on the local but when I apply it on WordPress, it seems that the WordPress didn't read the TCPDF file inside the theme directory.
How do I make my file to be read by WordPress?
Here's my complete code:
<?php
ob_start();
if (!isset($_POST['generate_pdf']))
{
$askPriceBeds = '{"status":"success","postcode":"W14 9JH","postcode_type":"full","url":"https://propertydata.co.uk/draw?input=W14+9JH","bedrooms":2,"data":{"points_analysed":20,"radius":"0.09","average":657495,"70pc_range":[ 575000, 725000 ],"80pc_range":[ 550000, 875000 ],"90pc_range":[ 550000, 925000 ],"100pc_range":[ 525000, 950000 ],"raw_data":[ {"price":650000,"lat":"51.48887000","lng":"-0.20776000","bedrooms":2,"type":"flat","distance":"0.00" }, {"price":575000,"lat":"51.48884800","lng":"-0.20701200","bedrooms":2,"type":"flat","distance":"0.03" }, {"price":615000,"lat":"51.48851000","lng":"-0.20742000","bedrooms":2,"type":"flat","distance":"0.03" }, {"price":640000,"lat":"51.48932000","lng":"-0.20804000","bedrooms":2,"type":"flat","distance":"0.03" }, {"price":725000,"lat":"51.48843100","lng":"-0.20775400","bedrooms":2,"type":"flat","distance":"0.03" }, {"price":699950,"lat":"51.48928000","lng":"-0.20793700","bedrooms":2,"type":"flat","distance":"0.03" }, {"price":550000,"lat":"51.48941000","lng":"-0.20832000","bedrooms":2,"type":"flat","distance":"0.04" }, {"price":600000,"lat":"51.48813700","lng":"-0.20781600","bedrooms":2,"type":"flat","distance":"0.05" }, {"price":925000,"lat":"51.48948300","lng":"-0.20827400","bedrooms":2,"type":"flat","distance":"0.05" }, {"price":650000,"lat":"51.48824900","lng":"-0.20669000","bedrooms":2,"type":"flat","distance":"0.06" }, {"price":700000,"lat":"51.48941200","lng":"-0.20666900","bedrooms":2,"type":"flat","distance":"0.06" }, {"price":625000,"lat":"51.48817000","lng":"-0.20689500","bedrooms":2,"type":"flat","distance":"0.06" }, {"price":645000,"lat":"51.48822500","lng":"-0.20872100","bedrooms":2,"type":"flat","distance":"0.06" }, {"price":675000,"lat":"51.48877800","lng":"-0.20643200","bedrooms":2,"type":"flat","distance":"0.06" }, {"price":525000,"lat":"51.48978800","lng":"-0.20864000","bedrooms":2,"type":"flat","distance":"0.07" }, {"price":700000,"lat":"51.48785100","lng":"-0.20757600","bedrooms":2,"type":"flat","distance":"0.07" }, {"price":675000,"lat":"51.48819900","lng":"-0.20637900","bedrooms":2,"type":"flat","distance":"0.08" }, {"price":875000,"lat":"51.48818700","lng":"-0.20931800","bedrooms":2,"type":"flat","distance":"0.08" }, {"price":550000,"lat":"51.48891000","lng":"-0.20955000","bedrooms":2,"type":"flat","distance":"0.08" }, {"price":950000,"lat":"51.48808300","lng":"-0.20628500","bedrooms":2,"type":"flat","distance":"0.09" } ] },"process_time":"2.08"}';
$data = json_decode($askPriceBeds, true);
$raw_data = $data['data']['raw_data'];
?>
<body>
<table>
<thead>
<tr>
<td>Price</td>
<td>Lat</td>
<td>Lng</td>
<td>Bedrooms</td>
<td>Type</td>
<td>Distance</td>
</tr>
</thead>
<tbody>
<?php
foreach($raw_data as $raw){
echo "<tr>
<td>".$raw['price']."</td>
<td>".$raw['lat']."</td>
<td>".$raw['lng']."</td>
<td>".$raw['bedrooms']."</td>
<td>".$raw['type']."</td>
<td>".$raw['distance']."</td>
</tr>";
}
?>
</tbody>
</table>
<form method="POST">
<input type="submit" name="generate_pdf" value="Generate PDF">
</form>
</body>
<?php
}
else
{
require_once($_SERVER['DOCUMENT_ROOT'].'/wp-content/themes/property-investment-vault/tcpdf/config/tcpdf_config.php');
require_once($_SERVER['DOCUMENT_ROOT'].'/wp-content/themes/property-investment-vault/tcpdf/tcpdf.php');
$tcpdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$tcpdf->SetCreator(PDF_CREATOR);
$tcpdf->SetTitle('');
$tcpdf->SetHeaderData('', '', PDF_HEADER_TITLE, PDF_HEADER_STRING);
$tcpdf->setHeaderFont(array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$tcpdf->setFooterFont(array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
$tcpdf->SetDefaultMonospacedFont('helvetica');
$tcpdf->SetFooterMargin(PDF_MARGIN_FOOTER);
$tcpdf->SetMargins(PDF_MARGIN_LEFT, '5', PDF_MARGIN_RIGHT);
$tcpdf->setPrintHeader(false);
$tcpdf->setPrintFooter(false);
$tcpdf->SetAutoPageBreak(true, 10);
$tcpdf->SetFont('helvetica', '', 12);
$tcpdf->AddPage('P', 'A4');
$html = '<html>
<head></head>
<body>
<table border="1">
<tr>
<th> Price</th>
<th> Lat</th>
<th> Lng</th>
<th> Bedrooms</th>
<th> Type</th>
<th> Distance</th>
</tr>
<tr>
<td> 650000</td>
<td> 51.48887000</td>
<td> -0.20776000</td>
<td> 2</td>
<td> flat</td>
<td> 0.00</td>
</tr>
<tr>
<td> 575000</td>
<td> 51.48884800</td>
<td> -0.20701200</td>
<td> 2</td>
<td> flat</td>
<td> 0.03</td>
</tr>
<tr>
<td> 615000</td>
<td> 51.48851000</td>
<td> -0.20742000</td>
<td> 2</td>
<td> flat</td>
<td> 0.03</td>
</tr>
<tr>
<td> 640000</td>
<td> 51.48932000</td>
<td> -0.20804000</td>
<td> 2</td>
<td> flat</td>
<td> 0.03</td>
</tr>
<tr>
<td> 725000</td>
<td> 51.48843100</td>
<td> -0.20775400</td>
<td> 2</td>
<td> flat</td>
<td> 0.03</td>
</tr>
<tr>
<td> 699950</td>
<td> 51.48928000</td>
<td> -0.20793700</td>
<td> 2</td>
<td> flat</td>
<td> 0.03</td>
</tr>
<tr>
<td> 550000</td>
<td> 51.48941000</td>
<td> -0.20832000</td>
<td> 2</td>
<td> flat</td>
<td> 0.04</td>
</tr>
<tr>
<td> 600000</td>
<td> 51.48813700</td>
<td> -0.20781600</td>
<td> 2</td>
<td> flat</td>
<td> 0.05</td>
</tr>
<tr>
<td> 925000</td>
<td> 51.48948300</td>
<td> -0.20827400</td>
<td> 2</td>
<td> flat</td>
<td> 0.05</td>
</tr>
<tr>
<td> 650000</td>
<td> 51.48824900</td>
<td> -0.20669000</td>
<td> 2</td>
<td> flat</td>
<td> 0.06</td>
</tr>
<tr>
<td> 700000</td>
<td> 51.48941200</td>
<td> -0.20666900</td>
<td> 2</td>
<td> flat</td>
<td> 0.06</td>
</tr>
<tr>
<td> 625000</td>
<td> 51.48817000</td>
<td> -0.20689500</td>
<td> 2</td>
<td> flat</td>
<td> 0.06</td>
</tr>
<tr>
<td> 645000</td>
<td> 51.48822500</td>
<td> -0.20872100</td>
<td> 2</td>
<td> flat</td>
<td> 0.06</td>
</tr>
<tr>
<td> 675000</td>
<td> 51.48877800</td>
<td> -0.20643200</td>
<td> 2</td>
<td> flat</td>
<td> 0.06</td>
</tr>
<tr>
<td> 525000</td>
<td> 51.48978800</td>
<td> -0.20864000</td>
<td> 2</td>
<td> flat</td>
<td> 0.07</td>
</tr>
<tr>
<td> 700000</td>
<td> 51.48785100</td>
<td> -0.20757600</td>
<td> 2</td>
<td> flat</td>
<td> 0.07</td>
</tr>
<tr>
<td> 675000</td>
<td> 51.48819900</td>
<td> -0.20637900</td>
<td> 2</td>
<td> flat</td>
<td> 0.08</td>
</tr>
<tr>
<td> 875000</td>
<td> 51.48818700</td>
<td> -0.20931800</td>
<td> 2</td>
<td> flat</td>
<td> 0.08</td>
</tr>
<tr>
<td> 550000</td>
<td> 51.48891000</td>
<td> -0.20955000</td>
<td> 2</td>
<td> flat</td>
<td> 0.08</td>
</tr>
<tr>
<td> 950000</td>
<td> 51.48808300</td>
<td> -0.20628500</td>
<td> 2</td>
<td> flat</td>
<td> 0.09</td>
</tr>
</table>
</body>
</html>';
$tcpdf->writeHTML($html, true, 0, true, 0);
$tcpdf->lastPage();
ob_end_clean();
$tcpdf->Output("datareport.pdf", 'FD');
}
?>
I am new to Laravel and currently working on a project where the guests enter form data for booking a room in a hotel and after submitting it, they will be redirected to a page showing their booking they have just done. I know this can be done by allowing guests to create login account but my project requires login only for the backend(admin). (Q2.)Also the guests can view booking by entering the booking ID and their last name.
My controller:
public function store(Request $request)
{
$book = new book();
$book->title = $request->input('title');
$book->fname = $request->input('firstname');
$book->lname = $request->input('lastname');
$book->country = $request->input('country');
$book->phone = $request->input('phone');
$book->email = $request->input('email');
$book->no_rooms = $request->input('no_rooms');
$book->room_type_id = $request->input('type_room');
$book->no_adults = $request->input('adults');
$book->no_children = $request->input('children');
$book->meal_id = $request->input('meal');
$book->check_in = $request->input('check_in');
$book->check_out = $request->input('check_out');
$book->save();
}
public function show(book $book)
{
return view('book.show', compact('book'));
}
The way I want it to display after it has been submitted:
<div class="row">
<div class="col-6">
<table class="table table-striped text-dark">
<tbody>
<tr>
<td> Name: </td>
<td> {{$book->title}} {{$book->firstname}} {{$book->lastname}} </td>
</tr>
<tr>
<td> Phone </td>
<td> {{$book->phone}} </td>
</tr>
<tr>
<td> Email </td>
<td> {{$book->email}} </td>
</tr>
<tr>
<td> Phone </td>
<td> {{$book->phone}} </td>
</tr>
<tr>
<td> Passport Issued: </td>
<td> {{$book->country}} </td>
</tr>
<tr>
<td> Number of rooms: </td>
<td> {{$book->no_rooms}} </td>
</tr>
<tr>
<td> Room Type </td>
<td> {{$book->type_room}} </td>
</tr>
<tr>
<td> No of adults: </td>
<td> {{$book->adults}} </td>
</tr><tr>
<td> No of children: </td>
<td> {{$book->children}} </td>
</tr>
<tr>
<td> Meal plan: </td>
<td> {{$book->meals_id}} </td>
</tr>
<tr>
<td> Check-in: </td>
<td> {{$book->check_in}} </td>
</tr>
<tr>
<td> Check-out: </td>
<td> {{$book->check_out}} </td>
</tr>
</tbody>
</table>
</div>
</div>
You can redirect to the show method from the bottom of your store method:
// Replace MyController with the name of your controller
return redirect()->action(
'MyController#show', ['book' => $book]
);
For more information check out the documentation: https://laravel.com/docs/master/redirects#redirecting-controller-actions
If I understand correctly your situation, you wish to show the same data introduced by the user after form submission.
I can think that redirecting to a page that show your div without anything else, is not enough. You'll still need to access to data entered in the form.
Laravel store form data in a session variable called old, according to documentation, that variable should be available during next request after submission, so you could try to get data using
<table class="table table-striped text-dark">
<tbody>
<tr>
<td> Name: </td>
<td> {{ old('title') }} {{ old('firstname') }} {{ old('lastname') }} </td>
</tr>
<tr>
<td> Phone </td>
<td> {{ old('phone') }} </td>
</tr>
<tr>
<td> Email </td>
<td> {{ old('email') }} </td>
</tr>
<tr>
<td> Phone </td>
<td> {{ old('phone') }} </td>
</tr>
<tr>
<td> Passport Issued: </td>
<td> {{ old('country') }} </td>
</tr>
<tr>
<td> Number of rooms: </td>
<td> {{ old('no_rooms') }} </td>
</tr>
<tr>
<td> Room Type </td>
<td> {{ old('type_room') }} </td>
</tr>
<tr>
<td> No of adults: </td>
<td> {{ old('adults') }} </td>
</tr><tr>
<td> No of children: </td>
<td> {{ old('children') }} </td>
</tr>
<tr>
<td> Meal plan: </td>
<td> {{ old('meals_id') }} </td>
</tr>
<tr>
<td> Check-in: </td>
<td> {{ old('check_in') }} </td>
</tr>
<tr>
<td> Check-out: </td>
<td> {{ old('check_out') }} </td>
</tr>
</tbody>
</table>
Just in case that you don't have old data available in your session because a successive redirect, you should inject that data in your session using flash.
You can find more detailed information here, in laravel documentation
You can just use the laravel helper redirect()
return redirect()->route('route name here');
or
return redirect('your-form-path');
read more here: laravel http redirects
I need to count how many of these items are open, and there are four types of them: Easy, Medium, Difficult and Not-Wanted. All of these types are values inside the div's. I need to exclude the 'Not-Wanted' types from the count. Notice the 'Open' and 'Close' values have different number of spaces around them. This is the html structure:
<table>
<tbody>
<tr>
<td>
<div>Difficult</div>
</td>
<td>Name</td>
<td> Open </td>
</tr>
<tr>
<td>
<div>Easy</div>
</td>
<td>Name</td>
<td> Closed </td>
</tr>
<tr>
<td>
<div>Easy</div>
</td>
<td>Name</td>
<td> Open </td>
</tr>
<tr>
<td>
<div>Medium</div>
</td>
<td>Name</td>
<td>Open </td>
</tr>
<tr>
<td>
<div>Easy</div>
</td>
<td>Name</td>
<td> Open </td>
</tr>
<tr>
<td>
<div>Medium</div>
</td>
<td>Name</td>
<td> Closed</td>
</tr>
<tr>
<td>
<div>Easy</div>
</td>
<td>Name</td>
<td>Closed </td>
</tr>
<tr>
<td>
<div>Not-wanted</div>
</td>
<td>Name</td>
<td> Open </td>
</tr>
<tr>
<td>
<div>Difficult</div>
</td>
<td>Name</td>
<td>Open</td>
</tr>
............
This is one of my attempts to solve the problem. It is obviously wrong, but I don't know how to get it right.
$doc = new DOMDocument();
$doc->loadHtmlFile('http://www.nameofsite.com');
$doc->preserveWhiteSpace = false;
$xpath = new DOMXPath($doc);
$elements = $xpath->query("/html/body/div[1]/div/section/div/section/article/div/div[1]/div/div/div[2]/div[1]/div[2]/div/section/div/div/table/tbody/tr");
$count = 0;
foreach ($elements as $element) {
if ($element->childNodes->nodeValue != 'Not-wanted') {
if ($element->childNodes->nodeValue === 'open') {
$count++;
}
}
}
echo $count;
I have a very rudimental knowledge of DOMXPath, so it is too complex for me, since I'm only able to create simple queries.
Can anybody help?
Thanks in advance.
Based on the data in your example, I think you can adjust the xpath expression to this to get all the <tr>'s that match your conditions:
//table/tbody/tr[normalize-space(td[3]/text()) = 'Open' and
td[1]/div/text() != 'Not-wanted']
$elements is then of type DOMNodeList and you can then get the length property to get the number of nodes in the list.
For example:
$source = <<<SOURCE
<table>
<tbody>
<tr>
<td>
<div>Difficult</div>
</td>
<td>Name</td>
<td> Open </td>
</tr>
<tr>
<td>
<div>Easy</div>
</td>
<td>Name</td>
<td> Closed </td>
</tr>
<tr>
<td>
<div>Easy</div>
</td>
<td>Name</td>
<td> Open </td>
</tr>
<tr>
<td>
<div>Medium</div>
</td>
<td>Name</td>
<td>Open </td>
</tr>
<tr>
<td>
<div>Easy</div>
</td>
<td>Name</td>
<td> Open </td>
</tr>
<tr>
<td>
<div>Medium</div>
</td>
<td>Name</td>
<td> Closed</td>
</tr>
<tr>
<td>
<div>Easy</div>
</td>
<td>Name</td>
<td>Closed </td>
</tr>
<tr>
<td>
<div>Not-wanted</div>
</td>
<td>Name</td>
<td> Open </td>
</tr>
<tr>
<td>
<div>Difficult</div>
</td>
<td>Name</td>
<td>Open</td>
</tr>
</tbody>
</table>
SOURCE;
$doc = new DOMDocument();
$doc->loadHTML($source);
$doc->preserveWhiteSpace = false;
$xpath = new DOMXPath($doc);
$elements = $xpath->query("//table/tbody/tr[normalize-space(td[3]/text()) = 'Open' and td[1]/div/text() != 'Not-wanted']");
echo $elements->length;
Which will result in:
5
Demo