PHP - Appended html string being cut off - php

I'm generating some html to be used with the MPDF php library for generating a PDF. But when I create some HTML with php which has a loop inside it, it cuts of the beginning of the string, here's my code:
$page = "<div class='A4'><h1>Test: lookbook</h1><p>";
$page .= $date;
$page .= "</p><div class='items'>";
while ($the_query->have_posts()) {
$the_query->the_post();
$page .= "<img src='";
$page .= get_field('product_image')['url'];
$page .= "'>";
}
$page .= "</div></div><div class='footer'>A Story in every gemstone</div>";
echo $page;
Running the above code returns:
1<img src='urltoimage'></div></div><div class='footer'>A Story in every gemstone</div>
So it looks like everything before the while loop is being cut out.

$page is a Wordpress global variable that exists within The Loop.
https://codex.wordpress.org/Global_Variables
WordPress-specific global variables are used throughout WordPress code for various reasons. Almost all data that WordPress generates can be found in a global variable.
While inside the loop, these globals are set, containing information about the current post being processed.
...
$page (int) The page of the current post being viewed. Specified by the query var page.

Related

Paginated PHP web scraper

Hi there I'm new here.
Trying to make this little code to loop over pages. And scrape off links of headings .
Scraping part works just fine but i cant make it to loop to the next page. It keeps looping on the same page.
<?php
include('../simple_html_dom.php');
// start at page 1
$xder = 1;
do {
// web page + page number (should change with every loop)
$html = file_get_html('https://webpage.com/stuff/page/$xder');
foreach($html->find('h3') as $h3)
{
foreach($h3->find('a') as $element)
{
echo $element->href . '<br>';
}
}
$xder++;
} while ($xder <= 5);
?>
I'm expecting to get list of links from all 5 pages, but I only get list of links from 1st page repeating 5 times.
I think the problem is here "/stuff/page/$xder');" I'm not sure how to add a variable to the back of an URL it doesn't appear to work.
Tried methods here:
Converting an integer to a string in PHP
Getting frustrated with this. Not sure what I'm missing here. Thanks for any thoughts :)
Php variables are treated as variables only if you use ", and not '
Change
$html = file_get_html('https://webpage.com/stuff/page/$xder');
to
$html = file_get_html("https://webpage.com/stuff/page/{$xder}");

how to print the content in wordpress php by passing the string

how to print the content in wordpress by passing the string php.I have written the following code but it print all the content including image.I want it to print only particular text.
<?php
$content = get_the_content('Read more');
print $content;
?>
This depends on whether or not you're in the Loop - if you are then your code should work - see more here: https://codex.wordpress.org/The_Loop
However, if you are outside the loop and want to pass in a post ID as a paramter, you can refer to this post as everything is explained very well in there: https://wordpress.stackexchange.com/questions/51741/get-post-content-from-outside-the-loop
$page_id = 302;
$page_object = get_page( $page_id );
echo $page_object->post_content;
Use the_excerpt for only showing small content with read more
<?php $short_desc= get_the_excerpt();
echo substr($short_desc,0,200);?>

PHP prints variable to screen and code stops running

I am trying to use Parsedown Extra with Parsedown (Have never used either before). I have the code $_GET the selected category (?cat=0) and set it's path & filename to a var. It $_GETs the page number just fine, however when I set the file var, it just prints to the screen and doesn't load my page.
//sets the page (category) number for use with array
//also sets the path to the category's pages
if (isset($_GET['cat'])) {
$catNum = $_GET['cat'];
$catPath = 'content/' . $pageList[$catNum]['path'];
echo '<div class="center pageNav">';
//lists out subpages of catagory
$pageAmt = count($pageList[$catNum]['pages']);
for ($i = 0; $i < $pageAmt; $i++) {
echo '' . $pageList[$catNum]['pages'][$i]['title'] . '';
};
echo '</div>';
//sets path & filename var to selected page: this is the part where it prints the var and doesn't run the rest. The var is pointing to the right file, I checked.
$page = $catPath . $pageList[$catNum]['mainPage'];
} else {
$page = 'content/home.md';
};
//parsedown
require 'parsedown/parsedown.php';
require 'parsedown/parsedownextra.php';
echo ParsedownExtra::instance()
->setBreaksEnabled(true)
->setMarkupEscaped(true)
->text($page);
you have a semi colon at the end of your if statement.
} else {
$page = 'content/home.md';
}; <--
Parsedown takes marked up text and renders it. In your example, you pass $page (which holds a string, a filename) to ->text($page). This parses the string as marked up text, and renders it. So, in your example you are seeing exactly what it is doing. If you are trying to run the text of the file through ->text, you need to load the file contents first and pass to Parsedown.

How to create permalinks without single pages in wordpress?

I'm creating my own wordpress theme which is a bit different because it will not have single pages (or atleast, no single page will be reachable). The whole website contains just the homepage (with the loop) and previous posts pages.
I want to link to individual posts inside the loop like site.com#post-124 or site.com/paged=5#post-214.
I already created a function that does this:
function getPermalink($id,$postsPerPage) {
$postNumber = Get_Post_Number($id);
//a function that get's the post number based on
//the chronical order of published posts.
$page = floor(($postNumber - 1) / $postsPerPage);
$url = get_option('home');
if($page > 0) {
$url .= '/?paged=' . ($page + (1 - floor($page / 5)));
}
$url .= '#post-' . $id;
return $url;
}
You can see it live here: http://mijnrealiteit.nl (the previous posts pages are replaced by an infite scroll plugin).
This works, however it breaks when I start adding posts because all the posts before will get shifted back to pages further away (this makes the link invalid).
The way I see it there are two possible solutions:
Change the permalinkstructure to display paging backwards (so x.com/paged=231 becomes the first 'previous' page. However this is not userfriendly.
Make links with just the ID and let wordpress handle custom redirection to the page at that current point in time.
Are there better alternatives? I'm sure this is already solved somewhere, I just couldn't find it.
I got pushed in the right direction by a friend, I build it quite easily using option 2:
The getPermalink function is now much simpler:
function getPermalink($id) {
return get_option('home') . '/?f=' . $id;
}
I didn't make any custom redirection, I just checked at the homepage for a an 'f' being passed in the GET request:
$perma = $_GET['f'];
if(isset($perma) && !is_paged()) {
$customposts = get_posts('p=' . $perma );
foreach( $customposts as $post ) :
setup_postdata($post); ?>
//load the post
<?php endforeach;
}?>
If that is true the post will be fetched using the get_posts function by Wordpress. I also check the (normal) loop for the post that already has been served:
<?php while (have_posts()) : the_post();
if(get_the_ID() != $perma) { ?>
//load the post
<?php } endwhile; ?>

Php is not working but iframe is working fine

I have few sets of code to display right side of my webpage. I am using this place to show some user information and some short of ads. For ads I am using that code in my database. Now the problem is that I want to display one external php file which is located in the same directory where I am trying to include or require this php file.
But I dont know why this is not working. I tried with iframe that works fine but I want to use php include but it is not working .please tell where I am doing mistake. Here is my code:
if (is_array($data['blocks'])) {
$output .= '<div id="appside">';
foreach($data['blocks'] as $block) {
if (is_array($block)) {
$output .= '
<div class="block">';
if ($block['title']) {
$output .= '<div class="block_title">'.$block['title'].'</div>';
}
$output .= '<div class="block_content">
'.$block['content'].
'</div>
</div>
';
}
}
$output .= get_gvar('theme_block_sidebar').
'<div><?php include("/home/xxxxxxx/public_html/xxxxxxxa.php"); ?></div>
</div>
';// end of app_sidebar
I tried with all short of alternatives but its printing raw php code. its not getting executed. I tried to put <?php include(\'/home/xxxxxxx/public_html/xxxxxxxa.php\'); ?>
and all other sort of alternatives but its printing raw php code.
'<div><?php include("/home/xxxxxxx/public_html/xxxxxxxa.php"); ?></div>
PHP won't execute inside a string (which is all the above is).
You can't really concatenate an include statement (unless the included file returns something via return). Given I can't tell if your code snippet is inside a function or how it's executed, my best solution is to use output buffering.
$output .= get_gvar('theme_block_sidebar') . '<div>';
ob_start();
include "/home/xxxxxxx/public_html/xxxxxxxa.php";
$output .= ob_get_contents();
ob_end_clean();
$output .= '</div>'
if the file you wish to include is in the same directory as this file, then you should include the filename only
<?php include('filename.php'); ?>
if it is in a higher level or in a directory or a sub-directory in a higher level, then you should move up to this level then down to the file.
<?php include('../../path/to/file.php'); ?>
where ../../ is going up 2 levels, you can set it to as many levels you want to go up.
If you want to get the output from an external php file in a variable, you can get it using the PHP buffering functions:
ob_start();
include("/home/xxxxxxx/public_html/xxxxxxxa.php");
$output_from_php_file = ob_get_contents();
ob_end_clean();
Then you can append $output_from_php_file to your $output variable wherever you want.

Categories