So i already have used SimpleHtmlDom with success. The difference is that i iterated through the pagination based on the url.
For example the domain would be:
www.example.com/articles
and the pagination would be
www.example.com/articles/page/1.html
Now i have a pagination that is based on javascript and i get only the results from the first page. Now, when i tell the page to get to the next page, i assume that it reloads the page, so it goes from the start again because the url does not change.
One extra problem is that the link that belongs to the "next page", if you copy it and paste it on the URL bar then it redirects you to another section of the website. (So much for developers -_- )
I will actually paste the original code so you can see by yourself what is going on with the links.
Anyways my question is the following:
How can i iterate through the whole pagination?
<?php
require "simple_html_dom.php";
$url = "http://es.4story.gameforge.com/ranking/browse?type=world";
$baseUrl = "http://es.4story.gameforge.com/ranking/browse?type=world";
//Create a DOM object
$html = new simple_html_dom();
// Load HTML from a URL
$html->load_file($url);
// Get all links
foreach ($html->find('.enc_controls .enc_pager a.enc_next') as $anchor) {
$tempUrl = $baseUrl . $anchor->href;
$linkHtml = file_get_html($tempUrl);
foreach ($linkHtml->find('#results td .item_classWrap a span') as $item ) {
echo $item;
echo "<hr/>";
}
}
Related
I'm building a web app to make my business more automated with HTML/CSS/jQuery in the front end and python in the backend .
I wrote an easy script with jQuery that call a route with ajax and recieve the data as json and after that parse it and return a list of iframes depending on it's length : If we have 20 items in that list and we want 5 items per page, create 4 pages and put 5 iframes in each link .
Here's the code responsible for this feature :
$('.pageContent').empty()
for (var i = 0; i < vidPageList.length; i++){
var videoUrl = vidPageList[i].videoLink
var postUrl = vidPageList[i].postLink
console.log(postUrl)
$('<iframe src ="'+videoUrl+'" class = "videoEntry" width = "360" height = "250"></iframe>').appendTo( ".pageContent" )
$('<a class = "postLink" href = "'+postUrl+'"> Video link </a>').appendTo(".videoEntry")
}
The problem:
iframes are added perfectly but the links are not added at all, I don't know why, the console is not showing me any error, I tried to debug the values of postUrl and they are all correct, why is it doing this ? It looks like the last line responsible for adding the link is not working, I also tried to add everything with one appendTo() call but it only added iframes, what I'm missing here ?
The first time your selectors ran, there was no .videoEntry class because it is in the iframe. It may be useful to first make all your html content before appending.
I'm working on a website based on the BETHEME, it includes muffin content builder and visual composer. Visual composer is how I hvae build the website.
On the homepage I have included a revolution slider in an visual composer text field. I used the shortcode as follows:
[rev_slider alias="Home_page"]
The problem is that when I search anything in the WordPress search, the results display a piece of javascript code for the homepage result (as it was content of the page).
var htmlDiv = document.getElementById("rs-plugin-settings-inline-css"); var htmlDivCss=""; if(htmlDiv) { htmlDiv.innerHTML = htmlDiv.innerHTML + htmlDivCss; }else{ var htmlDiv = document.createElement("div"); htmlDiv.innerHTML = "" + htmlDivCss + ""; document.getElementsByTagName("head")[0].appendChild(htmlDiv.childNodes[0]); […]
After some research i only found out it's used by revolution slider. How do I cause this script to not display in my search results?
This seems like a bug with Revolution Slider(not really sure)
However this is how I went about fixing it.
Edit your functions.php and add the following:
add_post_type_support( 'page', 'excerpt' );
This code modifies the default WordPress content type ‘page’ to add support for excerpts.
After you've done this, edit the page that shows the Javascript code in your search results and add content to the 'Excerpt' field for that page and save. Search again and you'll see that the code is no more.
One potential solution is to remove shortcodes from your search results.
One method to do this would be to add the below code to your theme's functions.php file.
function remove_shortcodes_from_search( $content ) {
// Only modify the content if it is the search results page
if ( is_search() ) {
$content = strip_shortcodes( $content );
}
return $content;
}
// Assign a very low number (-9999) to priority to ensure it runs before shortcodes are expanded
add_filter( 'the_content', 'remove_shortcodes_from_search', -9999 );
First time here so apologies if I'm doing something wrong.
I have the following php code:
<?php
$quoteFile = "quotes.txt"; //File holding qoutes
$fp = fopen($quoteFile, "r"); //Opens file for read
$content = fread($fp, filesize($quoteFile));
$quotes = explode("\n",$content); //Put quotes into array
fclose($fp); //Close the file
srand((double)microtime()*1000000); // randomize
$index = (rand(1, sizeof($quotes)) - 1); //Pick random qoute
?>
The code fetches a random quote from a text file by randomly choosing one of the lines of the .txt file.
I then echo out the result using:
echo $quotes[$index];
However what I want to achieve and don't seem to be able to is to have a button (html) that when clicked executes the echo $quotes[$index]; to the current page. So that each time the button is clicked it prints/echo's out a random quote from the .text file.
I did mess about with just setting a button up to refresh the page which by default made a new random quote display but it sometimes just reloaded a blank so I'm hoping someone can help me achieve this better or prompt me in the right direction. Thank tou.
You can try saving that variable into a session variable like this:
$_SESSION['quote'] = $quote['index'];
Then create an anchor element that redirects to current page:
Refresh
And print the result on the page:
<span><?php echo $_SESSION['quote']; ?></span>
To do all of this, you need to set a session. At top of your php file write:
session_start();
Hope that helps. :)
Your TXT file might have an empty line in it at the end or anywhere else. A second explanation of this, is that the way you are generating randomness is quite questionable.
Check out this simple example by W3 Schools.
$a=array("red","green","blue","yellow","brown");
$random_keys=array_rand($a,1);
echo $a[$random_keys[0]]."<br>";
The array_rand() function returns a random key from an array, or it returns an array of random keys if you specify that the function should return more than one key.
Or, simply:
<?php
$a=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
print_r(array_rand($a,1));
?>
Full Post: http://www.w3schools.com/php/func_array_rand.asp
Happy coding !
JQuery seems to be blocked
Hello there, I've been confronting this problem for several days, I just can't find a way to get this fixed, or around it.
What I want to do is simple, I want to read out every sub-folder of a big Project folder. Then assign a thumbnail image and a figcapture to this folder. With a simple for loop, php builds this for me. Everything works perfect and quick. The only thing is that the jquery won't respond. Even though I have created various menus with this technique. As you can see in my code, in the "script" tags, I have the jquery code which doesn't seem to work.
I don't know wheter php puts in a space somewhere or I just looked too long at this code for seing the error.
I appreciate any help.
<?php
/*Because of the "ease of use" aspect of this site, I prefered to write it completely in PHP,
advantage of that:
Images are loaded directly out of the folder, so I can just drag and drop something onto the server without having to write aditional code.
As you see, it can save a lot of time.*/
echo "<h1>Referenzen</h1><br>";
$projects = scandir('../src/sub/credentials'); //The credentials directory is being scanned and converted into an array.
$projectsSize = count($projects); //$size is being created. It counts the number of objects in the array which has been created above.
$projectsCaptions = file('../src/sub/captionsOfCredentials.txt'); //Edit the name of the figcaption in the "captionsOfCredentials.txt".
for($i = 2; $i < $projectsSize; $i++){ /*Simple "for" loop, that creates $i with the size of two, because PHP is 0-index based and has the "dot" and the "dotdot" folder. The loop stops at the end of the array($size).*/
echo '<a href="index.php#PRJ'.trim($projectsCaptions[$i]).'" class="ID'.trim($projectsCaptions[$i]).'">
<div class="projectFolder">
<img src="src/sub/credentialsThumb/project_00'.$i.'.jpg" width="100%" />
<figcaption>'
.$projectsCaptions[$i].
'</figcaption>
</div>
</a>';
/*Project folder level starts here.*/
$images = scandir('../src/sub/credentials/project_00'.$i);
$imagesSize = count($images);
for($k = 3; $k < $imagesSize; $k++){
$tempID = ('ID'.trim($projectsCaptions[$i]).'.php'); //$tempID is the entire file name including the .php part.
$handle = fopen($tempID, "a") or die ('Unable to open '.$tempID.' , please contact admin A.S.A.P..');
$imagesCode = 'test';
fwrite($handle, $imagesCode);
}
//end second for-loop
echo "
<script>
$(document).ready(function () {
$('#ID".$projectsCaptions[$i]."').click(function () {
$('#mainContent').load('de-DE/".$tempID."');
});
});
</script>";
}
//end first for-loop
?>
You're selecting an element by id when you need to use class. Change the JS block to this:
$(document).ready(function () {
// Note ".ID" not "#ID"
$('.ID".$projectsCaptions[$i]."').click(function () {
$('#mainContent').load('de-DE/".$tempID."');
});
});
UPDATE
It seems like you've also got an illegal character in $projectsCaptions[$i]. It's most likely a newline character. Try wrapping the above reference above in trim():
$('.ID" . trim($projectsCaptions[$i]) . "').click(function () {
On my web site every page has the same sidebar which is just a link to every page. Instead of writing the same thing on every page I'm using a file 'sidebar.html' which is then included to every page with php:
<?php include 'sidebar.html'; ?>
Now I would like to have the name of the current page in sidebar in different colour than any other page name. I have done this with JavaScript like this:
1. get the current page name by the specific id
2. set the new id called 'active' to this name and in css #active gets a new colour
I'm wondering if there is any other solution to obtain the same result and maybe if there is a solution without using JavaScript.
Thank you!
You can replace your sidebar.html by a sidebar.php file, and simply use PHP variables ; for example, you can define in your current page a variable that represent the current ID, and use it inside your sidebar.php to determine which link should be active
The other answers suggesting a PHP variable is one way to do this. On the other hand you could do this without using PHP or JavaScript, just pure CSS.
Start by indicating the current page with a class name on your <body> tag or other high-level wrapper.
<body class="contact-page">
Then in your sidebar, make sure each link has a unique class as well. Something like:
Home
Contact
... and so on
Now you can just write CSS classes to do the rest! So if you want your selected links to be blue for example...
.home-page .sidebar-home { color: blue; }
.contact-page .sidebar-contact { color: blue; }
You can change the html file to a php file and pass a parameter.
Then, in the new sidebar.php, get the parameter and define the active class accordingly.
I would do something like this:
sidebar.php
$links = array();
$links['page1.html'] = 'Page 1';
$links['page2.html'] = 'Page 2';
$links['page3.html'] = 'Page 3';
$curr_page = basename($_SERVER['PHP_SELF']);
foreach($links as $k=>$v)
{
echo '<a href="'.$k.'"';
if($curr_page === $k)
{
echo ' class="active_link"';
}
echo '>'.$v.'</a>';
}
Just wanted to point out that things will get complicated if you put files into sub-directories like this:
$links = array();
$links['subdir1/page1.html'] = 'Page 1';
$links['subdir2/page2.html'] = 'Page 2';
$links['subdir3/page3.html'] = 'Page 3';