2011-09-16 111 views
0

幫助,打印HTML表使用PHP DOM文檔

我想使用PHP DOM文檔,但掙扎 我也跟下面把代碼寫在我們的網頁打印此HTML代碼。你可以幫我嗎。

<table width="610" id="machine" border="0" cellSpacing="10" 
    cellPadding="0"> 

    <TBODY> 
     <TR> 
      <TD colSpan=2 align=left> 
      <DIV id=breadcrumbs><A href="/new-and-used-machinery.php">All 
      Machines</A> &nbsp;&gt;&nbsp; <A 
       href="/listing.php?cat=Air+Compressors">Air Compressors</A> 
      &nbsp;&gt;&nbsp;</DIV> 
      <DIV id=ForSale>FOR SALE:</DIV> 
      <H1>5 CFM Used Speedaire Air Compressor, Single Phase, 60 Gallon 
      tank, 1.5 H.P. #A1194</H1> 
      </TD> 
     </TR> 
     <TR> 
      <TD id=photos vAlign=top width=270 align=middle> 
      <DIV id=ClickPhotos>Click On Photos For Larger Image:</DIV> 
      <!-- PHOTOS DB div style="margin-bottom:12px"><img src="thumb.php?id=A1194.jpg&amp;w=250&amp;h=250"></div --><!-- ORIG PHOTOS div style="margin-bottom:12px"><img src="thumb.php?f=A1194.jpg&amp;w=250&amp;h=250"></div --> 
      <DIV style="MARGIN-BOTTOM: 12px"><A 
       href="/photos.php?id=7135&amp;p=1"><IMG 
       title="Used Speedaire Air Compressor" 
       alt="Used Speedaire Air Compressor" 
       src="/class/class_Image.php?load=cGhvdG9zL0ExMTk0LmpwZ3wyNTB8MjUwfDF8"></A> 
      </DIV> 
      <!-- PHOTOS DB div style="margin-bottom:12px"><img src="thumb.php?id=A1194_1.jpg&amp;w=250&amp;h=250"></div --><!-- ORIG PHOTOS div style="margin-bottom:12px"><img src="thumb.php?f=A1194_1.jpg&amp;w=250&amp;h=250"></div --> 
      <DIV style="MARGIN-BOTTOM: 12px"><A 
       href="/photos.php?id=7135&amp;p=2"><IMG 
       title="Used Speedaire Air Compressor" 
       alt="Used Speedaire Air Compressor" 
       src="/class/class_Image.php?load=cGhvdG9zL0ExMTk0XzEuanBnfDI1MHwyNTB8MXw="></A> 
      </DIV> 
      </TD> 
      <TD id=details vAlign=top width=310 align=left> 
      <TABLE 
       style="BORDER-BOTTOM: #cdcdcd 1px solid; BORDER-LEFT: #cdcdcd 1px solid; PADDING-BOTTOM: 10px; BACKGROUND-COLOR: #eee; PADDING-LEFT: 10px; PADDING-RIGHT: 10px; BORDER-TOP: #cdcdcd 1px solid; BORDER-RIGHT: #cdcdcd 1px solid; PADDING-TOP: 10px; -moz-border-radius: 10px; -webkit-border-radius: 10px" 
       id=machine_buttons border=0 cellSpacing=3 cellPadding=0> 
       <TBODY> 
        <TR> 
         <TD><A onmouseover="swapImg('Manufact', true)" 
          onmouseout="swapImg('Manufact', false)" 
          href="/manufacturer.php?brand=Speedaire"><IMG id=icon_Manufact 
          border=0 alt="" src="/images/machine_button_Manufact.gif" 
          width=66 height=33><BR> 
         Manufacturer Info</A></TD> 
        </TR> 
        <TR> 
         <TD><A onmouseover="swapImg('Category', true)" 
          onmouseout="swapImg('Category', false)" 
          href="/listing.php?cat=Air+Compressors"><IMG id=icon_Category 
          border=0 alt="" src="/images/machine_button_Category.gif" 
          width=66 height=33><BR> 
         Similar Machines</A></TD> 
        </TR> 
        <TR> 
         <TD><A onmouseover="swapImg('Brochure', true)" 
          onmouseout="swapImg('Brochure', false)" 
          href="/manufacturer.php?brand=Speedaire#brochures" target=_blank><IMG 
          id=icon_Brochure border=0 alt="" 
          src="/images/machine_button_Brochure.gif" width=66 height=33><BR> 
         OEM Brochure</A></TD> 
        </TR> 
        <TR> 
         <TD><A style="CURSOR: pointer" id=shadowbox_email class=sb 
          title="Used Speedaire Air Compressor" href="/share.php?id=7135" 
          rel=shadowbox;width=520;height=500 shadowboxCacheKey="1" 
          onMouseOver="swapImg('Email', true)" 
          onMouseOut="swapImg('Email', false)" jQuery1316148993734="2"><IMG 
          id=icon_Email border=0 alt="" 
          src="/images/machine_button_Email.gif" width=66 height=33 
          jQuery1316148993734="3"><BR> 
         Email A Friend</A></TD> 
        </TR> 
        <TR> 
         <TD><A onmouseover="swapImg('Ship', true)" 
          onmouseout="swapImg('Ship', false)" href="/shipping.php?id=7135"><IMG 
          id=icon_Ship border=0 alt="" 
          src="/images/machine_button_Ship.gif" width=66 height=33><BR> 
         Shipping</A></TD> 
        </TR> 
       </TBODY> 
      </TABLE> 
      <DIV id=QuickQuote><A href="/machine-print.php?id=7135" target=_blank><IMG 
       title="Print a Quick Quote!" border=0 alt="Print a Quick Quote!" 
       align=absMiddle 
       src="http://www.sterlingmachinery.com/images/quick_quote.jpg"> </A></DIV> 
      <DIV style="MARGIN: 5px 0px 0px 45px"> 
      <FORM method=post name=addmachine action=/request-quote.php 
       jQuery1316148993734="5"><INPUT id=add 
       title="Add Machine to Quote Cart" value="Add Machine to Quote Cart" 
       alt="Add Machine to Quote Cart" 
       src="http://www.sterlingmachinery.com/images/quick_add.jpg" 
       type=image name=add jQuery1316148993734="7"> <IMG 
       style="DISPLAY: none; CURSOR: pointer" id=remove 
       title="Remove from quote cart" name=remove 
       alt="Remove from quote cart" src="/images/remove.gif" width=16 
       height=16 jQuery1316148993734="8"> 
      <DIV style="MARGIN-TOP: 5px"><INPUT id=view title="View Quote Cart" 
       value="View Quote Cart" alt="View Quote Cart" 
       src="http://www.sterlingmachinery.com/images/quick_view.jpg" 
       type=image name=view></DIV> 
      </FORM> 
      </DIV> 
      <BR> 
      <TABLE id=machine_stats border=0 cellSpacing=5 cellPadding=0 
       width=300> 
       <TBODY> 
        <TR> 
         <TD class=field vAlign=top align=right>Stock #</TD> 
         <TD vAlign=top align=left>A1194</TD> 
        </TR> 
        <TR> 
         <TD class=field vAlign=top align=right>Manufacturer:</TD> 
         <TD vAlign=top align=left><A 
          href="/manufacturer.php?brand=Speedaire">Speedaire</A></TD> 
        </TR> 
        <TR> 
         <TD class=field vAlign=top align=right>Model:</TD> 
         <TD vAlign=top align=left><A 
          href="/listing.php?cat=Air+Compressors"></A></TD> 
        </TR> 
        <TR> 
         <TD class=field vAlign=top align=right>Capacity:</TD> 
         <TD vAlign=top align=left><A 
          href="/listing.php?cat=Air+Compressors">5 CFM</A></TD> 
        </TR> 
        <TR> 
         <TD class=field vAlign=top align=right>Price:</TD> 
         <TD vAlign=top align=left><SPAN style="BACKGROUND-COLOR: #ffff00">$350</SPAN></TD> 
        </TR> 
        <TR> 
         <TD class=field vAlign=top align=right>Category:</TD> 
         <TD vAlign=top align=left><A 
          href="/results.php?keywords=Air+Compressors">Air Compressors</A></TD> 
        </TR> 
        <TR> 
         <TD class=field vAlign=top align=right>Condition:</TD> 
         <TD vAlign=top align=left>Used Machinery</TD> 
        </TR> 
       </TBODY> 
      </TABLE> 
      <FIELDSET><LEGEND>Full Description</LEGEND> 
      <TABLE id=machine_specs border=0 cellSpacing=0 cellPadding=4 
       width=300> 
       <TBODY> 
        <TR> 
         <TD 
          style="BORDER-RIGHT-WIDTH: 0px; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px" 
          class=field vAlign=top align=right>CFM Rating:</TD> 
         <TD 
          style="BORDER-RIGHT-WIDTH: 0px; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px" 
          vAlign=top align=left>5</TD> 
        </TR> 
        <TR> 
         <TD class=field vAlign=top align=right>Tank Size:</TD> 
         <TD vAlign=top align=left>60 Gallon</TD> 
        </TR> 
        <TR> 
         <TD class=field vAlign=top align=right>Weight:</TD> 
         <TD vAlign=top align=left>400 LBS.</TD> 
        </TR> 
       </TBODY> 
      </TABLE> 
      </FIELDSET> 
      <FIELDSET><LEGEND>Equipped With</LEGEND>Single Phase Motor<BR> 
      Horsepower: 1 1/2</FIELDSET> 
      <DIV class=privates>* Temporary specifications subject to 
      verification.<BR> 
      &nbsp; All quotations subject to prior sale.</DIV> 
      </TD> 
     </TR> 
    </TBODY> 

</table> 

我已經寫了這段代碼,但它沒有得到正確的。 外部foreach循環運行17次,但應該只有兩次,因爲外部表只有兩行。

$xml = new DOMDocument(); 
$xml->validateOnParse = true; 
$xml->loadHTMLFile($url); 

$xpath = new DOMXPath($xml); 

$table =$xpath->query("//*[@id='machine']")->item(0); 

echo "<table border=2>"; 
$rows = $table->getElementsByTagName("tr"); 

foreach ($rows as $row) //there i only two rows. 
{ 
echo "<tr>"; 
    $cells = $row -> getElementsByTagName('td'); 

    foreach ($cells as $cell) 
    { 
    echo "<td>"; 
    foreach ($cell->getElementsByTagName('table') as $innerTable) 
    { 
     foreach ($innerTable->getElementsByTagName("tr") as $innerTrs) 
     { 
     foreach ($innerTrs->getElementsByTagName('td') as $innerCell) 
     { 
      foreach ($innerCell->getElementsByTagName('a') as $a) 
      { 
       echo '<br><a href="'.GetMainBaseFromURL($url).$a->getAttribute('href').'">'.$a->nodeValue.'</a>'; 

       foreach ($a->getElementsByTagName('img') as $img) 
       echo '<br><a href="'.GetMainBaseFromURL($url).$a->getAttribute('href').'"> <img src="'.GetMainBaseFromURL($url).$img->getAttribute('src').'"></a>'; 
      }   
     } 
     } 
    } 

    echo "</td>"; 
    } 
    echo "</tr>"; 
} 
echo "</table>"; 

那麼我怎麼能打印,因爲它是我的網頁中的HTML表使用PHP DOM文檔。 我是新的PHP DOM。

謝謝。 非常感謝。

+0

什麼不起作用?你有什麼錯誤嗎? – hakre

+0

沒有得到錯誤,但它僅打印內部html表格數據,而不打印外部表格內部但在內部html表格外部的外部數據。 – Bajrang

+0

然後,我必須寫這個?第一個tr的數據不是打印。 – Bajrang

回答

1

僅僅因爲您在發佈的HTML代碼中有17個標籤,foreach循環才迭代17。 其實$ rows = $ table-> getElementsByTagName(「tr」);提取樹中任意深度的任何標籤。

,僅保留兩個外,你可以這樣做

$ TBODY = $表 - >的childNodes;

$ outerTr = $ tbody-> childNodes;

的上$ outerTr的迭代

您也可以建立一個XPath表達式只得到兩個外

我沒有然而,測試YOUT代碼在別人的foreach,記住了同樣的問題的getElementsByTagName( 「TR」)(或其他標籤TD,一..)的

+0

謝謝你ab_dev86,你的好溝通解決了我的問題:) – Bajrang

1

如果你只是想複製的表,你可以這樣做:

<?php 
$xml = new DOMDocument(); 
$xml->validateOnParse = true; 
$xml->loadHTMLFile($url); 

$table = $xml->getElementById('machine'); 
echo $xml->saveXML($table); 
?> 
2

getElementsByTagName適用於整個文檔。該文檔有17 tr個元素,因此無論放置哪個元素,它都會返回它們。

當你想尋找具體tr元素,或特定表內的圖像更具體,我建議你讓XPath的做的大部分工作:

$xpath = new DOMXPath($xml); 

$nodes =$xpath->query("//table[@id='machine']/tbody/tr/td/div/a/img"); 

echo 'Found ', $nodes->length, ":\n"; 

foreach($nodes as $node) 
{ 
    $img = $node; 
    $a = $node->parentNode; 
    $aHref = $a->getAttribute('href'); 
    $imgSrc = $img->getAttribute('src'); 
    printf("Element:\n + href: %s;\n +- src: %s\n\n", $aHref, $imgSrc); 
} 

這將列出3個元素已經(不2)。你需要進一步決定哪些放棄,但這應該更容易。 Demo &輸出:

Found 3: 
Element: 
+ href: /photos.php?id=7135&p=1; 
+- src: /class/class_Image.php?load=cGhvdG9zL0ExMTk0LmpwZ3wyNTB8MjUwfDF8 

Element: 
+ href: /photos.php?id=7135&p=2; 
+- src: /class/class_Image.php?load=cGhvdG9zL0ExMTk0XzEuanBnfDI1MHwyNTB8MXw= 

Element: 
+ href: /machine-print.php?id=7135; 
+- src: http://www.sterlingmachinery.com/images/quick_quote.jpg 

另一個提示:一歸一化數據到像陣列的結構。然後你可以使用你的數組來生成輸出。這樣做可以將輸出中的解析分開,這將使您更容易實現目標。

  • 將文檔解析爲數組。
  • 修改您在該陣列中需要的值(例如GetMainBaseFromURL)。
  • 基於數組生成輸出。