dongshi6969 2014-07-17 13:45
浏览 45

PHP XPath DOM问题

I am new to XPath, so im sure im making some kind of rookie error, but would anyone be able to see what I am doing wrong here?

I am trying to compile an array for a calendar in the following format:

Array
(
[1] => Array
    (
        [14] => Array
            (
                [date] => 14-07-2014     //I ALREADY HAVE THIS INFORMATION, DO NOT NEED TO FIND WITH XPATH
                [title] => Event Title
                [time] => 9:00 AM
                [description] => Some Description
            )
     )

)

The HTML I am drawing on is as:

<div class="calendar-full">
 <table>
  <tbody>
    <tr>
      <td class="r selected">
        <a>14</a>
        <div class="eventsintable">
          <span class="eventinfo">
             <p class="eventtitle">
               <b>Event Title</b>
             </p>
             <p class="datetimerange"> 9:00AM </p>
             <p class="eventdescription">Some Description</p>
          </span>
       </div>
     </td>
     <td class="l selected">
       <a>15</a>
       ........    // SAME SUB STRUCTURE
     </td>
   </tr>
    <tr>
      <td class="l selected">
        <a>14</a>
        <div class="eventsintable">
          <span class="eventinfo">
             <p class="eventtitle">
               <b>Event Title</b>
             </p>
             <p class="datetimerange"> 9:00AM </p>
             <p class="eventdescription">Some Description</p>
          </span>
          <span class="eventinfo">
            <p class="eventtitle">
              <b>Event Title</b>
            </p>
            <p class="datetimerange"> from 09:00AM </p>
            <p class="eventdescription"></p>
          </span>
       </div>
      </td>
    </tr>
  </tbody>
</table>
</div>

The PHP / XPath I currently have:

    $nodes = $xpath->query("//div[@class='calendar-full']/table/tbody/tr//td[contains(@class, 'selected')]");

foreach ($nodes as $node)
{

    $d = 1;

    $today_query = $xpath->query("./a", $node);
    $today = trim($today_query->item(0)->nodeValue);

    $details = $xpath->query("./div[@class='eventsintable']//span[@class='eventinfo']", $node);

    foreach ($details as $detail){

        $eventDate = $today."-".$month."-".$year;

        $title_details = $xpath->query(".//p[@class='eventtitle']", $detail);
        $title = trim($title_details->item(0)->nodeValue);

        $time_details = $xpath->query(".//p[@class='datetimerange']");
        $time = preg_replace( '/\s+/', ' ', trim($time_details->item(0)->nodeValue));

        $desc_details = $xpath->query(".//p[@class='eventdescription']");
        $desc = preg_replace( '/\s+/', ' ', trim($desc_details->item(0)->nodeValue));

        mysql_query("INSERT INTO sas_sws_calendar (clientID, eventMonth, eventDate, eventTitle, eventTime, eventDescription) VALUES ('$clientID', '$month', '$eventDate', '$title', '$time', '$desc')") or die (mysql_error());


        $d++;
    }
}

I am so close to solving this, the array is forming perfectly, however the title, time and description fields in the array are appearing blank like so:

Array
(
    [14] => Array
        (
            [1] => Array
                (
                    [date] => 14-07-2014
                    [title] => 
                    [time] => 
                    [description] => 
                )

        )

Any help would be very much appreciated!!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥35 平滑拟合曲线该如何生成
    • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
    • ¥15 名为“Product”的列已属于此 DataTable
    • ¥15 安卓adb backup备份应用数据失败
    • ¥15 eclipse运行项目时遇到的问题
    • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
    • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
    • ¥15 自己瞎改改,结果现在又运行不了了
    • ¥15 链式存储应该如何解决
    • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站