Hi i am trying to use simple_html_dom for a text(website) clustering project but i have run into a weird problem. When i use echo inside the outer loop the url and the snippet are what you would expect but when i try to echo the array contents i have gathered outside the loop the urls are ok but the snippets are gone and the last snippet is in their place.
<?php
// create HTML DOM
include("simple_html_dom.php");
$search_query = 'something';
$j = 1;
$k = 1;
/*************************GOOGLE***************************/
for ($i = 0; $i < 1; $i++) {
$url = sprintf('http://www.google.com/search?q=%s&start=%d', $search_query, 10 * $i);
$html = file_get_html($url);
foreach ($html->find('a[class=l]') as $element) {
$urls[$j] = $element->href;
echo $element->href . "\n\n\n\n\n";
$j++;
}
foreach ($html->find('div[class=s]') as $element) {
$snippets[$k] = $element->innertext;
echo $element->innertext . "\n\n\n\n\n";
$k++;
}
}
$j = 1;
foreach ($snippets as $elemement) {
echo $urls[$j] . "\n" . $element . "\n\n\n\n";
$j++;
}
?>
Are you sure you did not made a typo in your code?
foreach ($snippets as $elemement) {
echo $urls[$j] . "\n" . $element . "\n\n\n\n";
$j++;
}
element and elemement are different; Your loop executes fine but your statement probably doesn't.
You made a typo, $elemenent really should be $element.
foreach ($snippets as $element) {
echo $urls[$j] . "\n" . $element . "\n\n\n\n";
$j++;
}
This is one reason to get used to make readable code. It's not because others like it, but because it makes debugging much easier.
Related
I want to access the values of an associative array in PHP. I populate the array using the following loop in PHP:
$db = array("a","b","c");
foreach ($db as $q) {
$$q = 'value';
}
This version prints the correct values
foreach ($db as $q) {
echo '<li>'; echo $$q; echo '</li>';
}
\\THIS GIVES ME THE CORRECT OUTPUT <li>value</li><li>value</li><li>value</li>
But I want to access the values through their index
$num = count($db);
for ($i = 0; $i < $num; $i++) {
echo '<li>'; echo $$db[$i]; echo '</li>';
}
\\\\THIS GIVES ME THE WRONG OUTPUT (EMPTY STRINGS <li></li><li></li><li></li>
What is going wrong in the second version? How can I access the values in this associative array through an index correctly?
echo '<li>'; echo $$db[$i]; echo '</li>';
In this line is one $ too much. Write:
echo '<li>'; echo $db[$i]; echo '</li>';
This should do the trick.
PS: You don't have to write echo everytime. Use string concatenation:
echo '<li>' . $db[$i] . '</li>';
You are trying to do something quite strange, anyway the solution to your problem are braces: { }
$num = count($db);
for ($i = 0; $i < $num; $i++) {
echo '<li>'; echo ${$db[$i]}; echo '</li>';
}
Look how braces are resolving the ambiguity, since without them, php wouldn't know if you were referring to ${$db}[$i] or ${$db[$i]}
This one only covers the first record in the array -- $form[items][0][description]. How could I iterate this to be able to echo succeeding ones i.e
$form[items][1][description];
$form[items][2][description];
$form[items][3][description];
and so on and so forth?
$array = $form[items][0][description];
function get_line($array, $line) {
preg_match('/' . preg_quote($line) . ': ([^\n]+)/', $array['#value'], $match);
return $match[1];
}
$anchortext = get_line($array, 'Anchor Text');
$url = get_line($array, 'URL');
echo '' . $anchortext . '';
?>
This should do the trick
foreach ($form['items'] as $item) {
echo $item['description'] . "<br>";
}
I could help you more if I saw the body of your get_line function, but here's the gist of it
foreach ($form['items'] as $item) {
$anchor_text = get_line($item['description'], 'Anchor Text');
$url = get_line($item['description'], 'URL');
echo "{$anchor_text}";
}
You can use a for loop to iterate over this array.
for($i=0; $i< count($form['items']); $i++)
{
$anchortext = get_line($form['items'][$i]['description'], 'Anchor Text');
$url = get_line($form['items'][$i]['description'], 'URL');
echo '' . $anchortext . '';
}
This is part of the working code that shows the entire array;
$files = filelist("./",1,1); // call the function
shuffle($files);
foreach ($files as $list) {//print array
echo "<h4> " . $list['name'] . " </h4>";
// echo "Directory: " . $list['dir'] . " => Level: " . $list['level'] . " => Name: " . $list['name'] . " => Path: " . $list['path'] ."<br>";
How do I modify it so that it only displays 10 or 15 list instead of all?
Use a counter to limit the number of iterations:
$counter = 0;
foreach ($files as $list) {//print array
// your loop code here...
$counter++;
if ($counter > 10) break;
}
If you know the keys or indexes of the array you can do what KingCrunch is doing a lot faster by simple for loop
for($i=0; $i<=14; $i++) {
// echo $file[$i];
}
There is a function for it
foreach(array_slice($files, 0, 15) as $file) {
/* your code here */
}
http://php.net/array-slice
Another solution is to use array_rand() instead of shuffle() and array_chunk()
foreach (array_rand($files, 15) as $key) {
$file = $files[$key];
// Your code here
}
http://php.net/array-rand
Note, that this keeps the order of the keys (see salathes comment below).
I have an array containing a "Variable" amount of results/entries.
I use foreach as normal to echo the array results.
Problem: I want to wrap every 5 results from the array in Unordered list.
I do not know the total number of results since it's variable. So for example if it contains 18 items. It should display 4 ULs, the first 3 ULs containing 5 results and the last UL contains only the remaining 3 items.
Is that simple to do? Thanks very much in advance for your help. :)
I rarely used this function, but array_chunk seems to do what you want.
$chunks = array_chunk($original, 5);
foreach ($chunks as $each_chunk) {
// echo out as unordered list
}
This is a fairly straightforward algorithm:
$htmlOutput = "";
for($i=0;$i<count($myArray);$i++)
{
if($i%5==0)
{
$htmlOutput.= "<ul>";
}
$htmlOutput.= "<li>".$myArray[$i]."</li>";
if($i%5==4)
{
$htmlOutput.= "</ul>";
}
}
if(count($myArray)%5!=0)
{
$htmlOutput.= "</ul>";
}
echo $htmlOutput;
You may need to modify this a bit to suit your requirements
$cnt = 1;
foreach ($arr as $key => $val)
{
if($cnt==1) echo "<ul>";
echo "<li>$val</li>";
$cnt++;
if($cnt==5)
{
echo "</ul>";
$cnt=1;
}
}
How about this:
<?php
$num_per_list = 5; // change me
$dudes = array("bill","jim","steve","bob","jason","brian","dave","joe","jeff","scott");
$count = 0;
$list_items = "";
foreach($dudes as $dude) {
$break = (($count%$num_per_list) == ($num_per_list-1));
$list_items .= "<li>" . $dude . "</li>";
if(($break) || (count($dudes)==($count+1))) {
$output = "<ul>" . $list_items . "</ul>";
$list_items = "";
// Output html
echo $output;
}
$count++;
}
?>
Let's suppose you put the list in an array...
$count = 0;
foreach ($unorderedList as $item) {
$count = ($count + 1)%5;
if ($count == 0) {
// wrap here
}
...do the stuff for every item you need
}
I am trying to determine the end of a foreach loop that is seeded with a collection of DOMNodeList. Currently, I am using a for loop would like to avoid having a 'magic' number there. I do know there are only going to be 8 columns, but I would like the code me generic for other applications.
Is it possible to convert this to a Foreach loop? I have tried the end() and next() functions, but they are not returning any data and I suspect that they only work on arrays and not this DOMNodeList collection.
The code is building a CSV file without the trailing ','
Current output is:
"Value 1","Value 2","Value 3","Value 4","Value 5","Value 6","Value 7","Value 8"
Here is an example of code:
$cols = $row->getElementsByTagName("td");
$printData = true;
// Throw away the header row
if ($isFirst && $printData) {
$isFirst = false;
continue;
}
for ($i = 0; $i <= 8; $i++) {
$output = iconv("UTF-8", "ASCII//IGNORE", $cols->item($i)->nodeValue);
$output2 = trim($output);
if ($i == 8) {
// Last Column
echo "\"" . $output2 . "\"" . "\n";
} else {
echo "\"" . $output2 . "\"" . ",";
}
}
You can use:
$cols->length
To retrieve the number of items in a DOMNodeList.
See http://php.net/manual/en/class.domnodelist.php
Edit:
If you change you're code to this, you don't have to worry about the trailing comma, or the length:
$output = array();
foreach ($cols as $item) {
$output = iconv("UTF-8", "ASCII//IGNORE", $item->nodeValue);
$output2 = trim($output);
$output[] = '"' . $output2 . '"';
}
$outputstring = implode(',', $output);
$cols->length
Should give you the number of items in the list
for ($i = 0; $i < $cols->length; $i++) {
// ...
if ($i == $cols->length - 1) {
// last column