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!!