Saturday, 19 July 2014

Modify my PHP code so it parses XML to display result after searching to find it



Not sure anyone would be able to help me but I have this code which pulls data from an XML feed.



<?php
if (!empty($_GET['pin']))
{
$feedURL = 'http://ift.tt/1yHZCXk';
$showOpsWithNoPics = false;

try
{
$xml = simplexml_load_file($feedURL . '?' . time());
$readers = $xml->xpath('/ReaderDetails/Reader[Pin="' . $_GET['pin'] . '"]');

foreach ($readers as $reader)
{
if ($reader->Picture == 'None' && $showOpsWithNoPics == false)
{
$picture = '';
}
elseif ($reader->Picture == 'None' && $showOpsWithNoPics == true)
{
$picture = 'images/defaultpic.jpg';
}
else
{
$picture = $reader->Picture;
}

if ($picture != '')
{
$name = $reader->Name;
$pin = $reader->Pin;
$status = $reader->Status;
$description = $reader->Description;

$arr_skills = '';
$arr_subjects = '';
foreach ($reader->Categories->Category as $Category)
{
foreach ($Category->Skill as $Skill)
{
switch ($Category['name'])
{
case 'Skills':
$arr_skills .= "<li>$Skill</li>";
break;
case 'Subjects':
$arr_subjects .= "<li>$Skill</li>";
break;
}
}
}

$arr_shift = '';
$hourWidth = 20;
$PreviousDate = "";
foreach ($reader->Rota->Shift as $Shift)
{
$Date = $Shift['Date'];
$Day = date("l", strtotime($Date));
$Start = $Shift['Start'];
$Stop = $Shift['Stop'];
if($Stop == '23:59:59' || $Stop == '00:00:00' ){$Stop = '24:00:00';}

$Left = date("H",strtotime($Date.' '.$Start)) * $hourWidth;
$Width = (round(abs(strtotime($Date.' '.$Stop) - strtotime($Date.' '.$Start)) / 60,2)/60)*$hourWidth;



$compare = strcmp ($PreviousDate, $Date);


if ($compare != 0)
{
if ($PreviousDate != '')
{
//this is not the first loop so close off the previous dayrow and hours divs
$arr_shift.= '</div></div>';
}
//create a new day row
$arr_shift .= '<div class="dayrow">';
$arr_shift .= '<div class="day">'.$Day.'</div>';
$arr_shift .= '<div class="hours">';
}
//add the shift object
$arr_shift .= '<div class="shift" title="'. substr($Start,0,5).'-'.substr($Stop,0,5) .'" style="left:'.$Left.'px; width:'.$Width.'px; opacity: 1;"></div>';
//set previouse date to the current shift's date so that we can check if we need to create a new day row next loop
$PreviousDate = $Date;
}
//now we have finshed looping the shifts, either close the last div or display a message to show
//we have no schedule for the op
if ($Date != "")
{
$arr_shift.= '</div></div>';
}
else
{
$arr_shift.= 'We are sorry but they have not scheduled their hours this week. Email for further details.';
}
}
}
}
catch (Exception $e)
{
echo 'cannot display operator profile';
}
}

?>


I put this in my tag. What I want to do is go have a PHP code that displays results for a specific reader without referring to the URL (right now it bases it on the URL example http://ift.tt/1rqTGRI - will return all the information for 1001).


Does anyone with PHP background know what I need to do to have it so I can paste this code in my header with a set PIN number (so I can make a page specific for each one) and then it'll return THOSE results back so I can echo the other bits like $picture etc?


So in a TL/DR way:


1) Search XML for a PIN number 2) Return the other variables like $name 3) I then echo statement the returned variables


I think it's something to do with this line but I cannot seem to make it work:



$readers = $xml->xpath('/ReaderDetails/Reader[Pin="' . $_GET['PIN'] . '"]');

No comments:

Post a Comment