TCPDF multiple pages not display properly - php

I am using PHP Codeigniter While I will try to convert pdf for multiple pages, the next page data coming on the first pages. I want to convert pages data page wise. How can I do it?
I have followed this code:
Controller:
<?php
$createPDFFile="myData.pdf";
$data['user_data'] = $returnData;
$htmlContent = $this->load->view('admin/download_user_data', $data, TRUE);
$this->createPDF(FCPATH.'assets/generate_pdf/'.$createPDFFile, $htmlContent);
?>
in View
<?php
if(!empty($user_data)) {
foreach($user_data as $pdata) {
?>
<!-- Here My html Code -->
<?php
}
}
?>

You have to put a break line after page data. I have got the success to added this line below of the last html tag.
<br pagebreak="true"/>
Hope it will be helpful for you.

Related

Bootstrap Carousel only on home page

I am building a Website using Bootstrap 4. The template that I am using has a carousel at the top of the page. I use my index.php page as a shell for other content which is called using the following code:
<?php
if (!isset($_REQUEST['content']))
include("home.php");
else
{
$content = $_REQUEST['content'];
$nextpage = $content;
include($nextpage);
}
?>
Currently the carousel will show on every page. I would like for it to only show on the home page or index.phpcontent=home.php but I have yet to figure out how to do that without breaking things.
On the index.php page I have set up chunks of code to be called with include statements:
<body>
<header>
<?php include 'navbar.php'; ?>
<?php include 'carousel.php'; ?>
</header>
I have tried to programatically simply call the carousel.php as follows:
<?php
if (!isset($_REQUEST['content']))
include("carousel.php");
else
{
include("");
}
?>
But that did not work. Any help would be greatly appreciated.

ACF Fields Rendered Outside of HTML Tags

I am using ACF and have set up a field for my products called product_title.
I am trying to display this custom field in on my product page using the WooCommerce hooks like this in my functions.php file.
function hom_add_product_header()
{
$product_title = the_field('product_title');
?>
<h1><?php echo $product_title; ?></h1>
<?php
}
add_filter('woocommerce_single_product_summary', 'hom_add_product_header', 5);
But when I look at the rendered page the product_title is rendered outside of empty <h1></h1> tags!
I have also tried output buffering like this...
function hom_add_product_header()
{
# Add the start
ob_start();
$product_title = the_field('product_title');
?>
<h1><?php echo $product_title; ?></h1>
<?php
# Fetch the cache
$data = ob_get_contents();
# End and clear the buffer
ob_end_clean();
# Send data back for render
return $data;
}
add_filter('woocommerce_single_product_summary', 'hom_add_product_header', 5);
But with this code nothing gets displayed at all!
Why is my content not being rendered inside the HTML tags?
This should work:
function hom_add_product_header()
{
$product_title = get_field('product_title');
echo "<h1>" . $product_title . "</h1>"
}
The php script is parsed from top to bottom and each non php part is directly rendered on parsing. Since you are calling you function a few lines after the declaration the parser sees the <h1></h1> tags and renders them. After that the renderer gets to the function call an executes the function. The critical part is that you are breaking the php controll flow within the function.

Using html code in php function or no?

i would to know what is good practice for writing code to put all HTML code inside PHP function and in my front index.php file just call function to show code.
class.php:
public function test() {
$sql='select id,title from test ';
$nem=$this->db->prepare($sql);
$nem->execute();
$nem->bind_result($id,$title);
echo '<ul class="centreList">';
while($nem->fetch())
{
echo '<li>'.$id.'<a href="'.$title.'" >Download</a></li>';
}
echo '</ul>';
}
index.php:
<?php $connection->test(); ?>
This work fine, but I would like to know is this proper way or is not a good practice to use html code inside PHP functions?
It's ok to build HTML within PHP, but I would not echo to the screen directly from within the function. Instead, return the built HTML string.
$html = '<ul class="centreList">';
while($nem->fetch())
{
$html .= '<li>'.$id.'<a href="'.$title.'" >Download</a></li>';
}
$html .='</ul>';
return $html
The function should not be responsible for pushing content to the browser because it really limits what you can do with your code. What if you wanted to further process the HTML? What if you run into a condition later in the code and decided to abort? What if you wanted to set some response headers later? Some content would already be gone so none of these things would be possible without clever workarounds.
In general you want to separate your responsibilities: I would even break things down further:
one piece of code is in charge of retrieving info from the DB and returning
Another piece is in charge of building the HTML string
A third piece is in charge of displaying the HTML (probably your index.php)
New index.php
<?= $connection->test(); ?>
Do not use echo to print the html directly, wrap the html within while loop surrounded by php tags
public function test() {
$sql='select id,title from test ';
$nem=$this->db->prepare($sql);
$nem->execute();
$nem->bind_result($id,$title);
return $nem;
}
<ul class="centreList">
<?php $res = test()->fetch();
while( $res->fetch() ) { ?>
<li> <?php echo $id ?> Download </li>;
<?php } ?>
</ul>

Using shortcode breaks RSS feed

I had successfully created shortcode on my wordpress website to use it in the posts. But I later realised that it is breaking my rss-feed.
Here is the shortcodes.php
<?php
function disclaimer() {
$disc = " <p style='font-style:italic;'> <strong>Disclaimer</strong> :</p>";
return $disc;
}
add_shortcode('disclaimer', 'disclaimer');
?>
Here is how I am including it on the functions.php
<?php
include 'shortcodes.php';
So now, I am getting this error when I access my rss URL on my browser
So when I removed this include 'shortcodes.php'; my rss feed started working. But then I also want my shortcode to work. Am I doing anything wrong here? Any help would be highly appreciated.
You can change your function so that the disclaimer is not added on feed page
function disclaimer() {
if ( ! is_feed() ) {
$disc = " <p style='font-style:italic;'> <strong>Disclaimer</strong> :</p>";
return $disc;
}
}
Edit:
Then it is not a problem with the code, but some problem with the file.
Make sure there is not extra space after the closing ?> php tag. Or better, try to remove it completely.

MVC for footer-header-sidebar at codeigniter

I use codeigniter. I have footer_view.php, header_view.php and other view php files for pages like aboutus, contactus, etc... for example for about us page, I create model for db actions, than I create a controller to get db variables and send variable to about us view as:
$this->load->view('aboutus/',$data);
Everything is fine until this point, but when I need to get data from db at footer, how will I use in the CodeIgniter Way?
If I create a footer_model, I cannot make view('footer/') because it is actually a part if page, not a page itself :/
You can use $this->load->vars($data);
This will make all data available to all views, footer_view.php, header_view.php and any other views.
$data['your_info'] = $this->user_model->get_info();
$data['your_latest_info'] = $this->user_model->get_latest_info();
$data['your_settings_info'] = $this->user_model->get_settings_info();
$this->load->vars($data);
$this->load->view('your_view');
You view can and will access the data like so:
Your "primary" view
<?php
// your_view.php
// this can access data
$this->load->view('header_view');
<?php foreach ($your_info as $r):?>
<?php echo $r->first_name;?>
<?php echo $r->last_name;?>
<?php endforeach;?>
$this->load->view('footer_view');
?>
Your header_view
<?php
// header_view.php
<?php echo $your_latest_info->site_name;?>
?>
Your footer_view
<?php
// footer_view.php
<?php foreach ($your_setting_info as $setting):?>
<?php echo $setting->pages;?>
<?php endforeach;?>
?>
No need for any template library...
I wrote a blog post about this http://joshhighland.com/blog/2008/11/09/how-i-do-layouts-and-views-in-codeigniter/
basically, you want to load all the data that all your views are going to need in the controller. I use an array of data elements, then pass that to the layout view. My blog post explains more about this, and shows more detail. Here is a sample of my controller.
$this->load->model('search');
$lastest_albums = $this->search->last_n_albumsAdded(10);
$popular_songs = $this->search->popular_n_songs(10);
$featuredAlbums = $this->search->getFeaturedAlbums();
$body_data['featured'] = $featuredAlbums;
$body_data['newest'] = $lastest_albums;
$body_data['popular'] = $popular_songs;
$layout_data['content_body'] = $this->load->view(’home/homePage’, $body_data, true);
$this->load->view('layouts/main', $layout_data);

Categories