-2

How do I put the first foreach statement's output in one column in a table and the other foreach statement's output in another column. I tried something but it put it all in one column for some reason. Here is my code:

<table border="0" align="center">
<?php
foreach($anchors as $a) {
    $text = $a->nodeValue;
    $href = $a->getAttribute('href');
    $i++;

    if ($i > 16) {
        if (strpos($text, "by owner") === false) {
            if (strpos($text, "map") === false) {
                echo "<tr><td><a href =' ".$href." '>".$text."</a><br/></td></tr>";
            }
        }
    }
    foreach($span as $s) {
        echo "<tr><td>".$s->nodeValue."</td></tr>";
    }
}
?>
</table>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user1973004
  • 15
  • 1
  • 8

2 Answers2

2

<tr></tr> marks a row. <td></td> marks a column. To make 2 columns, use just one set of <tr> tags per iteration, with two sets of <td></td>s between them.

That said, what exactly is $span? Does it contain the same number of elements as $anchors, and you want to display one item from each per row? If so you'll need to restructure your code a bit. There are several ways to do this—here's a simple way:

<table border="0" align="center">
<?php

$i = 0;

foreach($anchors as $a) {
    echo "<tr>";

    $text = $a->nodeValue;
    $href = $a->getAttribute('href');

    if ($i >= 16) {
        if (strpos($text, "by owner") === false) {
            if (strpos($text, "map") === false) {
                echo "<td><a href =' ".$href." '>".$text."</a><br/></td>";
            }
        }
    } else {
       echo "<td></td>";    #output a blank cell in the first column
    }

    echo "<td>" . $span[$i]->nodeValue . "</td>";
    echo "</tr>";

    ++$i
}
?>
</table>

EDIT: It looks like your $span is a DOMNodeList object, not an array. I don't have experience with this, but it looks like you can use the DOMNodelist::item function to get the current item in the list (see http://php.net/manual/en/domnodelist.item.php):

echo "<td>" . $span->item($i)->nodeValue . "</td>";

So try changing the respective line in my answer to that.

user428517
  • 4,132
  • 1
  • 22
  • 39
  • 1
    edited. not 100% sure this is what you want though because your question isn't very clear. – user428517 Apr 23 '13 at 21:32
  • it is what I want but how do I do it. – user1973004 Apr 23 '13 at 21:34
  • the code i pasted should do what you want ... assuming it does what you want. like i said, your question isn't very clear, so i'm not sure. :) — see the question i ask in my answer. – user428517 Apr 23 '13 at 21:34
  • Fatal error: Cannot use object of type DOMNodeList as array in /home/content/48/10901548/html/bot.php on line 123 – user1973004 Apr 23 '13 at 21:38
  • oh, it's not an array. try `$span($i)->nodeValue`. if that doesn't work, there's surely another way to access the element you want using `$i`, but i don't know how off the top of my head. maybe check http://php.net/manual/en/domnodelist.item.php – user428517 Apr 23 '13 at 21:38
  • It contains scraped content. – user1973004 Apr 23 '13 at 21:40
1

It is hard without an idea of the data, but something like this perhaps:

   // start a table
   echo '<table>';

   // for as long as there are elements in both span and anchors
   for ($i=0; $i < $anchors->length && $i < $span->length; $i++) { 
       // start a new table row
       echo '<tr>';

       // get the current span and anchor
       $a = $anchors->item($i);
       $s = $span->item($i);

       // print them
       $text = $a->nodeValue;
       $href = $a->getAttribute('href');
       // col 1, number
       echo '<td>'.$i.'</td>';
       // col 2, anchor
       echo '<td><a href ="' .$href. '">'.$text.'</a></td>';
       // col 3, span
       echo '<td>'.$s->nodeValue.'</td>';

       // close the table row
       echo '</tr>';
    }

    // close the table
    echo '</table>';

(code not tested) It is difficult to be more specific without the actual data.

This uses the 'current' and 'next' built in to php.

A few hints/remarks/sidenotes that may help you on the way:
- Note that I used single quotes cause they are much better for performance (double quotes will be interpreted by php).
- Try to use as little loops (for, while, foreach) as possible. They are a powerfull tool, but can drain memory and performance quickly!
- Only nest loops if you are working with multiple dimensions (array inside array), which is not the case here (I think)
- Try to limit the number of nested blocks (if inside if inside if inside loop). I try to go never deeper then 2 levels (which is not an absolute rule off course, just a good standard). If not possible create a function.
- Comment your code! I have difficulty understanding your code (and I write PHP daily for a living), and I can imagine you will to in a couple of weeks. Commenting may look like a waste of time, but it will ease debugging a lot, and is a blessing when updating your (or someone elses) code later on!

EDIT:
I just noticed you are not working with a DOMNodeList and not an array, so I updated my code. Should work fine, and a lot cleaner code imo. Like I said, hard without seeing the data...

Pevara
  • 14,242
  • 1
  • 34
  • 47
  • nice answer, although the difference in performance between single and double quotes is so minuscule that it's irrelevant (see: http://stackoverflow.com/questions/482202/is-there-a-performance-benefit-single-quote-vs-double-quote-in-php). especially in this script, it's a non-issue. – user428517 Apr 23 '13 at 21:46
  • 'much better' is indeed a bit overrated perhaps. I just consider it good practice... – Pevara Apr 23 '13 at 21:59
  • oh, same here. i've just found it's not worth trying to get other people to do it. +1 for the added advice. – user428517 Apr 23 '13 at 22:03
  • Hi I tried this but I get an error on the line where it says "$href = $a->getAttribute('href');" the error is: al error: Call to a member function getAttribute() on a non-object – user1973004 Apr 23 '13 at 22:21
  • this is the same problem you encountered when you tried my answer. neither `$anchors` nor `$span` are actually arrays, so looping through them like this answer does is not going to work (`foreach` works because it doesn't only accept arrays). **please** read this page: http://php.net/manual/en/domnodelist.item.php and try using the `item()` function there—i think it will work. see the last line i added in my answer for more. – user428517 Apr 23 '13 at 22:26