Im trying to create a table of Jobs on my site, pulling info from an xml feed I have access to... I've looked at various examples online and videos but I can't seem to understand how it works. My xml feed returns the following node structure:
<OutputVacancyAsXml>
<Vacancy>
<VacancyID></VacancyID>
<Job></Job>
<ClosingDate></ClosingDate>
</Vacancy>
</OutputVacancyAsXml>
I've had success with pulling through one item with this code:
<?php
$x = simplexml_load_file('https://www.octopus-hr.co.uk/recruit/OutputVacancyAsXml.aspx?CompanyID=400-73A3BCA1-D952-4BA6-AADB-D8BF3B495DF6');
echo $x->Vacancy[5]->Job;
?>
But converting it to foreach seems to be where I'm struggling. Heres the code I have tried so far with no luck;
<?php
$html = "";
$url = "https://www.octopus-hr.co.uk/recruit/OutputVacancyAsXml.aspx?CompanyID=400-73A3BCA1-D952-4BA6-AADB-D8BF3B495DF6";
$xml = simplexml_load_file($url);
for ($i = 0; $i < 10; $i++) {
$title = $xml->OutputVacancyAsXml->Vacancy[$i]->job;
$html .= "<p>$title</p>";
}
echo $html;
?>
Thanks all :)
Taken from the documentation
Note:
Properties ($movies->movie in previous example) are not arrays. They
are iterable and accessible objects.
With that kept in mind you can simple run over the nodes with foreach
$xml = simplexml_load_file($url);
foreach ($xml->OutputVacancyAsXml->Vacancy as $vacanacy)
{
echo (string)$vacanacy->Job; // Echo out the Job Title
}
Ok looks like I found a solution. Heres the code that worked for me plus it contains a little bit of code that pulls out duplicated (it was displaying each item 4 times!)...
<?php
$x = simplexml_load_file('https://www.octopus-hr.co.uk/recruit/OutputVacancyAsXml.aspx?CompanyID=400-73A3BCA1-D952-4BA6-AADB-D8BF3B495DF6');
$num = count($x->Vacancy);
//echo "num is $num";
$stopduplicates = array();
for ($i = 0; $i < $num; $i++) {
$job = $x->Vacancy[$i]->Job;
$closingdate = $x->Vacancy[$i]->ClosingDate;
// http://stackoverflow.com/questions/416548/forcing-a-simplexml-object-to-a-string-regardless-of-context
$vacancyid = (string) $x->Vacancy[$i]->VacancyID;
if (!in_array($vacancyid, $stopduplicates)) {
echo '
<tr class="job-row">
<td class="job-cell">'.$job.'</td>
<td class="date-cell">'.$closingdate.'</td>
<td class="apply-cell">
Apply Here
</td>
</tr>';
}
$stopduplicates[] = $vacancyid;
} //print_r($stopduplicates);
?>
I have never used XML before and am trying to loop through the XML and display all the display names in the 'A Team'. The code I am using is outputting 10 zeros and not the names.
The code I am using is attached below along with the feed.
Any assistance is much appreciated!
feed: https://apn.apcentiaservices.co.uk/ContactXML/agentfeed?organisation=se724de89ca150f
<?php
$url = 'https://apn.apcentiaservices.co.uk/ContactXML/agentfeed?organisation=se724de89ca150f';
$html = "";
$xml = simplexml_load_file($url);
for($i = 0; $i < 10; $i++){
$title = $xml->organisation['APN']->brand['AllStar Psychics']->pool['A Team']->agent[$i]->display-name;
echo $title;
}
echo $html;
?>
This might getthe basics you asked for. Not sure if it's what you want. I'm not that good at xpath.
$mydata = $xml->xpath('/organisation/brand/pool[#name="A Team"]//display-name');
foreach($mydata as $key=>$value){
echo('Name:' . $value .'<br>');
}
I need to read an XML file, i watched some tutorials and tried different sollutions, but for some reason I can't figure out why it doenst work.
The XML file that I want to read: http://www.voetbalzone.nl/rss/rss.xml
This is the code that im using:
$xml= "http://www.voetbalzone.nl/rss/rss.xml"
for ($i = 0; $i < 10; $i++)
{
$title = $xml->rss->channel->item[$i]->title;
}
The error I get: Premature end of data in tag
It works for me like this:
<?php
$xml = simplexml_load_file("http://www.voetbalzone.nl/rss/rss.xml");
for ($i = 0; $i < 10; $i++)
{
$title = $xml->channel->item[$i]->title;
}
?>
Note that you are overwriting the variable $title each time, so that you will have the title of the 10. element in it after the loop finished [I assume that is not what you want?]
To get all 'item'-Elements inside 'channel' as an Array to iterate through you can use xpath like this:
<?php
$xml = simplexml_load_file("http://www.voetbalzone.nl/rss/rss.xml");
$item_array = $xml->xpath("//rss/channel/item");
foreach($item_array as $item) {
echo $item->title . "\n";
}
?>
I would suggest to read about php's SimpleXML here: http://php.net/manual/en/book.simplexml.php
I'm a newbie in PHP and I'm confused about GET method.
Why the $text in the condition of the loop works with Appserv in Windows 7, but when I tried this code with Xampps on Mac it won't work I've to use for($i=0; $i<strlen($_GET['text']); $i++) instead.
At first, I understand that after I used isset($_GET['text']) so next time I just use only $text, but now I'm confused.
<? $color = array("#FFCCFF", "#FFCCCC", "#FFCC99", "#FF99FF", "#FF99CC",
"#FF9999", "#FF66FF", "#FF66CC", "#FF6699", "#FF6666");
if (isset($_GET['text'])) {
for($i=0; $i<strlen($text); $i++) {
$j = $i%10 ?>
<font color=<?= $color[$j]?>><? echo "$text[$i]"; ?></font>
}
} else {
echo "Empty String";
} ?>
The problem is solved by many of your help.
<?php $color = array("#FFCCFF", "#FFCCCC", "#FFCC99", "#FF99FF", "#FF99CC",
"#FF9999", "#FF66FF", "#FF66CC", "#FF6699", "#FF6666");
if( isset($_GET['text'])) {
$text = $_GET['text'];
for( $i=0; $i<strlen($text); $i++) {
$j = $i%10;
echo "<font color=$color[$j]>$text[$i]</font>";
}
} else
echo "Empty string";
?>
btw I'm trying to use HTML + PHP only because I want to practice with HTML before go in deep with CSS.
The actual answer to your question, if $text is working as an alias for $_GET['text'] is probably that your Windows server is configured with register_globals set to on, which would mean that anything passed over in your query string would be turned into the appropriate variable.
ie. ?awesome=true == $awesome = 'true'
This is bad. Disable register_globals at the offending side, and use $_GET['text'] to access your data.
Your code would look better a little something like this:
<?php
$color = array("#FFCCFF", "#FFCCCC", "#FFCC99", "#FF99FF", "#FF99CC",
"#FF9999", "#FF66FF", "#FF66CC", "#FF6699", "#FF6666");
if (isset($_GET['text'])) {
$text = $_GET['text'];
for($i=0; $i < strlen($text); $i++) {
$j = $i % 10; ?>
<span style="color: <?= $color[$j] ?>"><?= htmlentities($text[$i]); ?></span>
<?php }
} else {
echo "Empty String";
}
?>
Note that I have tidied up your code and made it slightly more sane/safe. htmlentities is used to stop XSS vulns that could come from this, despite being unlikely due to splitting the string. You were mixing up <?php echo .. ?> and <?= .. ?> for some reason, despite them being the exact same thing. Also, don't use <font>.
You said this:
At first, I understand that "after I used isset($_GET['text']) so next time I just use only($text), but now I'm confused.
If you know you're mixing them, why are you doing it? If you're checking for $_GET['text'] being set, then it's only logical that you would use that for access also.
Okay, why are you dropping in and out of PHP every line? It is allowed to have more than one line of PHP at a time, you know!
$_GET['text'] is a variable. Accessing it does nothing special, but it is special in that you can access it regardless of scope (it is superglobal). Referring to is as $text only works if the autoregister globals setting is enabled, which is not recommended for various reasons.
So, your code should look like:
<?php
$color = array(".....");
if( isset($_GET['text'])) {
$l = strlen($_GET['text']);
for( $i=0; $i<$l; $i++) {
$j = $i%10;
echo "<span style=\"color: ".$color[$j].";\">".$text[$i]."</span>";
}
}
else echo "Empty string";
?>
I also took the liberty of updating your HTML out of the last milennium.
You should initialize $text variable first, something like this:
$text = $_GET['text'];
This should work without any problems.
I'm still unsure as to what you're doing, but:
$colours = array("#FFCCFF", "#FFCCCC", "#FFCC99", "#FF99FF", "#FF99CC", "#FF9999", "#FF66FF", "#FF66CC", "#FF6699", "#FF6666");
if (isset($_GET['text'])) {
$text = $_GET['text'];
for ($i = 0; $i < strlen($text); $i++) {
$j = $i%10;
echo "<span style='color: {$colours[$j]}'>{$text[$i]}</span>";
}
}
else {
echo 'No text';
}
I am aware that this:
<?php
$file2 = file('website/files/myfolder/file.html');
echo $file2[8];
echo $file2[9];
echo $file2[10];
echo $file2[11];
?>
would give me the contents on lines 8,9,10,11 in the file.html file, however I would like to do this for about 100 lines ranging from lines 23 to 116. How would I accomplish this without using the echo $file2[NUMBER]; one hundrend times?
Another way to explain what I am trying to do would be possibly if I could do this in php:
echo $file2[23-116];
obviously this will not work but that is the concept.
Thanks for your help!
Use a loop like so
$data = file('website/files/myfolder/file.html');
for ($i = 23; $i <= 116; $i++) {
echo $data[$i];
}
http://php.net/manual/en/control-structures.for.php
Or you could splice the data:
$data = file('website/files/myfolder/file.html');
echo implode(PHP_EOL, array_splice($data, 23, 116 - 23));
http://php.net/manual/en/function.array-splice.php
http://php.net/manual/en/function.implode.php
How about something like this?
$file = file('website/files/myfolder/file.html');
foreach( range( 23, 116) as $i) {
echo $file[$i];
}