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}");
Related
I am trying to do a page grouping/ page break in TCPDF from the recordsets i get from database by grouping by sql statement. Each 'group by' recordsets from database should start from a new page in PDF. I tried looking for the examples given at TCPDF website but they are more static pages and not the dynamic. Is there any way around we can do it?
Thanks in advance
It is very easy Sarah.
When you doing the While loop to fetch the recordsets delcare it like:
<?php
$pb = 0; //pagebreak counter
do {
if ($pb != $row['yourforiegnkey']){?>
// make sure you close your tag for php above here
<br pagebreak="true"/>
<?php // and open your php tag again here, or else it will not go to the next page
$pb = $row['yourforiegnkey'];
}
else {
echo $row['yourforiegnkey'] . "<br>";
}
?>
Hope this works!!!
Here's the problem, I am trying to echo a statement or an array after dynamically generated HTML, and unfortunately the thing that i want to echo goes above the HTML, is there any way to echo it after that dynamic HTML or work around?
Code:
Link 1
Link 2
if(isset($_GET["id"]) && $_GET["id"] == "do_something") {
$html = "dynamic html generate";
echo $html;
//after this im using foreach
foreach($array as $item) { echo $item . "<br />"; }
}
As I click one of these two , dynamically generated HTML shows up. Now for example I have an array:
$array = array("error1", "error2");
All the generated PHP goes above the dynamic HTML :/.
How should i fix it so that i can echo all of this array below the dynamic HTML?
Thanks
Use buffering with ob_start
ob_start();
// dynamic html code generate
$dynamic_html = ob_get_clean();
echo $dynamic_html;
// your code
echo $dynamic_html;
Sounds like you missed some closing tags (most likely </table>) in the dynamic html. Thats why the later generated echo gets displayed at the top.
Example (Note the missing closing table):
<?php
echo "<table><tr><td>TableText</td></tr>";
echo "I should be bellow the table, but going to the top.";
?>
will produce:
I should be bellow the table, but going to the top.
TableText
$_SESSION are coming from another page.. when i run a search on my search engine the # of results shows up but stays on the page forever and ever.. have a look (gawd i need 10 rep points to post images)
well basically 0 results is always shown and below it are the results of the search which is always there as well.
0 results for "pepsi"
2 results for "flash"
flash - website tutorial
flash design - come learn from the best
this is basically what the search page looks like only the search result links and descriptions will disappear if the search result produces no results. i would like to have everything disappear on every new search.. even when i refresh teh page or come back to it the results from the previous search are there.
<div id="searchPageDiv">
<?php
if(isset($_SESSION['error'])){
echo $_SESSION['error'];
} else
print_r($_SESSION['search_output']);
{
echo ($_SESSION['count']);
foreach($_SESSION['search_output'] as $value){
$value['links'];
$value['title'];
$value['page_body'];
$title = $value['title'];
$link = $value['links'];
$body = $value['page_body'];
$search_output .= "<a href='".$link."'>".$title."</a> - $body<br>";}
}
?>
<?php echo $search_output; ?>
</div>
This will be helpful:
echo ($_SESSION['count']);
unset($_SESSION['count']);
Studying is best approach. Look for examples given in www.php.net/unset
I have installed a plugin (use PHP in posts) for Wordpress, and am having some issues.
Here is my code (though I don't think its the issue)
$usertoget = $_GET['player'];
$partone = "http://website.co.uk/path/to/php/file.php";
if(strlen($usertoget) != 0) {
$parttwo = "&player=";
$partthree = $usertoget;
$finalurl = $partone . $parttwo . $partthree;
print '<iframe src="$finalurl"></iframe>';
}
else {
print "<iframe src='/path/file.php'>";
}
The iframe gets printed perfectly and does what the target file should, but as you can see from this picture, messes up the formatting:
No sidebar;
No footer;
No floating header.
I have noticed that the page source finishes the iframe and does nothing else except close (</body></html>). There is a lot of stuff after it that should appear but doesn't.
Another issue is, using GET requests (/page/hello?player=9 and /page/hello/?player=9) returns a 404. Any ways to resolve this issue?
Does anybody know how I can fix this? I can post any more code if required :)
Any help would be appreciated :D Thanks!
If PHP suddenly stops sending the expected output, it usually means there's a PHP error. Is there anything in your logs? Have you tried turning WP_DEBUG on?
I'm surprised you say the iframe gets printed perfectly. This line
print '<iframe src="$finalurl"></iframe>';
looks wrong to me (though it won't cause PHP to error). If you have single quotes around a string, variables won't be evaluated. See this question for a little more detail.
You have mistake in concatenation of $finalurl .you can check you html its print the $finalurl variable as it is.Its didn't outputted its values .I had check it.So you just have to concatenate the $finalurl variable correctly .Here is your code which i had made corrected.
$usertoget = $_GET['player'];
$partone = "http://website.co.uk/path/to/php/file.php";
if(strlen($usertoget) != 0) {
$parttwo = "&player=";
$partthree = $usertoget;
$finalurl = $partone . $parttwo . $partthree;
print '<iframe src='.$finalurl.'></iframe>';
}
else {
print "<iframe src='/path/file.php'>";
}
if you have any query please comment bellow.
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; ?>