I would like to know how can i set scroll in foselasticabundle? I have this code
$res = $this->commentIndex->createSearch($query, ['scroll' => '1m']);
$res->addType('reading');
$res->scroll();
I know Im already close getting the result of my query. Can you help me which of the function in foselasticabundle I can use to display the results of my query. Im trying deep study the code of foselasticabundle.
I found the answer of the issue in this link
I have this code the same in the link
$search = $this->commentIndex->createSearch();
$search->addType('reading');
$search->setQuery($query);
$scroll = new \Elastica\Scroll($search);
$results = [];
foreach ($scroll as $scrollId => $resultSet) {
foreach ($resultSet->getDocuments() as $doc) {
$results[$doc->getId()] = $doc;
}
}
So far in this approach I can get the scroll id and the results i need. But if you found another way, hoping you can post it in this question.
Related
At start i made html table for specific xml array,
it worked.But then i tested my code on others and it failed read all levels :(
here are 2 arrays that try to access manually
$newaCref->Debts->LiabilityDebts[$i]->Debt->Sum[$b]->Total
$newaCref->Debts->LiabilityDebts[$k]->Debt[$j]->Sum->Total
to access them first parts $newaCref->Debts->LiabilityDebts are the same but then it changes and goes even deeper.
My question is how to make it go automatically through all levels i need?
This is how i do it now for each <td> row and $newaCref is result of XML
$newaCref = new SimpleXMLElement($xmldataC, LIBXML_NOCDATA);
for($i=0;$i<count($newaCref->Debts->LiabilityDebts);$i++){
for($b=0;$b<count($newaCref->Debts->LiabilityDebts[$i]->Debt->Sum);$b++){
foreach($newaCref->Liabilities->Liability[$i]->Sum[$b]->Total as $row){
echo '<td>'.$row.'</td>';
}
}
}
Not testet, just to explain:
$newaCref = new SimpleXMLElement($xmldataC, LIBXML_NOCDATA);
$result = $xml->xpath('/Debts/LiabilityDebts');
while(list( , $node) = each($result)) {
$subresult = $node->xpath('Debt/Sum');
while(list( , $subnode) = each($subresult)) {
echo '<td>'.((string)$subnode).'</td>';
}
}
Notes:
In your example the inner foreach makes no sence at this point.
$newaCref->Liabilities->Liability[$i]
The xpath for this would be /Liabilities/Liability
If you are showing $xmldataC in your example, why are you saying #RiggsFolly i cant do it :( and not just do print_r(htmlentities($xmldataC))?
So take this as example and get it work for your needs. But keep in mind, xml can be tricky if you dont now how they work, especially in PHP.
:)
I'm trying to get all of the notes from a particular evernote notebook. I am able to display all of the data as an array, and I'm trying to use a foreach loop to get the title. I also want to be able to get the content, date, etc.
$filter = new NoteFilter();
$filter->notebookGuid = $notebookGuid;
$notelist = $client->getNoteStore()->findNotes($authToken, $filter, 0, 100);
foreach($notelist as $value) {
echo $value->title;
}
I know that I'm being really stupid, but I'm new to php and evernote. Any help is appreciated!
The return value of NoteStore.findNotes is NoteList which is not a collection. You have to get notes attribute from NoteList and then iterate it.
By the way, findNotes is now deprecated so please use findNotesMetadata.
You might want to check the following example from evernote:
https://github.com/evernote/evernote-sdk-php/blob/master/sample/client/EDAMTest.php
Sort of a two part question but maybe one answers the other. I'm trying to get a piece of information out of an
<div id="foo">
<div class="bar"><a data1="xxxx" data2="xxxx" href="http://foo.bar">Inner text"</a>
<div class="bar2"><a data3="xxxx" data4="xxxx" href="http://foo.bar">more text"</a>
Here is what I'm using now.
$articles = array();
$html=file_get_html('http://foo.bar');
foreach($html->find('div[class=bar] a') as $a){
$articles[] = array($a->href,$a->innertext);
}
This works perfectly to grab the href and the inner text from the first div class. I tried adding a $a->data1 to the foreach but that didn't work.
How do I grab those inner data tags at the same time I grab the href and innertext.
Also is there a good way to get both classes with one statement? I assume I could build the find off of the id and grab all the div information.
Thanks
To grab all those attributes, you should before investigate the parsed element, like this:
foreach($html->find('div[class=bar] a') as $a){
var_dump($a->attr);
}
...and see if those attributes exist. They don't seem to be valid HTML, so maybe the parser discards them.
If they exist, you can read them like this:
foreach($html->find('div[class=bar] a') as $a){
$article = array($a->href, $a->innertext);
if (isset($a->attr['data1'])) {
$article['data1'] = $a->attr['data1'];
}
if (isset($a->attr['data2'])) {
$article['data2'] = $a->attr['data2'];
}
//...
$articles[] = $article;
}
To get both classes you can use a multiple selector, separated by a comma:
foreach($html->find('div[class=bar] a, div[class=bar2] a') as $a){
...
I know this question is old, but the OP asked how they could get all the attributes in one statement. I just did this for a project I'm working on.
You can get all the attributes for an element with the getAllAttributes() method. The results are automatically stored in an array property called attr.
In the example below I am grabbing all links but you can use this with whatever you want. NOTE: This also works with data- attributes. So if there is an attribute called data-url it will be accessible with $e->attr['data-url'] after you run the getAllAttributes method.
In your case the attributes your looking for will be $e->attr['data1'] and $e->attr['data2']. Hope this helps someone if not the OP.
Get all Attributes
$html = file_get_html('somefile.html');
foreach ($html->find('a') as $e) { //used a tag here, but use whatever you want
$e->getAllAttributes();
//testing that it worked
print_r($e->attr);
}
Check this code
<?php
$html = file_get_html('somefile.html');
foreach ($html->find('a') as $e) {
$filter = $e->getAttribute('data-filter-string');
}
?>
$data1 = $html->find('.bar > a', 0)->attr['data1'];
$data2 = $html->find('.bar > a', 0)->attr['data2'];
I'm having some problem understanding how to resolve this loop:
I'm developing a small scraper for myself and I'm trying to figure out how to loop within 2 methods until all the links are retrieved from the website.
I'm already retrieving the links from the first page but the problem is that I can't make a loop to verify the new links already extracted:
Here is my code:
$scrape->fetchlinks($url);//I scrape the links from the first page from a website
//for each one found I insert the url in the DB with status = "n"
foreach ($scrape->results as $result) {
if ($result) {
echo "$result \n";
$crawler->insertUrl($result);
//I select all the links with status = "n" to perform a scrape the stored links
$urlStatusNList = $crawler->selectUrlByStatus("n");
while (sizeof($urlStatusNList > 1)){
foreach($urlStatusNList as $sl){
$scrape->fetchlinks($sl->url); // I suppose it would retrieve all the new sublinks
$crawler->insertUrl($sl->url); // insert the sublinks in the db
$crawler->updateUrlByIdStatus($sl->id, "s"); //update the link scraped with status = "s", so I will not check these links again
//here I would like to return the loop for each new link in the db with status='n' until the system can not retrieve more links and stops with the script execution
}
}
}
}
Any type of help is very welcome. Thanks in advance !
In pseudo-code you're looking for something like this
do
{
grab new links and add them to database
} while( select all not yet extracted from database > 0 )
Will keep going on and on without recursion...
Hey there, I'm a total newbie and I'm looking for a way to have a search box with autocomplete just like google does.
I've searched and the best prospect that I've found seems to be http://www.pengoworks.com/workshop/jquery/autocomplete.htm which I found on a forum. The guy who suggested it says he uses it with http://code.google.com/p/searchable-behaviour-for-cakephp/ which is dead on because I've managed to install searchable on my last attempt at figuring out cakephp.
The thing is, I've not used much javascript before and I'm a bit confused as to what exactly I'm meant to be doing. The documentation with the autocomplete codes doesn't go into any detail that I can understand.
If we assume that I manage to get searchable behaviour installed correctly, could any kind person explain to me how I would go about making the autocomplete work?
It says to just use:
$("selector").autocomplete(url [, options]);
eg:
$("#input_box").autocomplete("autocomplete_ajax.cfm");
Autocomplete expects an input element with the id "input_box" to exist. When a user starts typing in the input box, the autocompleter will request autocomplete_ajax.cfm with a GET parameter named q
thats the bit I don't get. Where am I meant to put that? And once I've put it somewhere then do I just need to name one of my inputs "input_box"?
thanks in advance.
There are three steps:
1) create a totally normal form with an input field, using the Html helper in your view:
// app/views/foo_bars/search.ctp
<?php
echo $this->Form->create('FooBar');
echo $this->Form->input('field');
echo $this->Form->end('Submit');
?>
2) Have a jquery autocomplete fired:
// app/views/foo_bars/search.ctp
<?php
echo $this->Html->scriptBlock(
.'$("#FooBarField").autocomplete({'
.'source:"/foo_bars/find",'
.'delay: 100,'
.'select:function(event,ui){$(this).parent().parent().children("input").val(ui.item.id);},'
.'open:function(event,ui){$(this).parent().parent().children("input").val(0);}'
.'});'
array('inline' => false));
?>
3) Query a database through a controller to get possible values:
// app/controllers/foo_bars_controller.php
<?php
public function find() {
if ($this->RequestHandler->isAjax()) {
$this->autoLayout = false;
$this->autoRender = false;
$this->FooBar->recursive = -1;
$results = $this->FooBar->find('all', array('fields' => array('id', 'name'), 'conditions' => array('name LIKE "%'.$_GET['term'].'%"')));
$response = array();
$i = 0;
foreach($results as $result){
$response[$i]['value'] = $result['FooBar']['name'];
$response[$i]['id'] = $result['FooBar']['id'];
$i++;
}
echo json_encode($response);
}
}
?>
echo $this->Html->scriptBlock(
'$("#FooBarField").autocomplete({'
.'source:"/Search/find",'
.'delay: 100,'
.'select:function(event,ui){$(this).parent().parent().children("input").val(ui.item.id);},'
.'open:function(event,ui){$(this).parent().parent().children("input").val(0);}'
.'});'.
array('inline' => false));